Free javascript Hosting


topics-data.js

Uploaded on Feb 14 2022 10:23 by gv1uydmhw

async function getTopicsData(firstRender=false) {
var [topicsData, receivedTopicsData] = [mtd, mtbd];
var element;
if(firstRender) element = createTopicPage(receivedTopicsData);
else element = document.getElementsByClassName('topics')[0]

if(firstRender){
var [allTopics, topicsTop, topicBottom] = await createPageStructure();
}else{
var allTopics, topicsTop, topicBottom;
allTopics = document.getElementsByClassName('all-topics')[0];
topicsTop = document.getElementsByClassName('topics-top')[0];
topicBottom = document.getElementsByClassName('topics-bottom')[0];

allTopics.remove();
topicsTop.remove();
topicBottom.remove();

var [allTopics, topicsTop, topicBottom] = await createPageStructure();
}

var [bitsStatus, contentKeys, bitsVisitedQuestions] = [{}, [], {}]; // topics_results: {bitId: results}


// if this is the first time the user load the lesson
if(Object.keys(topics_ids).length === 0){ // we should send the contentStarted signal everytime we refresh a page!
await contentStarted(contentId);
var currentBitsStatus = await getProgress(`${contentId}`);
var tempStats = currentBitsStatus[trackingData.bitsStatus];
Object.entries(topicsData).map( async ([topicId, bitsMapper], tidx) => {
// console.log("bitsMapper: ", bitsMapper);
if(currentBitsStatus) {
var contentBitsIds = Object.keys(bitsMapper);
var progressTopicsIds = Object.keys(tempStats);
var progressBitsIds = (Object.keys(tempStats).includes(topicId) && Object.keys(tempStats[topicId]) ) || [];

// for each bit in This topic
for( var contentBit of contentBitsIds){
// check if the user has visited this topic before
if( progressTopicsIds.includes(topicId) ){
if(!Object.keys(bitsStatus).includes(topicId)) bitsStatus[topicId] = {};
// check if user has seen this bit before.
if(progressBitsIds.includes(contentBit)){
bitsStatus[topicId][contentBit] = tempStats[topicId][contentBit];
}else{ // if there is no such bit Id
bitsStatus[topicId][contentBit] = BitStatus.notStarted;
}
}else{ // if user never seen this topic before
bitsStatus[topicId] = {};
bitsStatus[topicId][contentBit] = BitStatus.notStarted;
}
}
await setProgress(`${contentId}`, {'bitsStatus': bitsStatus})
}
else Object.keys(bitsMapper).map( (bId) => bitsStatus[topicId] = { ...bitsStatus[topicId], [bId]: BitStatus.notStarted } );

var topic = receivedTopicsData.topics.find(e => e.id === topicId);

topic && 'practice' in topic && topicsTop.appendChild(createTopicDiv(topic, bitsMapper, bitsStatus, topicsData));

if( tidx === Object.keys(topicsData).length - 1) addTopicsToBody(element, topicsTop, topicBottom, allTopics);
});
// if user started the content, and we didnt find any data saved, then post something
if(!currentBitsStatus) await setProgress(`${contentId}`, {'bitsStatus': bitsStatus})
}else{
let currentState = await getProgress(`${contentId}`);
currentState = currentState[trackingData.bitsStatus];

Object.entries(topicsData).map(([topicId, bitsMapper], tidx) => {
var topic = receivedTopicsData.topics.find(e => e.id === topicId);
topic && 'practice' in topic && topicsTop.appendChild(createTopicDiv(topic, bitsMapper, currentState, topicsData));
if(tidx === Object.keys(topicsData).length - 1) addTopicsToBody(element, topicsTop, topicBottom, allTopics);
});
}
}

function addTopicsToBody(element, topicsTop, topicBottom, allTopics){
allTopics.appendChild(topicsTop);
allTopics.appendChild(topicBottom);
element.appendChild(allTopics)
document.getElementById('main').style.display = 'none';
document.getElementById('header').style.display = 'none';
document.getElementById('topics-section').prepend(element);
}

async function createPageStructure(){
var allTopics, topicsTop, topicBottom;
allTopics = document.createElement('div');
allTopics.setAttribute('class', 'all-topics');
topicsTop = document.createElement('div');
topicsTop.setAttribute('class', 'topics-top');
topicBottom = document.createElement('div');
topicBottom.setAttribute('class', 'topics-bottom');
return [allTopics, topicsTop, topicBottom];
}

Back to list