From 6f77b460cb81e249388c3cf6384a031ae424d73e Mon Sep 17 00:00:00 2001 From: Delucse <46593742+Delucse@users.noreply.github.com> Date: Sun, 13 Sep 2020 15:18:11 +0200 Subject: [PATCH] add/ delete tutorials/steps in initial state of redux based on tutorials.json --- src/reducers/tutorialReducer.js | 43 ++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/reducers/tutorialReducer.js b/src/reducers/tutorialReducer.js index 470b727..f966fcc 100644 --- a/src/reducers/tutorialReducer.js +++ b/src/reducers/tutorialReducer.js @@ -2,10 +2,47 @@ import { TUTORIAL_SUCCESS, TUTORIAL_ERROR, TUTORIAL_CHANGE, TUTORIAL_XML, TUTORI import tutorials from '../components/Tutorial/tutorials.json'; +const initialStatus = () => { + if(window.localStorage.getItem('status')){ + var status = JSON.parse(window.localStorage.getItem('status')); + var existingTutorialIds = []; + for(var i = 0; i < tutorials.length; i++){ + var tutorialsId = tutorials[i].id + existingTutorialIds.push(tutorialsId); + if(status.findIndex(status => status.id === tutorialsId) > -1){ + var tasks = tutorials[i].steps.filter(step => step.type === 'task'); + var existingTaskIds = []; + for(var j = 0; j < tasks.length; j++){ + var tasksId = tasks[j].id; + existingTaskIds.push(tasksId); + if(status[i].tasks.findIndex(task => task.id === tasksId) === -1){ + // task does not exist + status[i].tasks.push({id: tasksId}); + } + } + // deleting old tasks which do not longer exist + if(existingTaskIds.length > 0){ + status[i].tasks = status[i].tasks.filter(task => existingTaskIds.indexOf(task.id) > -1); + } + } + else{ + status.push({id: tutorialsId, tasks: new Array(tutorials[i].steps.filter(step => step.type === 'task').length).fill({})}); + } + } + // deleting old tutorials which do not longer exist + if(existingTutorialIds.length > 0){ + status = status.filter(status => existingTutorialIds.indexOf(status.id) > -1); + } + console.log('tutorial', existingTutorialIds); + console.log('tutorial', status); + return status; + } + // window.localStorage.getItem('status') does not exist + return tutorials.map(tutorial => {return {id: tutorial.id, tasks: tutorial.steps.filter(step => step.type === 'task').map(task => {return {id: task.id};})};}); +}; + const initialState = { - status: window.localStorage.getItem('status') ? - JSON.parse(window.localStorage.getItem('status')) - : tutorials.map(tutorial => {return {id: tutorial.id, tasks: new Array(tutorial.steps.filter(step => step.type === 'task').length).fill({}) };}), + status: initialStatus(), currentId: null, activeStep: 0, change: 0