From b7c00b352ba7fa59b0d0d89b0d37d09d31b74693 Mon Sep 17 00:00:00 2001 From: dannc Date: Sun, 17 Mar 2019 14:45:29 +0700 Subject: [PATCH] Add user words logic --- src/components/AppHeader.vue | 2 +- src/components/Article/Word/Card.vue | 26 +--- src/components/Article/Word/OriginalWord.vue | 87 +++++++++++++ src/components/User/ProfileData.vue | 4 +- src/components/User/ProfileWords.vue | 122 +++++++++++++++++++ src/filters/formattedDate.js | 11 +- src/filters/index.js | 1 + src/store/user.js | 2 +- src/store/userData.js | 38 ++++++ src/utils/eventBus.js | 4 +- src/views/Profile.vue | 3 + 11 files changed, 271 insertions(+), 29 deletions(-) create mode 100644 src/components/Article/Word/OriginalWord.vue create mode 100644 src/components/User/ProfileWords.vue create mode 100644 src/filters/index.js diff --git a/src/components/AppHeader.vue b/src/components/AppHeader.vue index 52476d0..06aa393 100644 --- a/src/components/AppHeader.vue +++ b/src/components/AppHeader.vue @@ -27,7 +27,7 @@ - + diff --git a/src/components/Article/Word/Card.vue b/src/components/Article/Word/Card.vue index 9238825..f86977c 100644 --- a/src/components/Article/Word/Card.vue +++ b/src/components/Article/Word/Card.vue @@ -2,19 +2,7 @@
- - - W - - Слово / das word - - - - RW - - Выражение / die Redewendung - - {{ getFullOriginalWord(wordEntity) }} +
@@ -39,7 +27,7 @@ diff --git a/src/components/Article/Word/OriginalWord.vue b/src/components/Article/Word/OriginalWord.vue new file mode 100644 index 0000000..0411738 --- /dev/null +++ b/src/components/Article/Word/OriginalWord.vue @@ -0,0 +1,87 @@ + + + + diff --git a/src/components/User/ProfileData.vue b/src/components/User/ProfileData.vue index 5e61539..8bd63bd 100644 --- a/src/components/User/ProfileData.vue +++ b/src/components/User/ProfileData.vue @@ -163,12 +163,12 @@ export default { }, }, created() { - this.$bus.$on(EVENTS.USER.DATA_CHANGED, () => { + this.$bus.$on(EVENTS.USER.PROFILE_CHANGED, () => { this.dialog = false; }); }, beforeDestroy() { - this.$bus.$off(EVENTS.USER.DATA_CHANGED); + this.$bus.$off(EVENTS.USER.PROFILE_CHANGED); }, }; diff --git a/src/components/User/ProfileWords.vue b/src/components/User/ProfileWords.vue new file mode 100644 index 0000000..42a8a5c --- /dev/null +++ b/src/components/User/ProfileWords.vue @@ -0,0 +1,122 @@ + + + + diff --git a/src/filters/formattedDate.js b/src/filters/formattedDate.js index 15b3c53..927e60d 100644 --- a/src/filters/formattedDate.js +++ b/src/filters/formattedDate.js @@ -1,12 +1,17 @@ -const formattedDate = (value) => { +export const buildDate = (value) => { if (value === null) { return null; } if (value instanceof Date) { - return value.toLocaleDateString(); + return value; } - return value.toDate().toLocaleDateString(); + return value.toDate(); +} + +const formattedDate = (value) => { + const buildDate = buildDate(value); + return buildDate ? buildDate.toLocaleDateString() : null; }; export default formattedDate; \ No newline at end of file diff --git a/src/filters/index.js b/src/filters/index.js new file mode 100644 index 0000000..35b3c59 --- /dev/null +++ b/src/filters/index.js @@ -0,0 +1 @@ +export * from '@/filters/formattedDate'; diff --git a/src/store/user.js b/src/store/user.js index e034c0b..a321777 100644 --- a/src/store/user.js +++ b/src/store/user.js @@ -119,7 +119,7 @@ export default { }); await commit('setProcessing', false); - await EventBus.notify(EVENTS.USER.DATA_CHANGED); + await EventBus.notify(EVENTS.USER.PROFILE_CHANGED); }, }, getters: { diff --git a/src/store/userData.js b/src/store/userData.js index a6c7511..09505eb 100644 --- a/src/store/userData.js +++ b/src/store/userData.js @@ -1,4 +1,8 @@ import Vue from 'vue'; +import firebase from 'firebase/app'; +import 'firebase/firestore'; + +import { EventBus, EVENTS } from '@/utils'; const defaultUserData = { articles: {}, @@ -30,6 +34,7 @@ export default { }) .catch(e => window.console.error(e)); + await EventBus.notify(EVENTS.USER.DATA_LOADED); await commit('setProcessing', false); }, async addUserArticle({ commit, getters }, articleId) { @@ -108,6 +113,33 @@ export default { .then(() => commit('openUserArticlePart', { articleId, partId, timestamp })) .catch(e => window.console.error(e)); }, + processUserLearnWord({ commit, getters }, wordKey) { + const word = getters.userData.words[wordKey]; + + const userDataRef = Vue.$db.collection('userData').doc(getters.userId); + + if (word.bucket === 5) { + return userDataRef.update({ + [`words.${wordKey}`]: firebase.firestore.FieldValue.delete(), + }) + .then(() => commit('removeUserWord', wordKey)) + .then(() => EventBus.notify(EVENTS.USER.WORD_UPDATED, { wordKey })); + } + + let nextShowDate = new Date(); + nextShowDate.setDate((new Date().getDate() + word.bucket * 2)); + word.nextShowDate = nextShowDate; + word.bucket = word.bucket + 1; + + userDataRef.set({ + words: { + [wordKey]: word, + }, + }, { merge: true }) + .then(() => commit('updateUserWord', { word, wordKey })) + .then(() => EventBus.notify(EVENTS.USER.WORD_UPDATED, { wordKey })); + + }, }, mutations: { setUserData(state, payload) { @@ -134,6 +166,12 @@ export default { Vue.set(state.userData.articles[articleId].parts[partId], 'finishedAt', timestamp); Vue.set(state.userData.articles[articleId].parts[partId], 'rating', rating); }, + removeUserWord(state, wordKey) { + Vue.delete(state.userData.words, wordKey); + }, + updateUserWord(state, { word, wordKey }) { + Vue.set(state.userData.words, wordKey, word); + }, }, getters: { userData: state => state.userData, diff --git a/src/utils/eventBus.js b/src/utils/eventBus.js index d4e6075..6bab641 100644 --- a/src/utils/eventBus.js +++ b/src/utils/eventBus.js @@ -2,7 +2,9 @@ import Vue from 'vue'; export const EVENTS = { USER: { - DATA_CHANGED: 'user-profile-data-changed', + DATA_LOADED: 'user-data-loaded', + PROFILE_CHANGED: 'user-profile-data-changed', + WORD_UPDATED: 'user-data-word-updated', }, }; diff --git a/src/views/Profile.vue b/src/views/Profile.vue index 64a8543..6809fa0 100644 --- a/src/views/Profile.vue +++ b/src/views/Profile.vue @@ -19,6 +19,7 @@ + @@ -29,6 +30,7 @@