@@ -9,7 +9,6 @@ import { BackButton } from "../../components/BackButton"
99import { BorderButton } from "../../components/BorderButton"
1010import {
1111 postSearchRecipesApi ,
12- postSearchRecipesKeywordsApi ,
1312 getUserFavoritesApi ,
1413 postUserFavoritesApi ,
1514 deleteUserFavoritesApi ,
@@ -44,7 +43,11 @@ export const Result = () => {
4443 [ ...searchInfo . ingredients , searchInfo . dish , searchInfo . cookingTime ] . join ( " " )
4544 )
4645
47- const { data : recipes , isLoading : isLoadingRecipes } = useQuery ( {
46+ const {
47+ data : recipes ,
48+ isLoading : isLoadingRecipes ,
49+ refetch,
50+ } = useQuery ( {
4851 queryKey : [ "recipes" ] ,
4952 queryFn : async ( ) => {
5053 const response = await fetch ( postSearchRecipesApi ( ) , {
@@ -85,10 +88,9 @@ export const Result = () => {
8588 const addFavorite = useMutation ( {
8689 mutationFn : async ( recipeId : number ) => {
8790 if ( ! session ?. access_token ) return [ ]
88- const response = await fetch ( postUserFavoritesApi ( ) , {
91+ const response = await fetch ( postUserFavoritesApi ( recipeId ) , {
8992 method : "POST" ,
9093 headers : { "Content-Type" : "application/json" , Authorization : `Bearer ${ session ?. access_token } ` } ,
91- body : JSON . stringify ( { recipeId : recipeId } ) ,
9294 } )
9395 if ( ! response . ok ) throw new Error ( "お気に入りの追加に失敗しました" )
9496 } ,
@@ -113,39 +115,22 @@ export const Result = () => {
113115 } )
114116
115117 const toggleFavorite = ( recipeId : number ) => {
116- if ( ! favoriteRecipes ) return
117- if ( favoriteRecipes . some ( ( recipe ) => recipe . id === recipeId ) ) {
118+ if ( isFavorited ( recipeId ) ) {
118119 deleteFavorite . mutate ( recipeId )
119120 } else {
120121 addFavorite . mutate ( recipeId )
121122 }
122123 }
123124
124- //----------------------------------------------------------------
125- // フリーワード検索機能
126- //----------------------------------------------------------------
127- const searchRecipesKeywords = useMutation ( {
128- mutationFn : async ( ) => {
129- const response = await fetch ( postSearchRecipesKeywordsApi ( ) , {
130- method : "POST" ,
131- headers : { "Content-Type" : "application/json" } ,
132- body : JSON . stringify ( { keywords : inputContent } ) ,
133- } )
134- if ( ! response . ok ) throw new Error ( "レシピの取得に失敗しました" )
135- const recipes : Recipe [ ] = await response . json ( )
136- return recipes
137- } ,
138- onSuccess : ( recipes ) => {
139- queryClient . setQueryData ( [ "recipes" ] , recipes )
140- } ,
141- } )
142-
143- const onClickSearchRecipesKeywords = ( ) => {
144- searchRecipesKeywords . mutate ( )
125+ const isFavorited = ( recipeId : number | string ) => {
126+ if ( ! favoriteRecipes ) return false
127+ return favoriteRecipes . some ( ( favorite ) => favorite . id . toString ( ) === recipeId . toString ( ) )
145128 }
146129
147- const onChangeHandler = ( e : React . ChangeEvent < HTMLInputElement > ) => {
148- setInputContent ( e . target . value )
130+ const onClickSearch = ( ) => {
131+ searchInfo . ingredients = inputContent . split ( " " )
132+ localStorage . setItem ( "ingredients" , JSON . stringify ( searchInfo . ingredients ) )
133+ refetch ( )
149134 }
150135
151136 if ( isLoadingRecipes || isLoadingFavoriteRecipes ) return < Loading />
@@ -154,8 +139,8 @@ export const Result = () => {
154139 < div className = { styles . header } >
155140 < BackButton onClick = { ( ) => navigate ( "/questions?reset=true" ) } />
156141 < Searchbox
157- onClickHandler = { onClickSearchRecipesKeywords }
158- onChange = { onChangeHandler }
142+ onClickHandler = { onClickSearch }
143+ onChange = { ( e ) => setInputContent ( e . target . value ) }
159144 inputContent = { inputContent }
160145 placeholder = ""
161146 />
@@ -171,20 +156,15 @@ export const Result = () => {
171156 < div className = { styles . cards } >
172157 { recipes . map ( ( recipe ) => (
173158 < Fragment key = { recipe . id } >
174- < RecipeCard recipe = { recipe } favoriteRecipes = { favoriteRecipes } toggleFavorite = { toggleFavorite } />
159+ < RecipeCard recipe = { recipe } isFavorited = { isFavorited ( recipe . id ) } toggleFavorite = { toggleFavorite } />
175160 </ Fragment >
176161 ) ) }
177162 </ div >
178163 </ div >
179164 ) : (
180165 < div className = { styles . emptyResults } >
181166 < EmptyResults />
182- < BorderButton
183- onClick = { ( ) => {
184- navigate ( "/questions?reset=true" )
185- } }
186- disabled = { false }
187- >
167+ < BorderButton onClick = { ( ) => refetch ( ) } disabled = { false } >
188168 < h3 > 再検索する</ h3 >
189169 </ BorderButton >
190170 </ div >
0 commit comments