|
|
@ -4,7 +4,7 @@ import { TOKEN_COOKIE_NAME } from './jwt'; |
|
|
|
import Element = astroHTML.JSX.Element; |
|
|
|
import Element = astroHTML.JSX.Element; |
|
|
|
|
|
|
|
|
|
|
|
export type ResourceType = 'roadmap' | 'best-practice'; |
|
|
|
export type ResourceType = 'roadmap' | 'best-practice'; |
|
|
|
export type ResourceProgressType = 'done' | 'learning' | 'pending'; |
|
|
|
export type ResourceProgressType = 'done' | 'learning' | 'pending' | 'skipped'; |
|
|
|
|
|
|
|
|
|
|
|
type TopicMeta = { |
|
|
|
type TopicMeta = { |
|
|
|
topicId: string; |
|
|
|
topicId: string; |
|
|
@ -26,14 +26,18 @@ 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'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (progressResult?.skipped?.includes(topicId)) { |
|
|
|
|
|
|
|
return 'skipped'; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return 'pending'; |
|
|
|
return 'pending'; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -46,6 +50,7 @@ export async function updateResourceProgress( |
|
|
|
const { response, error } = await httpPost<{ |
|
|
|
const { response, error } = await httpPost<{ |
|
|
|
done: string[]; |
|
|
|
done: string[]; |
|
|
|
learning: string[]; |
|
|
|
learning: string[]; |
|
|
|
|
|
|
|
skipped: string[]; |
|
|
|
}>(`${import.meta.env.PUBLIC_API_URL}/v1-update-resource-progress`, { |
|
|
|
}>(`${import.meta.env.PUBLIC_API_URL}/v1-update-resource-progress`, { |
|
|
|
topicId, |
|
|
|
topicId, |
|
|
|
resourceType, |
|
|
|
resourceType, |
|
|
@ -61,20 +66,23 @@ export async function updateResourceProgress( |
|
|
|
resourceType, |
|
|
|
resourceType, |
|
|
|
resourceId, |
|
|
|
resourceId, |
|
|
|
response.done, |
|
|
|
response.done, |
|
|
|
response.learning |
|
|
|
response.learning, |
|
|
|
|
|
|
|
response.skipped, |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
return response; |
|
|
|
return response; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function getResourceProgress( |
|
|
|
export async function getResourceProgress( |
|
|
|
resourceType: 'roadmap' | 'best-practice', |
|
|
|
resourceType: 'roadmap' | 'best-practice', |
|
|
|
resourceId: string |
|
|
|
resourceId: string |
|
|
|
): Promise<{ done: string[]; learning: string[] }> { |
|
|
|
): Promise<{ done: string[]; learning: string[], skipped: string[] }> { |
|
|
|
// No need to load progress if user is not logged in
|
|
|
|
// No need to load progress if user is not logged in
|
|
|
|
if (!Cookies.get(TOKEN_COOKIE_NAME)) { |
|
|
|
if (!Cookies.get(TOKEN_COOKIE_NAME)) { |
|
|
|
return { |
|
|
|
return { |
|
|
|
done: [], |
|
|
|
done: [], |
|
|
|
learning: [], |
|
|
|
learning: [], |
|
|
|
|
|
|
|
skipped: [], |
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -101,31 +109,27 @@ async function loadFreshProgress( |
|
|
|
const { response, error } = await httpGet<{ |
|
|
|
const { response, error } = await httpGet<{ |
|
|
|
done: string[]; |
|
|
|
done: string[]; |
|
|
|
learning: string[]; |
|
|
|
learning: string[]; |
|
|
|
|
|
|
|
skipped: string[]; |
|
|
|
}>(`${import.meta.env.PUBLIC_API_URL}/v1-get-user-resource-progress`, { |
|
|
|
}>(`${import.meta.env.PUBLIC_API_URL}/v1-get-user-resource-progress`, { |
|
|
|
resourceType, |
|
|
|
resourceType, |
|
|
|
resourceId, |
|
|
|
resourceId, |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
if (error) { |
|
|
|
if (error || !response) { |
|
|
|
console.error(error); |
|
|
|
console.error(error); |
|
|
|
return { |
|
|
|
return { |
|
|
|
done: [], |
|
|
|
done: [], |
|
|
|
learning: [], |
|
|
|
learning: [], |
|
|
|
}; |
|
|
|
skipped: [], |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!response?.done || !response?.learning) { |
|
|
|
|
|
|
|
return { |
|
|
|
|
|
|
|
done: [], |
|
|
|
|
|
|
|
learning: [], |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
setResourceProgress( |
|
|
|
setResourceProgress( |
|
|
|
resourceType, |
|
|
|
resourceType, |
|
|
|
resourceId, |
|
|
|
resourceId, |
|
|
|
response.done, |
|
|
|
response?.done || [], |
|
|
|
response.learning |
|
|
|
response?.learning || [], |
|
|
|
|
|
|
|
response?.skipped || [], |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
return response; |
|
|
|
return response; |
|
|
@ -135,13 +139,15 @@ export function setResourceProgress( |
|
|
|
resourceType: 'roadmap' | 'best-practice', |
|
|
|
resourceType: 'roadmap' | 'best-practice', |
|
|
|
resourceId: string, |
|
|
|
resourceId: string, |
|
|
|
done: string[], |
|
|
|
done: string[], |
|
|
|
learning: string[] |
|
|
|
learning: string[], |
|
|
|
|
|
|
|
skipped: string [], |
|
|
|
): void { |
|
|
|
): void { |
|
|
|
localStorage.setItem( |
|
|
|
localStorage.setItem( |
|
|
|
`${resourceType}-${resourceId}-progress`, |
|
|
|
`${resourceType}-${resourceId}-progress`, |
|
|
|
JSON.stringify({ |
|
|
|
JSON.stringify({ |
|
|
|
done, |
|
|
|
done, |
|
|
|
learning, |
|
|
|
learning, |
|
|
|
|
|
|
|
skipped, |
|
|
|
timestamp: new Date().getTime(), |
|
|
|
timestamp: new Date().getTime(), |
|
|
|
}) |
|
|
|
}) |
|
|
|
); |
|
|
|
); |
|
|
@ -152,6 +158,7 @@ export function renderTopicProgress( |
|
|
|
topicProgress: ResourceProgressType |
|
|
|
topicProgress: ResourceProgressType |
|
|
|
) { |
|
|
|
) { |
|
|
|
const isLearning = topicProgress === 'learning'; |
|
|
|
const isLearning = topicProgress === 'learning'; |
|
|
|
|
|
|
|
const isSkipped = topicProgress === 'skipped'; |
|
|
|
const isDone = topicProgress === 'done'; |
|
|
|
const isDone = topicProgress === 'done'; |
|
|
|
const matchingElements: Element[] = []; |
|
|
|
const matchingElements: Element[] = []; |
|
|
|
|
|
|
|
|
|
|
@ -185,13 +192,15 @@ export function renderTopicProgress( |
|
|
|
matchingElements.forEach((element) => { |
|
|
|
matchingElements.forEach((element) => { |
|
|
|
if (isDone) { |
|
|
|
if (isDone) { |
|
|
|
element.classList.add('done'); |
|
|
|
element.classList.add('done'); |
|
|
|
element.classList.remove('learning'); |
|
|
|
element.classList.remove('learning', 'skipped'); |
|
|
|
} else if (isLearning) { |
|
|
|
} else if (isLearning) { |
|
|
|
element.classList.add('learning'); |
|
|
|
element.classList.add('learning'); |
|
|
|
element.classList.remove('done'); |
|
|
|
element.classList.remove('done', 'skipped'); |
|
|
|
|
|
|
|
} else if (isSkipped) { |
|
|
|
|
|
|
|
element.classList.add('skipped'); |
|
|
|
|
|
|
|
element.classList.remove('done', 'learning'); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
element.classList.remove('done'); |
|
|
|
element.classList.remove('done', 'skipped', 'learning'); |
|
|
|
element.classList.remove('learning'); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
@ -200,7 +209,7 @@ export async function renderResourceProgress( |
|
|
|
resourceType: ResourceType, |
|
|
|
resourceType: ResourceType, |
|
|
|
resourceId: string |
|
|
|
resourceId: string |
|
|
|
) { |
|
|
|
) { |
|
|
|
const { done = [], learning = [] } = |
|
|
|
const { done = [], learning = [], skipped = [] } = |
|
|
|
(await getResourceProgress(resourceType, resourceId)) || {}; |
|
|
|
(await getResourceProgress(resourceType, resourceId)) || {}; |
|
|
|
|
|
|
|
|
|
|
|
done.forEach((topicId) => { |
|
|
|
done.forEach((topicId) => { |
|
|
@ -210,4 +219,8 @@ export async function renderResourceProgress( |
|
|
|
learning.forEach((topicId) => { |
|
|
|
learning.forEach((topicId) => { |
|
|
|
renderTopicProgress(topicId, 'learning'); |
|
|
|
renderTopicProgress(topicId, 'learning'); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
skipped.forEach((topicId) => { |
|
|
|
|
|
|
|
renderTopicProgress(topicId, 'skipped'); |
|
|
|
|
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|