toggle favorite
This commit is contained in:
@@ -8,14 +8,8 @@ export const useGetFood = (id: string) => {
|
||||
});
|
||||
};
|
||||
|
||||
export const useAddToFavorite = () => {
|
||||
export const useToggleFavorite = () => {
|
||||
return useMutation({
|
||||
mutationFn: api.addToFavorite,
|
||||
});
|
||||
};
|
||||
|
||||
export const useRemoveFromFavorite = () => {
|
||||
return useMutation({
|
||||
mutationFn: api.removeFromFavorite,
|
||||
mutationFn: api.toggleFavorite,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -9,7 +9,7 @@ import { ArrowLeft, Clock, Cup, Heart, TruckTick } from 'iconsax-react'
|
||||
import Image from 'next/image'
|
||||
import { useParams, useRouter } from 'next/navigation'
|
||||
import React, { useEffect, useMemo, useState } from 'react'
|
||||
import { useAddToFavorite, useGetFood, useRemoveFromFavorite } from './hooks/useFoodData'
|
||||
import { useGetFood, useToggleFavorite } from './hooks/useFoodData'
|
||||
import { useGetAbout } from '../about/hooks/useAboutData'
|
||||
import { useGetProfile } from '../../(Profile)/profile/hooks/userProfileData'
|
||||
import { toast } from '@/components/Toast'
|
||||
@@ -26,8 +26,7 @@ function FoodPage({ }: Props) {
|
||||
const { data: about } = useGetAbout();
|
||||
const { items, addToCart, removeFromCart } = useCart();
|
||||
const [isFavorite, setIsFavorite] = useState<boolean>(false);
|
||||
const { mutate: addToFavorite } = useAddToFavorite();
|
||||
const { mutate: removeFromFavorite } = useRemoveFromFavorite();
|
||||
const { mutate: toggleFavorite } = useToggleFavorite();
|
||||
|
||||
const foodId = food?.data?.id || id as string;
|
||||
const quantity = useMemo(() => {
|
||||
@@ -63,29 +62,16 @@ function FoodPage({ }: Props) {
|
||||
const previousFavorite = isFavorite;
|
||||
setIsFavorite(!isFavorite);
|
||||
|
||||
if (previousFavorite) {
|
||||
removeFromFavorite(foodId, {
|
||||
onSuccess: () => {
|
||||
// تایید تغییر در صورت موفقیت
|
||||
},
|
||||
onError: () => {
|
||||
// بازگرداندن به حالت قبلی در صورت خطا
|
||||
setIsFavorite(previousFavorite);
|
||||
toast('خطا در حذف از علاقهمندیها', 'error');
|
||||
},
|
||||
});
|
||||
} else {
|
||||
addToFavorite(foodId, {
|
||||
onSuccess: () => {
|
||||
// تایید تغییر در صورت موفقیت
|
||||
},
|
||||
onError: () => {
|
||||
// بازگرداندن به حالت قبلی در صورت خطا
|
||||
setIsFavorite(previousFavorite);
|
||||
toast('خطا در افزودن به علاقهمندیها', 'error');
|
||||
},
|
||||
});
|
||||
}
|
||||
toggleFavorite(foodId, {
|
||||
onSuccess: () => {
|
||||
// تایید تغییر در صورت موفقیت
|
||||
},
|
||||
onError: () => {
|
||||
// بازگرداندن به حالت قبلی در صورت خطا
|
||||
setIsFavorite(previousFavorite);
|
||||
toast('خطا در تغییر وضعیت علاقهمندی', 'error');
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
@@ -6,12 +6,7 @@ export const getFood = async (id: string): Promise<FoodResponse> => {
|
||||
return data;
|
||||
};
|
||||
|
||||
export const addToFavorite = async (foodId: string) => {
|
||||
export const toggleFavorite = async (foodId: string) => {
|
||||
const { data } = await api.post(`/public/foods/favorite/${foodId}`);
|
||||
return data;
|
||||
};
|
||||
|
||||
export const removeFromFavorite = async (foodId: string) => {
|
||||
const { data } = await api.delete(`/public/foods/favorite/${foodId}`);
|
||||
return data;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user