adjustments do mongoDB output

This commit is contained in:
Delucse
2020-11-27 17:21:42 +01:00
parent 28720edeea
commit a2c571c1e7
9 changed files with 36 additions and 35 deletions
+20 -19
View File
@@ -5,9 +5,9 @@ import { returnErrors, returnSuccess } from './messageActions';
export const getTutorial = (id) => (dispatch, getState) => {
dispatch({type: TUTORIAL_PROGRESS});
axios.get(`https://api.blockly.sensebox.de/tutorial/${id}`)
axios.get(`${process.env.REACT_APP_BLOCKLY_API}/tutorial/${id}`)
.then(res => {
var tutorial = res.data;
var tutorial = res.data.tutorial;
existingTutorial(tutorial, getState().tutorial.status).then(status => {
dispatch({
type: TUTORIAL_SUCCESS,
@@ -30,9 +30,10 @@ export const getTutorial = (id) => (dispatch, getState) => {
export const getTutorials = () => (dispatch, getState) => {
dispatch({type: TUTORIAL_PROGRESS});
axios.get(`https://api.blockly.sensebox.de/tutorial`)
axios.get(`${process.env.REACT_APP_BLOCKLY_API}/tutorial`)
.then(res => {
var tutorials = res.data;
var tutorials = res.data.tutorials;
console.log(tutorials);
existingTutorials(tutorials, getState().tutorial.status).then(status => {
dispatch({
type: TUTORIAL_SUCCESS,
@@ -72,9 +73,9 @@ export const tutorialChange = () => (dispatch) => {
export const tutorialCheck = (status, step) => (dispatch, getState) => {
var tutorialsStatus = getState().tutorial.status;
var id = getState().tutorial.tutorials[0].id;
var tutorialsStatusIndex = tutorialsStatus.findIndex(tutorialStatus => tutorialStatus.id === id);
var tasksIndex = tutorialsStatus[tutorialsStatusIndex].tasks.findIndex(task => task.id === step.id);
var id = getState().tutorial.tutorials[0]._id;
var tutorialsStatusIndex = tutorialsStatus.findIndex(tutorialStatus => tutorialStatus._id === id);
var tasksIndex = tutorialsStatus[tutorialsStatusIndex].tasks.findIndex(task => task._id === step._id);
tutorialsStatus[tutorialsStatusIndex].tasks[tasksIndex] = {
...tutorialsStatus[tutorialsStatusIndex].tasks[tasksIndex],
type: status
@@ -89,13 +90,13 @@ export const tutorialCheck = (status, step) => (dispatch, getState) => {
export const storeTutorialXml = (code) => (dispatch, getState) => {
var tutorial = getState().tutorial.tutorials[0];
if (tutorial) {
var id = tutorial.id;
var id = tutorial._id;
var activeStep = getState().tutorial.activeStep;
var steps = tutorial.steps;
if (steps && steps[activeStep].type === 'task') {
var tutorialsStatus = getState().tutorial.status;
var tutorialsStatusIndex = tutorialsStatus.findIndex(tutorialStatus => tutorialStatus.id === id);
var tasksIndex = tutorialsStatus[tutorialsStatusIndex].tasks.findIndex(task => task.id === steps[activeStep].id);
var tutorialsStatusIndex = tutorialsStatus.findIndex(tutorialStatus => tutorialStatus._id === id);
var tasksIndex = tutorialsStatus[tutorialsStatusIndex].tasks.findIndex(task => task._id === steps[activeStep]._id);
tutorialsStatus[tutorialsStatusIndex].tasks[tasksIndex] = {
...tutorialsStatus[tutorialsStatusIndex].tasks[tasksIndex],
xml: code
@@ -123,38 +124,38 @@ const existingTutorials = (tutorials, status) => new Promise(function(resolve, r
existingTutorial(tutorial, status).then(status => {
newstatus = status;
});
return tutorial.id;
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);
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);
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) {
var tasksId = task._id;
if (status[statusIndex].tasks.findIndex(task => task._id === tasksId) === -1) {
// task does not exist
status[statusIndex].tasks.push({ id: tasksId });
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);
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 }; }) });
status.push({ _id: tutorialsId, tasks: tutorial.steps.filter(step => step.type === 'task').map(task => { return { _id: task._id }; }) });
}
resolve(status);
});