Refactor code to be clearer

This commit is contained in:
Ramon Wenger 2019-09-09 11:07:45 +02:00
parent aeab246aee
commit 371c5cd276
2 changed files with 16 additions and 10 deletions

View File

@ -0,0 +1,11 @@
const extractAnswerFromQuestion = (previous, question) => {
return [...previous, {title: question.title, answer: question.correctAnswer}];
};
export const extractSurveySolutions = (prev, element) => {
if (!element || !element.elements) {
// element does not exist or does not have children, so just return the previous result
return prev
}
return [...prev, ...element.elements.reduce(extractAnswerFromQuestion, [])];
};

View File

@ -18,6 +18,8 @@
import UPDATE_ANSWER from '@/graphql/gql/mutations/updateAnswer.gql';
import Solution from '@/components/content-blocks/Solution';
import {extractSurveySolutions} from '@/helpers/survey-solutions';
const Survey = SurveyVue.Survey;
export default {
@ -54,14 +56,9 @@
}
},
answers() {
return this.survey.currentPage && this.survey.currentPage.elements ? this.survey.currentPage.elements.reduce((prev, element) => {
if (!element || !element.elements) {
return prev
}
return [...prev, ...element.elements.reduce((pr, question) => {
return [...pr, {title: question.title, answer: question.correctAnswer}];
}, [])];
}, []) : []
return this.survey.currentPage && this.survey.currentPage.elements
? this.survey.currentPage.elements.reduce(extractSurveySolutions, [])
: []
}
},
@ -108,8 +105,6 @@
survey.locale = 'de';
survey.showProgressBar = 'bottom';
console.log(survey);
return survey;
},
reopen() {