-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from SofiaCantero24/favorites
feat: favorites view and API integration
- Loading branch information
Showing
20 changed files
with
511 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { type UseMutationOptions, useQueryClient } from '@tanstack/react-query'; | ||
|
||
import { showErrorMessage } from '@/ui'; | ||
|
||
import { client, parseAxiosError, useBaseMutation } from '../common'; | ||
import type { FavoriteProduct } from './types'; | ||
|
||
export const addFavorite = async (id: number): Promise<FavoriteProduct> => { | ||
try { | ||
const { data } = await client.post(`/products/${id}/favorite`, {}); | ||
return data; | ||
} catch (error) { | ||
throw parseAxiosError(error); | ||
} | ||
}; | ||
|
||
export const useAddFavorite = ( | ||
props: UseMutationOptions<FavoriteProduct, string, number, any> | ||
) => { | ||
const queryClient = useQueryClient(); | ||
|
||
return useBaseMutation<number, FavoriteProduct>({ | ||
mutationKey: ['addFavorite'], | ||
mutationFn: addFavorite, | ||
onSuccess: (data, variables, context) => { | ||
queryClient.invalidateQueries({ queryKey: ['products'] }); | ||
props?.onSuccess?.(data, variables, context); | ||
}, | ||
onError: (error, variables, context) => { | ||
showErrorMessage('Something went wrong'); | ||
props?.onError?.(error, variables, context); | ||
}, | ||
...props, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { type UseMutationOptions, useQueryClient } from '@tanstack/react-query'; | ||
|
||
import { showErrorMessage } from '@/ui'; | ||
|
||
import { client } from '../common'; | ||
import { parseAxiosError, useBaseMutation } from '../common/utils'; | ||
|
||
export const removeFavorite = async (id: number): Promise<null> => { | ||
try { | ||
const { data } = await client.delete(`/products/${id}/favorite`); | ||
return data; | ||
} catch (error) { | ||
throw parseAxiosError(error); | ||
} | ||
}; | ||
|
||
export const useRemoveFavorite = ( | ||
props: UseMutationOptions<null, string, number, any> | ||
) => { | ||
const queryClient = useQueryClient(); | ||
|
||
return useBaseMutation<number, null>({ | ||
mutationKey: ['removeFavorite'], | ||
mutationFn: removeFavorite, | ||
onSuccess: (data, variables, context) => { | ||
queryClient.invalidateQueries({ queryKey: ['products'] }); | ||
props?.onSuccess?.(data, variables, context); | ||
}, | ||
onError: (error, variables, context) => { | ||
showErrorMessage('Something went wrong'); | ||
props?.onError?.(error, variables, context); | ||
}, | ||
...props, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { Pagination, Product } from '../types'; | ||
|
||
export type FavoriteProduct = { | ||
id: number; | ||
product: Product; | ||
}; | ||
|
||
export type FavoriteProductsResponse = { | ||
data: FavoriteProduct[]; | ||
pagination: Pagination; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { createInfiniteQuery } from 'react-query-kit'; | ||
|
||
import { client, DEFAULT_PAGE_PARAMS } from '../common'; | ||
import { API_CONSTS, QUERY_KEYS } from '../consts'; | ||
import type { FavoriteProductsResponse } from './types'; | ||
|
||
type Variables = { | ||
items: number; | ||
}; | ||
|
||
export const useFavorites = createInfiniteQuery({ | ||
queryKey: QUERY_KEYS.FAVORITES, | ||
fetcher: async ( | ||
variables: Variables, | ||
{ pageParam = API_CONSTS.INITIAL_PAGE } | ||
): Promise<FavoriteProductsResponse> => { | ||
const { data } = await client.get<FavoriteProductsResponse>( | ||
'/products/favorites', | ||
{ | ||
params: { | ||
page: pageParam, | ||
items: variables.items, | ||
}, | ||
} | ||
); | ||
return data; | ||
}, | ||
...DEFAULT_PAGE_PARAMS, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,68 @@ | ||
import { Buttons } from '@/components/buttons'; | ||
import { Colors } from '@/components/colors'; | ||
import { Inputs } from '@/components/inputs'; | ||
import { Typography } from '@/components/typography'; | ||
import { FocusAwareStatusBar, SafeAreaView, ScrollView } from '@/ui'; | ||
import { useFocusEffect } from 'expo-router'; | ||
import { useCallback } from 'react'; | ||
import { FlatList } from 'react-native'; | ||
import { twMerge } from 'tailwind-merge'; | ||
|
||
import { API_CONSTS } from '@/api/consts'; | ||
import { useFavorites } from '@/api/favorites/use-favorites'; | ||
import { FavoriteCard } from '@/components/favourites/favorite-card'; | ||
import { HeaderLogo } from '@/components/header-logo'; | ||
import { SafeAreaView, Text, View } from '@/ui'; | ||
|
||
export default function Favorite() { | ||
const { data, hasNextPage, fetchNextPage, isFetchingNextPage, refetch } = | ||
useFavorites({ variables: { items: API_CONSTS.INITIAL_ITEMS } }); | ||
|
||
const handleLoadMore = () => { | ||
if (hasNextPage && !isFetchingNextPage) { | ||
fetchNextPage(); | ||
} | ||
}; | ||
|
||
useFocusEffect( | ||
useCallback(() => { | ||
refetch(); | ||
}, [refetch]) | ||
); | ||
|
||
const productsToDisplay = data?.pages.flatMap((page) => page.data) || []; | ||
|
||
export default function Style() { | ||
return ( | ||
<> | ||
<FocusAwareStatusBar /> | ||
<ScrollView className="px-4"> | ||
<SafeAreaView className="flex-1"> | ||
<Typography /> | ||
<Colors /> | ||
<Buttons /> | ||
<Inputs /> | ||
</SafeAreaView> | ||
</ScrollView> | ||
<SafeAreaView className="flex-1"> | ||
<View className="bg-light_background pb-52"> | ||
<HeaderLogo /> | ||
<Text className="my-4 px-4 text-lg">My favourites</Text> | ||
<FlatList | ||
className="pb-6" | ||
onEndReached={handleLoadMore} | ||
data={productsToDisplay} | ||
renderItem={({ item, index }) => { | ||
const isFirstItem = index === 0; | ||
const isLastItem = index === productsToDisplay.length - 1; | ||
|
||
return ( | ||
<View | ||
className={twMerge( | ||
'mx-4 border', | ||
isFirstItem && 'rounded-t-lg', | ||
isLastItem && 'rounded-b-lg' | ||
)} | ||
> | ||
<FavoriteCard | ||
id={item.product.id} | ||
price={item.product.unit_price} | ||
state={item.product.state} | ||
name={item.product.title} | ||
image_url={item.product.pictures[0]} | ||
/> | ||
</View> | ||
); | ||
}} | ||
keyExtractor={(item) => item.id.toString()} | ||
/> | ||
</View> | ||
</SafeAreaView> | ||
</> | ||
); | ||
} |
Oops, something went wrong.