From 77c47e8f03bd3c555ab43e863e732b1d446d759b Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Thu, 19 Sep 2024 05:32:53 +0600 Subject: [PATCH] feat: add mark favourite button (#7156) * feat: add mark favourite button * fix: update favourite --- src/components/Roadmaps/RoadmapCard.tsx | 31 ++++++++++++++++ src/components/Roadmaps/RoadmapsPage.tsx | 45 +++++++++++++++++++----- src/hooks/use-is-mounted.ts | 13 +++++++ 3 files changed, 81 insertions(+), 8 deletions(-) create mode 100644 src/components/Roadmaps/RoadmapCard.tsx create mode 100644 src/hooks/use-is-mounted.ts diff --git a/src/components/Roadmaps/RoadmapCard.tsx b/src/components/Roadmaps/RoadmapCard.tsx new file mode 100644 index 000000000..f7e43ccaa --- /dev/null +++ b/src/components/Roadmaps/RoadmapCard.tsx @@ -0,0 +1,31 @@ +import { useIsMounted } from '../../hooks/use-is-mounted'; +import { MarkFavorite } from '../FeaturedItems/MarkFavorite'; +import type { GroupType } from './RoadmapsPage'; + +type RoadmapCardProps = { + roadmap: GroupType['roadmaps'][number]; +}; + +export function RoadmapCard(props: RoadmapCardProps) { + const { roadmap } = props; + + const isMounted = useIsMounted(); + + return ( + + {roadmap.title} + + {isMounted && ( + + )} + + ); +} diff --git a/src/components/Roadmaps/RoadmapsPage.tsx b/src/components/Roadmaps/RoadmapsPage.tsx index eb1c4896a..c13a43684 100644 --- a/src/components/Roadmaps/RoadmapsPage.tsx +++ b/src/components/Roadmaps/RoadmapsPage.tsx @@ -8,6 +8,10 @@ import { getUrlParams, setUrlParams, } from '../../lib/browser.ts'; +import { RoadmapCard } from './RoadmapCard.tsx'; +import { httpGet } from '../../lib/http.ts'; +import type { UserProgressResponse } from '../HeroSection/FavoriteRoadmaps.tsx'; +import { isLoggedIn } from '../../lib/jwt.ts'; const groupNames = [ 'Absolute Beginners', @@ -27,7 +31,7 @@ const groupNames = [ type AllowGroupNames = (typeof groupNames)[number]; -type GroupType = { +export type GroupType = { group: AllowGroupNames; roadmaps: { title: string; @@ -473,6 +477,37 @@ export function RoadmapsPage() { ]); }, [activeGroup]); + async function loadProgress() { + const { response: progressList, error } = + await httpGet( + `${import.meta.env.PUBLIC_API_URL}/v1-get-hero-roadmaps`, + ); + + if (error || !progressList) { + return; + } + + progressList?.forEach((progress) => { + window.dispatchEvent( + new CustomEvent('mark-favorite', { + detail: { + resourceId: progress.resourceId, + resourceType: progress.resourceType, + isFavorite: progress.isFavorite, + }, + }), + ); + }); + } + + useEffect(() => { + if (!isLoggedIn()) { + return; + } + + loadProgress().finally(() => {}); + }, []); + useEffect(() => { const { g } = getUrlParams() as { g: AllowGroupNames }; if (!g) { @@ -547,13 +582,7 @@ export function RoadmapsPage() {
{group.roadmaps.map((roadmap) => ( - - {roadmap.title} - + ))}
diff --git a/src/hooks/use-is-mounted.ts b/src/hooks/use-is-mounted.ts new file mode 100644 index 000000000..ee266e9b2 --- /dev/null +++ b/src/hooks/use-is-mounted.ts @@ -0,0 +1,13 @@ +import { useEffect, useState } from 'react'; + +export function useIsMounted() { + const [isMounted, setIsMounted] = useState(false); + useEffect(() => { + setIsMounted(true); + + return () => { + setIsMounted(false); + }; + }, []); + return isMounted; +}