diff --git a/frontend/src/components/CreateProgramModal.component.jsx b/frontend/src/components/CreateProgramModal.component.jsx index cc8903e..a353243 100644 --- a/frontend/src/components/CreateProgramModal.component.jsx +++ b/frontend/src/components/CreateProgramModal.component.jsx @@ -272,6 +272,16 @@ function CreateProgramModal({ isOpen, onClose }) { }, onSuccess: () => { queryClient.invalidateQueries(['training-programs']); + queryClient.invalidateQueries( + { + queryKey: ['explore-programs'] + } + ) + queryClient.invalidateQueries( + { + queryKey: ['recommended-programs'] + } + ) resetAllFields(); onClose(); }, diff --git a/frontend/src/components/PostFeed.component.jsx b/frontend/src/components/PostFeed.component.jsx index b2194e5..8294e87 100644 --- a/frontend/src/components/PostFeed.component.jsx +++ b/frontend/src/components/PostFeed.component.jsx @@ -99,7 +99,7 @@ function PostFeed() { }) } > - {user && ( + {ongoingJoinedPrograms && ongoingJoinedPrograms.length > 0 && ( Your Active Programs: diff --git a/frontend/src/components/ProgramFeedCard.component.jsx b/frontend/src/components/ProgramFeedCard.component.jsx index 34d7d9c..08dbcaa 100644 --- a/frontend/src/components/ProgramFeedCard.component.jsx +++ b/frontend/src/components/ProgramFeedCard.component.jsx @@ -34,7 +34,6 @@ import { useDisclosure } from '@chakra-ui/react'; function ProgramFeedCard({ program }) { - console.log('program', program) const { isOpen, onOpen, onClose } = useDisclosure(); const { isOpen: isFeedbackOpen, @@ -58,15 +57,23 @@ function ProgramFeedCard({ user && following && following.includes(program.trainer) ) const [isUserJoined, setIsUserJoined] = useState( - user && user.joinedPrograms && user.joinedPrograms + user && joinedPrograms .filter( - (joinedProgram) => joinedProgram.status !== 'LEFT' + (joinedProgram) => joinedProgram.status !== 'LEFT' || joinedProgram.status !== 'COMPLETED' ) .map((joinedProgram) => joinedProgram.id).includes(program.id) ) useEffect(() => { if (user && joinedPrograms) { + console.log('setIsProgramCompleted', joinedPrograms + .filter( + (joinedProgram) => joinedProgram.status === 'COMPLETED' + ) + .map((joinedProgram) => joinedProgram.id) + .includes(program.id), program.title + ) + setIsProgramCompleted( joinedPrograms .filter( @@ -85,17 +92,25 @@ function ProgramFeedCard({ }, [user, following]) useEffect(() => { - if (user && user.joinedPrograms) { + if (user && joinedPrograms) { + console.log('setIsUserJoined', joinedPrograms + .filter( + (joinedProgram) => joinedProgram.status == "ONGOING" + ) + .map((joinedProgram) => joinedProgram.id) + .includes(program.id), program.title + ) + setIsUserJoined( - user.joinedPrograms + joinedPrograms .filter( - (joinedProgram) => joinedProgram.status !== 'LEFT' + (joinedProgram) => joinedProgram.status == "ONGOING" ) .map((joinedProgram) => joinedProgram.id) .includes(program.id) ) } - }, [user]) + }, [user, joinedPrograms]) // Follow a user Mutation const { mutate: followUser } = useMutation( @@ -204,6 +219,16 @@ function ProgramFeedCard({ queryClient.invalidateQueries({ queryKey: ['joinedPrograms'] }) + queryClient.invalidateQueries( + { + queryKey: ['explore-programs'] + } + ) + queryClient.invalidateQueries( + { + queryKey: ['recommended-programs'] + } + ) }, onError: (error) => { console.log(error) @@ -253,6 +278,16 @@ function ProgramFeedCard({ queryClient.invalidateQueries({ queryKey: ['joinedPrograms'] }) + queryClient.invalidateQueries( + { + queryKey: ['explore-programs'] + } + ) + queryClient.invalidateQueries( + { + queryKey: ['recommended-programs'] + } + ) }, onError: (error) => { console.log(error) diff --git a/frontend/src/components/Training.component.jsx b/frontend/src/components/Training.component.jsx index b6183d8..59b6a41 100644 --- a/frontend/src/components/Training.component.jsx +++ b/frontend/src/components/Training.component.jsx @@ -206,20 +206,28 @@ const TrainingCard = () => { } if (user) { + try { const joinedProgramsResponse = await apiInstance(sessionToken).get( `/api/training-programs/ongoing/${programID}` ); const joinedProgram = joinedProgramsResponse.data; - setIsUserJoined(joinedProgram ? true : false); + const userJoinedBool = joinedProgram && joinedProgram.status === 'ONGOING'; + + setIsUserJoined(userJoinedBool); + + if (userJoinedBool) { + setError(null); + setTrainingProgram(joinedProgram); + setprogressValue(parseInt(joinedProgram.completionPercentage)); + return; + } + } catch (error) { + setTrainingProgram(data); + setprogressValue(0); setError(null); - setTrainingProgram(joinedProgram); - setprogressValue(parseInt(joinedProgram.completionPercentage)); - return; } - - setTrainingProgram(data); - setError(null); + } } catch (error) { console.error('Error fetching training program:', error); toast({