Skip to content

Commit 18afe42

Browse files
committed
Loading available subjects in vuex store.
1 parent d06ea03 commit 18afe42

3 files changed

Lines changed: 87 additions & 3 deletions

File tree

src/locales/en-US.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@
4747
"Title": "Title",
4848
"confidential": "confidential",
4949
"It is confidential": "It is confidential",
50-
"The note will not be displayed by default, you will have to ask to show it": "The note will not be displayed by default, you will have to ask to show it"
50+
"The note will not be displayed by default, you will have to ask to show it": "The note will not be displayed by default, you will have to ask to show it",
51+
"Subjects": "Subjects"
5152
}

src/locales/fr-FR.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@
4747
"Title": "Titre",
4848
"confidential": "confidentiel",
4949
"It is confidential": "C'est confidentiel",
50-
"The note will not be displayed by default, you will have to ask to show it": "La note ne sera pas affichée par défaut, vous devrez demander à l'afficher"
50+
"The note will not be displayed by default, you will have to ask to show it": "La note ne sera pas affichée par défaut, vous devrez demander à l'afficher",
51+
"Subjects": "Sujets"
5152
}

src/store.js

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const store = new Vuex.Store({
2525
state: {
2626
settings: DEFAULT_SETTINGS,
2727
notes: {},
28+
subjects: {},
2829
},
2930
mutations: {
3031
setNote(state, payload) {
@@ -43,6 +44,18 @@ const store = new Vuex.Store({
4344
Vue.toasted.show(i18n.t('A note has been updated (or added)'), { duration: 2000, type: 'info', icon: 'edit' });
4445
}
4546
},
47+
setSubject(state, payload) {
48+
if(typeof(payload.data) === 'undefined') {
49+
console.error('$store.mutations.setSubject : missing "data" object on payload');
50+
return;
51+
}
52+
if(typeof(payload.data.key) === 'undefined') {
53+
console.error('$store.mutations.setSubject : missing "key" value on payload.data');
54+
return;
55+
}
56+
57+
Vue.set(state.subjects, payload.data.key, payload.data.value);
58+
},
4659
deleteNote(state, payload) {
4760
if(typeof(payload.data) === 'undefined') {
4861
console.error('$store.mutations.deleteNote : missing "data" object in payload');
@@ -196,6 +209,29 @@ const store = new Vuex.Store({
196209
});
197210
});
198211
},
212+
fetchAllSubjects(context) {
213+
return new Promise((resolve, reject) => {
214+
db.query('subjects_weights/subjects_weights', {group: true, reduce: true})
215+
.then((res) => {
216+
res.rows.sort((a, b) => {
217+
if(a.value < b.value) {
218+
return +1;
219+
} else if(a.value > b.value) {
220+
return -1;
221+
} else {
222+
return 0;
223+
}
224+
});
225+
226+
res.rows.forEach((row) => {
227+
context.commit('setSubject', {data: row});
228+
});
229+
})
230+
.catch((err) => {
231+
reject(err);
232+
});
233+
});
234+
},
199235
getNote(context, payload) {
200236
return new Promise((resolve, reject) => {
201237
const found = context.state.notes[payload.id];
@@ -362,7 +398,7 @@ db.get('_design/all_notes')
362398
});
363399
}
364400
})
365-
.catch((err) => {
401+
.catch((err) => {
366402
if(err.status === 404) {
367403
db.post(allNotes)
368404
.then(() => {
@@ -382,6 +418,50 @@ db.get('_design/all_notes')
382418
});
383419
});
384420

421+
const subjectsWeights = {
422+
_id: '_design/subjects_weights',
423+
views: {
424+
subjects_weights: {
425+
map: 'function (doc) { if (doc.data_type == "note" && doc.subjects != undefined) { for(let i=0; i<doc.subjects.length; i++) { emit(doc.subjects[i], 1); } } }',
426+
reduce: '_count',
427+
},
428+
},
429+
};
430+
431+
db.get('_design/subjects_weights')
432+
.then((doc) => {
433+
Vue.set(subjectsWeights, '_rev', doc._rev);
434+
435+
if(doc.views.subjects_weights.map !== subjectsWeights.views.subjects_weights.map || doc.views.subjects_weights.reduce !== subjectsWeights.views.subjects_weights.reduce) {
436+
db.put(subjectsWeights)
437+
.then(() => {
438+
console.log('_design/subjects_weights updated');
439+
})
440+
.catch((err) => {
441+
console.error('CPE0012: ', err);
442+
});
443+
}
444+
})
445+
.catch((err) => {
446+
if(err.status === 404) {
447+
db.post(subjectsWeights)
448+
.then(() => {
449+
console.log('_design/subjects_weights created');
450+
})
451+
.catch((err2) => {
452+
console.error('CPE0013: ', err2);
453+
});
454+
} else {
455+
console.error('CPE0014: ', err);
456+
}
457+
})
458+
.finally(() => {
459+
store.dispatch('fetchAllSubjects')
460+
.catch((err) => {
461+
console.error('CPE0015: ', err);
462+
});
463+
});
464+
385465
dbSettings.get('locale')
386466
.then((doc) => {
387467
store.commit('setLocale', {value: doc.value});
@@ -420,6 +500,8 @@ db.changes({
420500
} else if(change.deleted === true && typeof(store.state.notes[change.doc._id]) !== 'undefined') {
421501
console.log('DB:delete:', change.doc);
422502
store.commit('deleteNote', { data: change.doc });
503+
} else {
504+
console.log('DB:ignored:', change);
423505
}
424506
})
425507
.on('error', (err) => {

0 commit comments

Comments
 (0)