From 2cf94f981b33307dde16033dda1131ccf4d2ba6d Mon Sep 17 00:00:00 2001 From: Kamran Ahmed Date: Tue, 9 May 2023 02:14:27 +0100 Subject: [PATCH] Resource progress functionality --- src/components/TopicDetail/TopicDetail.tsx | 20 +++++++------- src/lib/resource-progress.ts | 31 +++++++++++----------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/src/components/TopicDetail/TopicDetail.tsx b/src/components/TopicDetail/TopicDetail.tsx index 948e97a50..71d7b0718 100644 --- a/src/components/TopicDetail/TopicDetail.tsx +++ b/src/components/TopicDetail/TopicDetail.tsx @@ -65,11 +65,7 @@ export function TopicDetail() { .then(() => { setProgress(progress); setIsActive(false); - renderTopicProgress( - topicId, - progress === 'done', - progress === 'learning' - ); + renderTopicProgress(topicId, progress); }) .catch((err) => { alert(err.message); @@ -117,20 +113,22 @@ export function TopicDetail() { // Toggle the topic status isTopicDone({ topicId, resourceId, resourceType }) - .then((oldIsDone) => { - return updateResourceProgressApi( + .then((oldIsDone) => + updateResourceProgressApi( { topicId, resourceId, resourceType, }, oldIsDone ? 'pending' : 'done' + ) + ) + .then(({ done = [] }) => { + renderTopicProgress( + topicId, + done.includes(topicId) ? 'done' : 'pending' ); }) - .then((updatedResult) => { - const newIsDone = updatedResult.done.includes(topicId); - renderTopicProgress(topicId, newIsDone, false); - }) .catch((err) => { alert(err.message); console.error(err); diff --git a/src/lib/resource-progress.ts b/src/lib/resource-progress.ts index 6199b5d78..3f4279323 100644 --- a/src/lib/resource-progress.ts +++ b/src/lib/resource-progress.ts @@ -1,5 +1,5 @@ -import { httpGet, httpPost } from './http'; import Cookies from 'js-cookie'; +import { httpGet, httpPost } from './http'; import { TOKEN_COOKIE_NAME } from './jwt'; import Element = astroHTML.JSX.Element; @@ -14,13 +14,10 @@ type TopicMeta = { export async function isTopicDone(topic: TopicMeta): Promise { const { topicId, resourceType, resourceId } = topic; - const progressResult = await getResourceProgress(resourceType, resourceId); - - if (!progressResult.done) { - return false; - } + const { done = [] } = + (await getResourceProgress(resourceType, resourceId)) || {}; - return progressResult.done.includes(topicId); + return done?.includes(topicId); } export async function getTopicStatus( @@ -29,11 +26,11 @@ export async function getTopicStatus( const { topicId, resourceType, resourceId } = topic; const progressResult = await getResourceProgress(resourceType, resourceId); - if (progressResult.done.includes(topicId)) { + if (progressResult?.done.includes(topicId)) { return 'done'; } - if (progressResult.learning.includes(topicId)) { + if (progressResult?.learning.includes(topicId)) { return 'learning'; } @@ -152,9 +149,10 @@ export function setResourceProgress( export function renderTopicProgress( topicId: string, - isDone: boolean, - isLearning: boolean + topicProgress: ResourceProgressType ) { + const isLearning = topicProgress === 'learning'; + const isDone = topicProgress === 'done'; const matchingElements: Element[] = []; // Elements having sort order in the beginning of the group id @@ -202,13 +200,14 @@ export async function renderResourceProgress( resourceType: ResourceType, resourceId: string ) { - const progress = await getResourceProgress(resourceType, resourceId); + const { done = [], learning = [] } = + (await getResourceProgress(resourceType, resourceId)) || {}; - progress.done.forEach((topicId) => { - renderTopicProgress(topicId, true, false); + done.forEach((topicId) => { + renderTopicProgress(topicId, 'done'); }); - progress.learning.forEach((topicId) => { - renderTopicProgress(topicId, false, true); + learning.forEach((topicId) => { + renderTopicProgress(topicId, 'learning'); }); }