Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honglin add lesson list edit delete buttons #3107

Draft
wants to merge 11 commits into
base: development
Choose a base branch
from
8 changes: 3 additions & 5 deletions src/actions/bmdashboard/lessonLikesActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,16 @@ export const likeLessonAction = (lessonIndex, userId) => {
});

if (response.status === 200) {
dispatch(fetchBMLessons())

dispatch(setLikes(response.data));
} else {
console.error('Unexpected response status:', response.status);
// console.error('Unexpected response status:', response.status);
}
} catch (error) {
console.error('Error liking lesson:', error);
// console.error('Error liking lesson:', error);
}
};
};


export const setLikes = payload => {
return {
type: BM_LESSON_LIKES,
Expand Down
126 changes: 80 additions & 46 deletions src/actions/bmdashboard/lessonsAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,79 @@ export const fetchBMLessons = () => {
try {
const response = await axios.get(ENDPOINTS.BM_LESSONS);
const lessons = response.data;
const authorIds = lessons.map(lesson => lesson.author);
const projectIds = lessons.map(lesson => lesson.relatedProject);
const authorIds = [...new Set(lessons.map(lesson => lesson.author))];
const projectIds = [...new Set(lessons.map(lesson => lesson.relatedProject))];

// Fetch user profiles and project details concurrently
const [projectDetails, userProfiles] = await Promise.all([
Promise.all(projectIds.map(projectId => dispatch(fetchProjectById(projectId)))),
Promise.all(authorIds.map(authorId => dispatch(getUserProfile(authorId))))
const [authorProfiles, projectDetails] = await Promise.all([
Promise.all(
authorIds.map(async authorId => {
try {
return await dispatch(getUserProfile(authorId));
} catch (error) {
console.error('Error fetching user profile:', authorId, error);
return null;
}
})
),
Promise.all(
projectIds.map(async projectId => {
try {
return await dispatch(fetchProjectById(projectId));
} catch (error) {
console.error('Error fetching project:', projectId, error);
return null;
}
})
)
]);

const updatedLessons = lessons.map((lesson, index) => {
return {
const authorMap = authorIds.reduce((acc, id, index) => {
if (authorProfiles[index]) {
acc[id] = authorProfiles[index];
}
return acc;
}, {});

const projectMap = projectIds.reduce((acc, id, index) => {
const project = projectDetails[index];
if (project && project.name) {
acc[id] = {
id,
name: project.name,
location: project.location
};
} else {
acc[id] = {
id,
name: 'Unknown Project',
location: ''
};
}
return acc;
}, {});

const updatedLessons = lessons.map(lesson => ({
...lesson,
author: userProfiles[index]
? {
id: userProfiles[index]._id,
name: `${userProfiles[index].firstName} ${userProfiles[index].lastName}`,
}
: lesson.author,
relatedProject: projectDetails[index]
? {
id: projectDetails[index]._id,
name: projectDetails[index].projectName,
}
: lesson.relatedProject,
};
});
// Dispatch an action to update the lessons with the new author and project info
author: {
id: lesson.author,
name: authorMap[lesson.author]
? `${authorMap[lesson.author].firstName} ${authorMap[lesson.author].lastName}`
: 'Unknown'
},
relatedProject: projectMap[lesson.relatedProject] || {
id: lesson.relatedProject,
name: 'Unknown Project'
}
}));
dispatch(setLessons(updatedLessons));
} catch (error) {
console.error('Error fetching lessons:', error);
dispatch(setErrors(error));
}
};
};

export const setLessons = payload => {
// console.log('Setting lessons in Redux:', payload);
return {
type: GET_BM_LESSONS,
payload
Expand Down Expand Up @@ -82,7 +119,7 @@ export const fetchSingleBMLesson = (lessonId) => {
};
dispatch(setLesson(updatedLesson));
} catch (error) {
console.error('Error fetching lesson:', error);
// console.error('Error fetching lesson:', error);
dispatch(setErrors(error));
}
};
Expand All @@ -99,7 +136,7 @@ export const updateBMLesson = (lessonId, content) => {
try {
await axios.put(url, { content });
} catch (err) {
console.log('err')
// console.log('err')
}
dispatch(updateLesson());
};
Expand All @@ -115,24 +152,21 @@ export const updateLesson = (lessonId, content) => {



export const deleteBMLesson = (lessonId) => {
return async dispatch => {
const url = ENDPOINTS.BM_LESSON + lessonId;
try {
await axios.delete(url);
} catch (err) {
console.log('err')
}
export const deleteBMLesson = (lessonId) => {
return async dispatch => {
const url = ENDPOINTS.BM_LESSON + lessonId;
try {
await axios.delete(url);
dispatch(deleteLesson(lessonId));
dispatch(fetchBMLessons())
};

}

export const deleteLesson = (lessonId) => {
return {
type: DELETE_LESSON,
lessonId
};
};
return Promise.resolve();
} catch (err) {
// console.error('Error deleting lesson:', err);
return Promise.reject(err);
}
};
};

export const deleteLesson = (lessonId) => ({
type: DELETE_LESSON,
lessonId
});
15 changes: 11 additions & 4 deletions src/actions/bmdashboard/projectByIdAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,22 @@ export const fetchProjectById = (projectId) => {
return async (dispatch) => {
try {
const response = await axios.get(url);
const projectData = response.data;
if (!response.data) {
throw new Error('No data received from project API');
}

const projectData = response.data;
dispatch(setProject(projectData));
return projectData;

return projectData;
} catch (error) {
console.error('Error fetching project:', {
projectId,
error: error.message,
response: error.response?.data
});
dispatch(setErrors(error));

throw error;
return null;
}
};
};
Expand Down
17 changes: 12 additions & 5 deletions src/components/BMDashboard/LessonList/LessonCard.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function LessonCard({ filteredLessons, onEditLessonSummary, onDeliteLessonCard,
};

const lessonCards = filteredLessons.map(lesson => {
const isLiked = lesson.likes?.includes(currentUserId);
return (
<Card key={`${lesson._id} + ${lesson.title} `} className="lesson-card">
<Card.Header
Expand Down Expand Up @@ -144,13 +145,15 @@ function LessonCard({ filteredLessons, onEditLessonSummary, onDeliteLessonCard,
</Card.Body>
<Card.Footer className=" lesson-card-footer text-muted">
<div>
<span className="footer-items-author-and-from">Author: {lesson.author.name}</span>
<span className="footer-items-author-and-from">
From: {lesson.relatedProject.name}
Author: {lesson.author?.name || 'Unknown'}
</span>
<span className="footer-items-author-and-from">
From: {lesson.relatedProject?.name || 'Unknown Project'}
</span>
</div>
<div className="lesson-card-footer-items">
{currentUserId === lesson.author.id && (
{currentUserId === lesson.author?.id && (
<div>
<button
className="text-muted"
Expand Down Expand Up @@ -184,9 +187,13 @@ function LessonCard({ filteredLessons, onEditLessonSummary, onDeliteLessonCard,
className="ml-2"
icon={faHeart}
size="sm"
style={{ color: '##7A7D81', cursor: 'pointer' }}
style={{
color: isLiked ? '#ff4d4d' : '#7A7D81',
cursor: 'pointer',
fill: isLiked ? '#ff4d4d' : 'none',
}}
/>
Like:{lesson.totalLikes}
Like: {lesson.totalLikes}
</span>
</div>
</div>
Expand Down
Loading