updated status reading
This commit is contained in:
@@ -3,14 +3,21 @@ import { TUTORIAL_PROGRESS, GET_TUTORIAL, GET_TUTORIALS, TUTORIAL_SUCCESS, TUTOR
|
||||
import axios from 'axios';
|
||||
import { returnErrors, returnSuccess } from './messageActions';
|
||||
|
||||
export const getTutorial = (id) => (dispatch) => {
|
||||
export const getTutorial = (id) => (dispatch, getState) => {
|
||||
dispatch({type: TUTORIAL_PROGRESS});
|
||||
axios.get(`https://api.blockly.sensebox.de/tutorial/${id}`)
|
||||
.then(res => {
|
||||
dispatch({type: TUTORIAL_PROGRESS});
|
||||
dispatch({
|
||||
type: GET_TUTORIAL,
|
||||
payload: res.data
|
||||
var tutorial = res.data;
|
||||
existingTutorial(tutorial, getState().tutorial.status).then(status => {
|
||||
dispatch({
|
||||
type: TUTORIAL_SUCCESS,
|
||||
payload: status
|
||||
});
|
||||
dispatch({type: TUTORIAL_PROGRESS});
|
||||
dispatch({
|
||||
type: GET_TUTORIAL,
|
||||
payload: tutorial
|
||||
});
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
@@ -21,14 +28,21 @@ export const getTutorial = (id) => (dispatch) => {
|
||||
});
|
||||
};
|
||||
|
||||
export const getTutorials = () => (dispatch) => {
|
||||
export const getTutorials = () => (dispatch, getState) => {
|
||||
dispatch({type: TUTORIAL_PROGRESS});
|
||||
axios.get(`https://api.blockly.sensebox.de/tutorial`)
|
||||
.then(res => {
|
||||
dispatch({type: TUTORIAL_PROGRESS});
|
||||
dispatch({
|
||||
type: GET_TUTORIALS,
|
||||
payload: res.data
|
||||
var tutorials = res.data;
|
||||
existingTutorials(tutorials, getState().tutorial.status).then(status => {
|
||||
dispatch({
|
||||
type: TUTORIAL_SUCCESS,
|
||||
payload: status
|
||||
});
|
||||
dispatch({
|
||||
type: GET_TUTORIALS,
|
||||
payload: tutorials
|
||||
});
|
||||
dispatch({type: TUTORIAL_PROGRESS});
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
@@ -44,6 +58,10 @@ export const resetTutorial = () => (dispatch) => {
|
||||
type: GET_TUTORIALS,
|
||||
payload: []
|
||||
});
|
||||
dispatch({
|
||||
type: TUTORIAL_STEP,
|
||||
payload: 0
|
||||
});
|
||||
};
|
||||
|
||||
export const tutorialChange = () => (dispatch) => {
|
||||
@@ -96,3 +114,47 @@ export const tutorialStep = (step) => (dispatch) => {
|
||||
payload: step
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
const existingTutorials = (tutorials, status) => new Promise(function(resolve, reject){
|
||||
var newstatus;
|
||||
new Promise(function(resolve, reject){
|
||||
var existingTutorialIds = tutorials.map((tutorial, i) => {
|
||||
existingTutorial(tutorial, status).then(status => {
|
||||
newstatus = status;
|
||||
});
|
||||
return tutorial.id;
|
||||
});
|
||||
resolve(existingTutorialIds)
|
||||
}).then(existingTutorialIds => {
|
||||
// deleting old tutorials which do not longer exist
|
||||
if (existingTutorialIds.length > 0) {
|
||||
status = newstatus.filter(status => existingTutorialIds.indexOf(status.id) > -1);
|
||||
}
|
||||
resolve(status);
|
||||
});
|
||||
});
|
||||
|
||||
const existingTutorial = (tutorial, status) => new Promise(function(resolve, reject){
|
||||
var tutorialsId = tutorial.id;
|
||||
var statusIndex = status.findIndex(status => status.id === tutorialsId);
|
||||
if (statusIndex > -1) {
|
||||
var tasks = tutorial.steps.filter(step => step.type === 'task');
|
||||
var existingTaskIds = tasks.map((task, j) => {
|
||||
var tasksId = task.id;
|
||||
if (status[statusIndex].tasks.findIndex(task => task.id === tasksId) === -1) {
|
||||
// task does not exist
|
||||
status[statusIndex].tasks.push({ id: tasksId });
|
||||
}
|
||||
return tasksId;
|
||||
});
|
||||
// deleting old tasks which do not longer exist
|
||||
if (existingTaskIds.length > 0) {
|
||||
status[statusIndex].tasks = status[statusIndex].tasks.filter(task => existingTaskIds.indexOf(task.id) > -1);
|
||||
}
|
||||
}
|
||||
else {
|
||||
status.push({ id: tutorialsId, tasks: tutorial.steps.filter(step => step.type === 'task').map(task => { return { id: task.id }; }) });
|
||||
}
|
||||
resolve(status);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user