import { httpGet } from '../../lib/http'; import { useEffect, useState } from 'react'; import { pageProgressMessage } from '../../stores/page'; import type { UserProgressResponse } from '../HeroSection/FavoriteRoadmaps'; import { SelectionButton } from './SelectionButton'; type RoadmapSelectProps = { selectedRoadmaps: string[]; setSelectedRoadmaps: (updatedRoadmaps: string[]) => void; }; export function RoadmapSelect(props: RoadmapSelectProps) { const { selectedRoadmaps, setSelectedRoadmaps } = props; const [progressList, setProgressList] = useState(); const fetchProgress = async () => { const { response, error } = await httpGet( `${import.meta.env.PUBLIC_API_URL}/v1-get-user-all-progress` ); if (error || !response) { return; } setProgressList(response); }; useEffect(() => { fetchProgress().finally(() => { pageProgressMessage.set(''); }); }, []); const canSelectMore = selectedRoadmaps.length < 4; const allProgress = progressList?.filter((progress) => progress.resourceType === 'roadmap') || []; return (
{allProgress?.length === 0 && (

No progress tracked so far.

)} {allProgress?.map((progress) => { const isSelected = selectedRoadmaps.includes(progress.resourceId); const canSelect = isSelected || canSelectMore; return ( { if (isSelected) { setSelectedRoadmaps( selectedRoadmaps.filter( (roadmap) => roadmap !== progress.resourceId ) ); } else if (selectedRoadmaps.length < 4) { setSelectedRoadmaps([...selectedRoadmaps, progress.resourceId]); } }} /> ); })}
); }