Resource progress functionality

pull/3907/head
Kamran Ahmed 2 years ago
parent f1973f63c2
commit 2cf94f981b
  1. 20
      src/components/TopicDetail/TopicDetail.tsx
  2. 31
      src/lib/resource-progress.ts

@ -65,11 +65,7 @@ export function TopicDetail() {
.then(() => { .then(() => {
setProgress(progress); setProgress(progress);
setIsActive(false); setIsActive(false);
renderTopicProgress( renderTopicProgress(topicId, progress);
topicId,
progress === 'done',
progress === 'learning'
);
}) })
.catch((err) => { .catch((err) => {
alert(err.message); alert(err.message);
@ -117,20 +113,22 @@ export function TopicDetail() {
// Toggle the topic status // Toggle the topic status
isTopicDone({ topicId, resourceId, resourceType }) isTopicDone({ topicId, resourceId, resourceType })
.then((oldIsDone) => { .then((oldIsDone) =>
return updateResourceProgressApi( updateResourceProgressApi(
{ {
topicId, topicId,
resourceId, resourceId,
resourceType, resourceType,
}, },
oldIsDone ? 'pending' : 'done' 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) => { .catch((err) => {
alert(err.message); alert(err.message);
console.error(err); console.error(err);

@ -1,5 +1,5 @@
import { httpGet, httpPost } from './http';
import Cookies from 'js-cookie'; import Cookies from 'js-cookie';
import { httpGet, httpPost } from './http';
import { TOKEN_COOKIE_NAME } from './jwt'; import { TOKEN_COOKIE_NAME } from './jwt';
import Element = astroHTML.JSX.Element; import Element = astroHTML.JSX.Element;
@ -14,13 +14,10 @@ type TopicMeta = {
export async function isTopicDone(topic: TopicMeta): Promise<boolean> { export async function isTopicDone(topic: TopicMeta): Promise<boolean> {
const { topicId, resourceType, resourceId } = topic; const { topicId, resourceType, resourceId } = topic;
const progressResult = await getResourceProgress(resourceType, resourceId); const { done = [] } =
(await getResourceProgress(resourceType, resourceId)) || {};
if (!progressResult.done) {
return false;
}
return progressResult.done.includes(topicId); return done?.includes(topicId);
} }
export async function getTopicStatus( export async function getTopicStatus(
@ -29,11 +26,11 @@ export async function getTopicStatus(
const { topicId, resourceType, resourceId } = topic; const { topicId, resourceType, resourceId } = topic;
const progressResult = await getResourceProgress(resourceType, resourceId); const progressResult = await getResourceProgress(resourceType, resourceId);
if (progressResult.done.includes(topicId)) { if (progressResult?.done.includes(topicId)) {
return 'done'; return 'done';
} }
if (progressResult.learning.includes(topicId)) { if (progressResult?.learning.includes(topicId)) {
return 'learning'; return 'learning';
} }
@ -152,9 +149,10 @@ export function setResourceProgress(
export function renderTopicProgress( export function renderTopicProgress(
topicId: string, topicId: string,
isDone: boolean, topicProgress: ResourceProgressType
isLearning: boolean
) { ) {
const isLearning = topicProgress === 'learning';
const isDone = topicProgress === 'done';
const matchingElements: Element[] = []; const matchingElements: Element[] = [];
// Elements having sort order in the beginning of the group id // Elements having sort order in the beginning of the group id
@ -202,13 +200,14 @@ export async function renderResourceProgress(
resourceType: ResourceType, resourceType: ResourceType,
resourceId: string resourceId: string
) { ) {
const progress = await getResourceProgress(resourceType, resourceId); const { done = [], learning = [] } =
(await getResourceProgress(resourceType, resourceId)) || {};
progress.done.forEach((topicId) => { done.forEach((topicId) => {
renderTopicProgress(topicId, true, false); renderTopicProgress(topicId, 'done');
}); });
progress.learning.forEach((topicId) => { learning.forEach((topicId) => {
renderTopicProgress(topicId, false, true); renderTopicProgress(topicId, 'learning');
}); });
} }

Loading…
Cancel
Save