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) => {
try {
// 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 there is no user progress record for this user, create a new one
const newUserProgress = new UserProgress({
userProgress = new UserProgress({
userId,
curriculums: [curriculum],
});
await newUserProgress.save();
} else {
// If there is an existing user progress record, check if the curriculum already exists
const existingCurriculumIndex = userProgress.curriculums.findIndex(c => c.curriculumCode === curriculum.curriculumCode);
......
......@@ -47,6 +47,7 @@ const allColumns = [
const List = () => {
const dispatch = useDispatch();
const { curriculums, error, success, isLoading } = useSelector(state => state.curriculum);
const { success: userProgressSuccess } = useSelector(state => state.userProgress);
const [data, setData] = useState<curriculumType[]>([])
......@@ -91,7 +92,7 @@ const List = () => {
*/
useEffect(() => {
dispatch(fetchCurriculums());
}, [dispatch]);
}, [dispatch, userProgressSuccess]);
useEffect(() => {
setData(curriculums);
......
......@@ -15,7 +15,7 @@ import {
import MainCard from 'components/MainCard';
// assets
import { PlusOutlined, SendOutlined } from '@ant-design/icons';
import { MinusOutlined, PlusOutlined, SendOutlined } from '@ant-design/icons';
import AnimateButton from 'components/@extended/AnimateButton';
import useAuth from 'hooks/useAuth';
import { useDispatch, useSelector } from 'store';
......@@ -46,6 +46,7 @@ const CurriculumCard = ({ curriculum }: { curriculum: curriculumType }) => {
const [desc, setDesc] = useState(curriculum.curriculumDescription?.slice(0, 100))
const [readMore, setReadMore] = useState(false)
const [isSubscribed, setIsSubscribed] = useState(false)
/**
* API Config
......@@ -135,6 +136,20 @@ const CurriculumCard = ({ curriculum }: { curriculum: curriculumType }) => {
}
}, [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 (
<>
<Animation
......@@ -168,27 +183,33 @@ const CurriculumCard = ({ curriculum }: { curriculum: curriculumType }) => {
</Typography>
</Grid>
<Grid item xs={6}>
<Box sx={{ display: 'inline-block' }}>
<Box sx={{ display: 'inline-block', width: "100%" }}>
<AnimateButton>
<Button
fullWidth
size='small'
variant="outlined"
endIcon={<PlusOutlined />}
endIcon={isSubscribed ? <MinusOutlined /> : <PlusOutlined />}
sx={{ my: 2, width: "100%" }}
onClick={FollowCurriculum}
color='success'
onClick={() => {
if (!isSubscribed) {
FollowCurriculum()
}
}}
color={isSubscribed ? 'secondary' : 'success'}
disabled={isLoading}
>
{isLoading ? <CircularProgress /> : 'Follow Curriculum'}
{isLoading ? <CircularProgress /> : <>{isSubscribed ? 'Un Follow Curriculum' : 'Follow Curriculum'} </>}
</Button>
</AnimateButton>
</Box>
</Grid>
<Grid item xs={6}>
<Box sx={{ display: 'inline-block' }}>
<Box sx={{ display: 'inline-block', width: "100%" }}>
<AnimateButton>
<Button
fullWidth
size='small'
variant="outlined"
endIcon={<SendOutlined />}
sx={{ my: 2, width: "100%" }}
......
......@@ -14,7 +14,7 @@ export interface curriculumType {
createdAt?: Date
updatedBy?: string
updatedAt?: Date
subscribedUser?: any[]
subscribedUser?: string[]
}
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