Commit 2d1af9bb authored by janithgamage1.ed's avatar janithgamage1.ed

fix: update

Desc : update project
parent e3dcca4e
...@@ -6,15 +6,14 @@ export const subscribeCurriculum = async (req, res) => { ...@@ -6,15 +6,14 @@ export const subscribeCurriculum = async (req, res) => {
try { try {
// Check if the user is already subscribed to the curriculum // Check if the user is already subscribed to the curriculum
const userProgress = await UserProgress.findOne({ userId }); let userProgress = await UserProgress.findOne({ userId });
if (!userProgress) { if (!userProgress) {
// If there is no user progress record for this user, create a new one // If there is no user progress record for this user, create a new one
const newUserProgress = new UserProgress({ userProgress = new UserProgress({
userId, userId,
curriculums: [curriculum], curriculums: [curriculum],
}); });
await newUserProgress.save();
} else { } else {
// If there is an existing user progress record, check if the curriculum already exists // If there is an existing user progress record, check if the curriculum already exists
const existingCurriculumIndex = userProgress.curriculums.findIndex(c => c.curriculumCode === curriculum.curriculumCode); const existingCurriculumIndex = userProgress.curriculums.findIndex(c => c.curriculumCode === curriculum.curriculumCode);
...@@ -37,7 +36,7 @@ export const subscribeCurriculum = async (req, res) => { ...@@ -37,7 +36,7 @@ export const subscribeCurriculum = async (req, res) => {
userProgress.totalCurriculumsMarks = totalCurriculumsMarks; userProgress.totalCurriculumsMarks = totalCurriculumsMarks;
const curriculumCode = curriculum.curriculumCode const curriculumCode = curriculum.curriculumCode
// Now, update the curriculum collection with the subscribed user // Now, update the curriculum collection with the subscribed user
await Curriculum.updateOne( await Curriculum.updateOne(
{ curriculumCode }, { curriculumCode },
......
...@@ -47,6 +47,7 @@ const allColumns = [ ...@@ -47,6 +47,7 @@ const allColumns = [
const List = () => { const List = () => {
const dispatch = useDispatch(); const dispatch = useDispatch();
const { curriculums, error, success, isLoading } = useSelector(state => state.curriculum); const { curriculums, error, success, isLoading } = useSelector(state => state.curriculum);
const { success: userProgressSuccess } = useSelector(state => state.userProgress);
const [data, setData] = useState<curriculumType[]>([]) const [data, setData] = useState<curriculumType[]>([])
...@@ -83,7 +84,7 @@ const List = () => { ...@@ -83,7 +84,7 @@ const List = () => {
setPage(p); setPage(p);
_DATA.jump(p); _DATA.jump(p);
}; };
/** /**
* API Config * API Config
...@@ -91,7 +92,7 @@ const List = () => { ...@@ -91,7 +92,7 @@ const List = () => {
*/ */
useEffect(() => { useEffect(() => {
dispatch(fetchCurriculums()); dispatch(fetchCurriculums());
}, [dispatch]); }, [dispatch, userProgressSuccess]);
useEffect(() => { useEffect(() => {
setData(curriculums); setData(curriculums);
......
...@@ -15,7 +15,7 @@ import { ...@@ -15,7 +15,7 @@ import {
import MainCard from 'components/MainCard'; import MainCard from 'components/MainCard';
// assets // assets
import { PlusOutlined, SendOutlined } from '@ant-design/icons'; import { MinusOutlined, PlusOutlined, SendOutlined } from '@ant-design/icons';
import AnimateButton from 'components/@extended/AnimateButton'; import AnimateButton from 'components/@extended/AnimateButton';
import useAuth from 'hooks/useAuth'; import useAuth from 'hooks/useAuth';
import { useDispatch, useSelector } from 'store'; import { useDispatch, useSelector } from 'store';
...@@ -46,13 +46,14 @@ const CurriculumCard = ({ curriculum }: { curriculum: curriculumType }) => { ...@@ -46,13 +46,14 @@ const CurriculumCard = ({ curriculum }: { curriculum: curriculumType }) => {
const [desc, setDesc] = useState(curriculum.curriculumDescription?.slice(0, 100)) const [desc, setDesc] = useState(curriculum.curriculumDescription?.slice(0, 100))
const [readMore, setReadMore] = useState(false) const [readMore, setReadMore] = useState(false)
const [isSubscribed, setIsSubscribed] = useState(false)
/** /**
* API Config * API Config
* User Progress API * User Progress API
*/ */
const FollowCurriculum = () => { const FollowCurriculum = () => {
if (!user) { if (!user) {
// User is missing // User is missing
dispatch( dispatch(
...@@ -97,7 +98,7 @@ const CurriculumCard = ({ curriculum }: { curriculum: curriculumType }) => { ...@@ -97,7 +98,7 @@ const CurriculumCard = ({ curriculum }: { curriculum: curriculumType }) => {
dispatch(addUserProgress(user.id, curriculum)); dispatch(addUserProgress(user.id, curriculum));
} }
} }
// handel error // handel error
useEffect(() => { useEffect(() => {
if (error != null) { if (error != null) {
...@@ -135,6 +136,20 @@ const CurriculumCard = ({ curriculum }: { curriculum: curriculumType }) => { ...@@ -135,6 +136,20 @@ const CurriculumCard = ({ curriculum }: { curriculum: curriculumType }) => {
} }
}, [success]) }, [success])
useEffect(() => {
if (curriculum.subscribedUser) {
const userId = user?.id; // Assuming user.id is how you get the user's ID
const isUserSubscribed = curriculum.subscribedUser.includes(userId!);
if (isUserSubscribed) {
setIsSubscribed(true)
}
}
}, [curriculum, user]);
return ( return (
<> <>
<Animation <Animation
...@@ -168,27 +183,33 @@ const CurriculumCard = ({ curriculum }: { curriculum: curriculumType }) => { ...@@ -168,27 +183,33 @@ const CurriculumCard = ({ curriculum }: { curriculum: curriculumType }) => {
</Typography> </Typography>
</Grid> </Grid>
<Grid item xs={6}> <Grid item xs={6}>
<Box sx={{ display: 'inline-block' }}> <Box sx={{ display: 'inline-block', width: "100%" }}>
<AnimateButton> <AnimateButton>
<Button <Button
fullWidth fullWidth
size='small'
variant="outlined" variant="outlined"
endIcon={<PlusOutlined />} endIcon={isSubscribed ? <MinusOutlined /> : <PlusOutlined />}
sx={{ my: 2, width: "100%" }} sx={{ my: 2, width: "100%" }}
onClick={FollowCurriculum} onClick={() => {
color='success' if (!isSubscribed) {
FollowCurriculum()
}
}}
color={isSubscribed ? 'secondary' : 'success'}
disabled={isLoading} disabled={isLoading}
> >
{isLoading ? <CircularProgress /> : 'Follow Curriculum'} {isLoading ? <CircularProgress /> : <>{isSubscribed ? 'Un Follow Curriculum' : 'Follow Curriculum'} </>}
</Button> </Button>
</AnimateButton> </AnimateButton>
</Box> </Box>
</Grid> </Grid>
<Grid item xs={6}> <Grid item xs={6}>
<Box sx={{ display: 'inline-block' }}> <Box sx={{ display: 'inline-block', width: "100%" }}>
<AnimateButton> <AnimateButton>
<Button <Button
fullWidth fullWidth
size='small'
variant="outlined" variant="outlined"
endIcon={<SendOutlined />} endIcon={<SendOutlined />}
sx={{ my: 2, width: "100%" }} sx={{ my: 2, width: "100%" }}
......
...@@ -14,7 +14,7 @@ export interface curriculumType { ...@@ -14,7 +14,7 @@ export interface curriculumType {
createdAt?: Date createdAt?: Date
updatedBy?: string updatedBy?: string
updatedAt?: Date updatedAt?: Date
subscribedUser?: any[] subscribedUser?: string[]
} }
export type Curriculum = { export type Curriculum = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment