Save user answers in surveys on page change
This commit is contained in:
parent
e940066db8
commit
64bf100a19
|
|
@ -39,6 +39,7 @@
|
|||
survey: this.initSurvey(),
|
||||
title: '',
|
||||
module: {},
|
||||
completed: false,
|
||||
me: {
|
||||
permissions: []
|
||||
},
|
||||
|
|
@ -74,19 +75,21 @@
|
|||
},
|
||||
|
||||
methods: {
|
||||
initSurvey(data, answer) {
|
||||
initSurvey(data, answers) {
|
||||
let survey = new SurveyVue.Model(data);
|
||||
const flatAnswers = {};
|
||||
for (let k in answer) {
|
||||
flatAnswers[k] = answer[k].answer;
|
||||
for (let k in answers) {
|
||||
flatAnswers[k] = answers[k].answer;
|
||||
}
|
||||
survey.data = flatAnswers;
|
||||
|
||||
survey.onComplete.add((sender, options) => {
|
||||
const saveSurvey = (sender, options) => {
|
||||
// sender.clear(false);
|
||||
//
|
||||
// sender.mode = 'display';
|
||||
|
||||
this.completed = true;
|
||||
|
||||
const data = {};
|
||||
|
||||
for (let k in survey.data) {
|
||||
|
|
@ -109,43 +112,29 @@
|
|||
}
|
||||
}
|
||||
},
|
||||
refetchQueries: [{
|
||||
query: SURVEY_QUERY,
|
||||
variables: {id: this.id}
|
||||
}],
|
||||
// fixme: make the update work instead of refetching
|
||||
// update: (store, {data: {updateAnswer: {answer}}}) => {
|
||||
// // try {
|
||||
// console.log(answer);
|
||||
// const query = SURVEY_QUERY;
|
||||
// const variables = {id: this.id};
|
||||
// const queryData = store.readQuery({query, variables});
|
||||
// console.log('queryData', queryData);
|
||||
// if (queryData.survey) {
|
||||
// console.log('answer.data', answer.data);
|
||||
// queryData.survey.answer = {
|
||||
// data: {
|
||||
// ...data,
|
||||
// __typename: 'AnswerNode'
|
||||
// }
|
||||
// };
|
||||
// store.writeQuery({query, variables, data: queryData});
|
||||
// }
|
||||
// // } catch (e) {
|
||||
// // console.error(e);
|
||||
// // // Query did not exist in the cache, and apollo throws a generic Error. Do nothing
|
||||
// // }
|
||||
// }
|
||||
update: (store, {data: {updateAnswer: {answer}}}) => {
|
||||
const query = SURVEY_QUERY;
|
||||
const variables = {id: this.id};
|
||||
const queryData = store.readQuery({query, variables});
|
||||
if (queryData.survey) {
|
||||
queryData.survey.answer = answer;
|
||||
store.writeQuery({query, variables, data: queryData});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
survey.onComplete.add(saveSurvey);
|
||||
survey.onCurrentPageChanged.add(saveSurvey);
|
||||
|
||||
survey.css = css;
|
||||
survey.locale = 'de';
|
||||
survey.showProgressBar = 'bottom';
|
||||
|
||||
return survey;
|
||||
},
|
||||
reopen() {
|
||||
this.completed = false;
|
||||
let data = this.survey.data; // save the data
|
||||
this.survey.clear();
|
||||
this.survey.data = data; // reapply it
|
||||
|
|
@ -170,7 +159,9 @@
|
|||
answer = JSON.parse(data.survey.answer.data);
|
||||
}
|
||||
|
||||
this.survey = this.initSurvey(json, answer);
|
||||
if (!this.completed) {
|
||||
this.survey = this.initSurvey(json, answer);
|
||||
}
|
||||
this.title = json.title;
|
||||
const module = data.survey.module;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue