Skip to content

Commit

Permalink
remove console logs and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sametaln committed Dec 17, 2024
1 parent 4b9a9cd commit b8f8263
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 15 deletions.
10 changes: 10 additions & 0 deletions frontend/src/components/CreateProgramModal.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/PostFeed.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function PostFeed() {
})
}
>
{user && (
{ongoingJoinedPrograms && ongoingJoinedPrograms.length > 0 && (
<Box>
<Heading size="lg" mb={4}>Your Active Programs:</Heading>
<UserJoinedProgramsCard />
Expand Down
49 changes: 42 additions & 7 deletions frontend/src/components/ProgramFeedCard.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 15 additions & 7 deletions frontend/src/components/Training.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit b8f8263

Please sign in to comment.