diff --git a/features/home/actions/getRecommenationAnime.ts b/features/home/actions/Hero/getRecommenationAnime.ts similarity index 100% rename from features/home/actions/getRecommenationAnime.ts rename to features/home/actions/Hero/getRecommenationAnime.ts diff --git a/features/home/actions/Hero/removeHeroBannerMediaFromSaved.ts b/features/home/actions/Hero/removeHeroBannerMediaFromSaved.ts new file mode 100644 index 0000000..0c2c873 --- /dev/null +++ b/features/home/actions/Hero/removeHeroBannerMediaFromSaved.ts @@ -0,0 +1,21 @@ +"use server"; + +import { backendFetch } from "@/shared/helpers/backendFetch"; + +export const removeHeroBannerMediaFromSaved = async (mediaId: string) => { + try { + return await backendFetch("collections/sys", { + method: "DELETE", + body: JSON.stringify({ + name: "Saved", + itemId: mediaId, + }), + }); + } catch (error) { + console.error("Error removing media from saved list:", error); + return { + success: false, + message: "Failed to remove media from saved list.", + }; + } +}; diff --git a/features/home/sections/Hero/components/AddToList.tsx b/features/home/sections/Hero/components/AddToList.tsx index c9606ec..255a7c4 100644 --- a/features/home/sections/Hero/components/AddToList.tsx +++ b/features/home/sections/Hero/components/AddToList.tsx @@ -1,29 +1,63 @@ "use client"; import { addHeroBannerMediaToSaved } from "@/features/home/actions/Hero/addHeroBannerMediaToSaved"; +import { removeHeroBannerMediaFromSaved } from "@/features/home/actions/Hero/removeHeroBannerMediaFromSaved"; import { useAuth } from "@/shared/contexts/AuthContext"; +import { BackendResponse } from "@/shared/helpers/backendFetch"; import { Button } from "@/shared/libs/shadcn/ui/button"; import { Icon } from "@iconify/react"; +import React from "react"; -const AddToList = ({ mediaId }: { mediaId: string }) => { +const AddToList = ({ + mediaId, + isInCollection, +}: { + mediaId: string; + isInCollection: boolean; +}) => { const { session } = useAuth(); + const [isSaved, setIsSaved] = React.useState(isInCollection); const handleAddToList = async () => { - const result = await addHeroBannerMediaToSaved(mediaId); - console.log("Hasil dari fungsi server:", result); + setIsSaved(!isSaved); + const result = (await addHeroBannerMediaToSaved(mediaId).catch( + (_) => void _, + )) as BackendResponse; + if (!result || !result.success) { + setIsSaved((prev) => !prev); + } + }; + const handleRemoveFromList = async () => { + setIsSaved(!isSaved); + const result = (await removeHeroBannerMediaFromSaved(mediaId).catch( + (_) => void _, + )) as BackendResponse; + if (!result || !result.success) { + setIsSaved((prev) => !prev); + } }; return (
- {session?.user && ( - - )} + {session?.user && + (isSaved ? ( + + ) : ( + + ))}
); }; diff --git a/features/home/sections/Hero/components/Swiper.tsx b/features/home/sections/Hero/components/Swiper.tsx index 5b9ddfc..40322f5 100644 --- a/features/home/sections/Hero/components/Swiper.tsx +++ b/features/home/sections/Hero/components/Swiper.tsx @@ -19,6 +19,7 @@ export interface HeroSwiperProps { slug: string; name: string; }[]; + isInCollection: boolean; }[]; } @@ -81,7 +82,10 @@ const HeroSwiper = (props: HeroSwiperProps) => { - + diff --git a/features/home/sections/Recommendation/components/Card.tsx b/features/home/sections/Recommendation/components/Card.tsx index d4662c4..d93df64 100644 --- a/features/home/sections/Recommendation/components/Card.tsx +++ b/features/home/sections/Recommendation/components/Card.tsx @@ -1,4 +1,4 @@ -import { RecommendationAnime } from "@/features/home/actions/getRecommenationAnime"; +import { RecommendationAnime } from "@/features/home/actions/Hero/getRecommenationAnime"; import { Icon } from "@iconify/react"; const AnimeRecommendationCard = ({ data }: { data: RecommendationAnime }) => { diff --git a/features/home/sections/Recommendation/main.client.tsx b/features/home/sections/Recommendation/main.client.tsx index 3fae04f..d4871d3 100644 --- a/features/home/sections/Recommendation/main.client.tsx +++ b/features/home/sections/Recommendation/main.client.tsx @@ -1,7 +1,7 @@ "use client"; import { useRef } from "react"; -import { RecommendationAnime } from "../../actions/getRecommenationAnime"; +import { RecommendationAnime } from "../../actions/Hero/getRecommenationAnime"; import AnimeRecommendationCard from "./components/Card"; import ScrollingButton from "./components/ScrollingButton"; diff --git a/features/home/sections/Recommendation/main.tsx b/features/home/sections/Recommendation/main.tsx index e4d3229..83d7850 100644 --- a/features/home/sections/Recommendation/main.tsx +++ b/features/home/sections/Recommendation/main.tsx @@ -1,4 +1,4 @@ -import { getRecommendationAnimeAction } from "../../actions/getRecommenationAnime"; +import { getRecommendationAnimeAction } from "../../actions/Hero/getRecommenationAnime"; import RecommendationClient from "./main.client"; const RecommendationMain = async () => {