-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
100 lines (96 loc) · 2.9 KB
/
Copy pathindex.js
File metadata and controls
100 lines (96 loc) · 2.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import Vue from 'vue';
import Vuex from 'vuex';
import createPersistedState from 'vuex-persistedstate';
Vue.use(Vuex);
/**
* Answer type
* @typedef {Object} Answer
* @property {any} value - input value
* @property {boolean} done - completion state
*/
/**
* State Object
* @property {Object.<string, Answer>} answers - keys are the field IDs
* @property {boolean} printMode
* @property {string} newNotes - notes will be written to this field while editing and saved to notes when input is closed
* @property {string} notes
* @property {string} prevQuestionLabel - holds the label of the question which was presented in the notes before
*/
const initialState = {
answers: {},
printMode: false,
newNotes: '',
notes: '',
prevQuestionLabel: ''
};
export default new Vuex.Store({
state: { ...initialState },
getters: {
getFieldCompletion: state => fieldId => state.answers[fieldId]?.done,
getFieldValue: state => fieldId => state.answers[fieldId]?.value,
getSectionCompletion: state => sectionId => {
for (const key in state.answers) {
if (
key.startsWith(`${sectionId}-`) &&
state.answers[key].done !== true
) {
return false;
}
}
return true;
},
getNewNotes: state => state.newNotes,
getNotes: state => state.notes,
getPrevQuestionLabel: state => state.prevQuestionLabel
},
mutations: {
resetState(state) {
for (const key in state.answers) {
Vue.delete(state.answers, key);
}
Object.assign(state, { ...initialState });
},
importState(state, importedState) {
Object.assign(state, importedState);
},
/**
* Update the completion state of fields
* @param {*} state
* @param {Object} data
* @param {string[]} data.fieldIds - The list of the answer IDs to receive the value
* @param {boolean} data.value - The value to be set
*/
updateAnswerCompletion(state, { fieldIds, value }) {
for (const fieldId of fieldIds) {
if (!Object.hasOwnProperty.call(state.answers, fieldId)) {
Vue.set(state.answers, fieldId, {});
}
Vue.set(state.answers[fieldId], 'done', value);
}
},
updateAnswerValue(state, { fieldId, value }) {
if (!Object.hasOwnProperty.call(state.answers, fieldId)) {
Vue.set(state.answers, fieldId, {});
}
Vue.set(state.answers[fieldId], 'value', value);
},
updatePrintMode(state, newVal) {
state.printMode = newVal;
},
updateNewNotes(state, newVal) {
state.newNotes = newVal;
},
updateNotes(state, newVal) {
state.notes = newVal;
},
updatePrevQuestionLabel(state, newVal) {
state.prevQuestionLabel = newVal;
}
},
strict: process.env.NODE_ENV !== 'production',
plugins: [
createPersistedState({
filter: mutation => mutation.type !== 'updatePrintMode'
})
]
});