Skip to content

Commit

Permalink
Loading available subjects in vuex store.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimskapt committed Sep 19, 2018
1 parent d06ea03 commit 18afe42
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@
"Title": "Title",
"confidential": "confidential",
"It is confidential": "It is confidential",
"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"
"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",
"Subjects": "Subjects"
}
3 changes: 2 additions & 1 deletion src/locales/fr-FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@
"Title": "Titre",
"confidential": "confidentiel",
"It is confidential": "C'est confidentiel",
"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"
"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",
"Subjects": "Sujets"
}
84 changes: 83 additions & 1 deletion src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const store = new Vuex.Store({
state: {
settings: DEFAULT_SETTINGS,
notes: {},
subjects: {},
},
mutations: {
setNote(state, payload) {
Expand All @@ -43,6 +44,18 @@ const store = new Vuex.Store({
Vue.toasted.show(i18n.t('A note has been updated (or added)'), { duration: 2000, type: 'info', icon: 'edit' });
}
},
setSubject(state, payload) {
if(typeof(payload.data) === 'undefined') {
console.error('$store.mutations.setSubject : missing "data" object on payload');
return;
}
if(typeof(payload.data.key) === 'undefined') {
console.error('$store.mutations.setSubject : missing "key" value on payload.data');
return;
}

Vue.set(state.subjects, payload.data.key, payload.data.value);
},
deleteNote(state, payload) {
if(typeof(payload.data) === 'undefined') {
console.error('$store.mutations.deleteNote : missing "data" object in payload');
Expand Down Expand Up @@ -196,6 +209,29 @@ const store = new Vuex.Store({
});
});
},
fetchAllSubjects(context) {
return new Promise((resolve, reject) => {
db.query('subjects_weights/subjects_weights', {group: true, reduce: true})
.then((res) => {
res.rows.sort((a, b) => {
if(a.value < b.value) {
return +1;
} else if(a.value > b.value) {
return -1;
} else {
return 0;
}
});

res.rows.forEach((row) => {
context.commit('setSubject', {data: row});
});
})
.catch((err) => {
reject(err);
});
});
},
getNote(context, payload) {
return new Promise((resolve, reject) => {
const found = context.state.notes[payload.id];
Expand Down Expand Up @@ -362,7 +398,7 @@ db.get('_design/all_notes')
});
}
})
.catch((err) => {
.catch((err) => {
if(err.status === 404) {
db.post(allNotes)
.then(() => {
Expand All @@ -382,6 +418,50 @@ db.get('_design/all_notes')
});
});

const subjectsWeights = {
_id: '_design/subjects_weights',
views: {
subjects_weights: {
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); } } }',
reduce: '_count',
},
},
};

db.get('_design/subjects_weights')
.then((doc) => {
Vue.set(subjectsWeights, '_rev', doc._rev);

if(doc.views.subjects_weights.map !== subjectsWeights.views.subjects_weights.map || doc.views.subjects_weights.reduce !== subjectsWeights.views.subjects_weights.reduce) {
db.put(subjectsWeights)
.then(() => {
console.log('_design/subjects_weights updated');
})
.catch((err) => {
console.error('CPE0012: ', err);
});
}
})
.catch((err) => {
if(err.status === 404) {
db.post(subjectsWeights)
.then(() => {
console.log('_design/subjects_weights created');
})
.catch((err2) => {
console.error('CPE0013: ', err2);
});
} else {
console.error('CPE0014: ', err);
}
})
.finally(() => {
store.dispatch('fetchAllSubjects')
.catch((err) => {
console.error('CPE0015: ', err);
});
});

dbSettings.get('locale')
.then((doc) => {
store.commit('setLocale', {value: doc.value});
Expand Down Expand Up @@ -420,6 +500,8 @@ db.changes({
} else if(change.deleted === true && typeof(store.state.notes[change.doc._id]) !== 'undefined') {
console.log('DB:delete:', change.doc);
store.commit('deleteNote', { data: change.doc });
} else {
console.log('DB:ignored:', change);
}
})
.on('error', (err) => {
Expand Down

0 comments on commit 18afe42

Please sign in to comment.