Skip to content

Commit 5f4b5f1

Browse files
committed
feat: fix notes changes auto-saving, rework editNote mutation
1 parent b9b7fab commit 5f4b5f1

File tree

6 files changed

+43
-34
lines changed

6 files changed

+43
-34
lines changed

components/notesLayout/Note.vue

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ export default {
7777
pageId() {
7878
return this.$route.params.pageID
7979
},
80-
...mapState('notes', ['currentNotes'])
80+
...mapState('notes', ['currentNotes']),
81+
...mapState('auth', ['currentUser'])
8182
},
8283
methods: {
8384
focus() {
@@ -93,24 +94,24 @@ export default {
9394
}
9495
},
9596
addNote() {
96-
this.$store.commit('notes/addNote', this.pageId)
97+
this.$store.commit('notes/addNote', { pageId: this.pageId, author: this.currentUser.userId })
9798
setTimeout(() => {
9899
this.$parent.$refs[
99100
this.currentNotes[this.currentNotes.length - 1]?.noteId
100101
][0]?.focus()
101102
}, 100)
102103
},
103104
save() {
104-
const updatedData = {
105-
content: this.updatedContent,
105+
this.$store.commit('notes/editNote', {
106106
page: this.page,
107107
noteId: this.noteId,
108-
styles: this.styles,
109108
author: this.author,
110-
position: this.position,
111-
newNote: this.newNote
112-
}
113-
this.$store.commit('notes/editNote', updatedData)
109+
updated: {
110+
content: this.updatedContent,
111+
styles: this.styles,
112+
position: this.position,
113+
}
114+
})
114115
}
115116
}
116117
}

layouts/default.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<div class="header">
88
<v-toolbar-title class="logo">
99
<v-avatar><img src="" alt="" width="32px" /></v-avatar>
10-
<span class="logo-text">NotesAPP</span>
10+
<nuxt-link to="/" class="logo-text">NotesAPP</nuxt-link>
1111
</v-toolbar-title>
1212
<v-spacer></v-spacer>
1313
<v-toolbar-items class="d-flex align-center">
@@ -79,6 +79,8 @@ export default {
7979
8080
.logo-text {
8181
margin-left: 5px;
82+
color: inherit;
83+
text-decoration: none;
8284
}
8385
8486
.profile-card-nickname {

layouts/notes.vue

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export default {
104104
)
105105
},
106106
methods: {
107-
...mapMutations('notes', ['clearChangedNotes', 'clearRemovedNotes']),
107+
...mapMutations('notes', ['clearChangedNotes', 'clearRemovedNotes', 'removeFromChangedNotes']),
108108
...mapActions('notes', ['clearNewNotes']),
109109
saveAll() {
110110
this.saveNewNotes()
@@ -156,9 +156,17 @@ export default {
156156
return
157157
}
158158
159-
const changedNotes = this.currentNotes.filter((note) =>
160-
this.changedNotes.includes(note.noteId)
161-
)
159+
let changedNotes = this.currentNotes.filter((note) => this.changedNotes.includes(note.noteId))
160+
const newNotesInChangedNotes = changedNotes.filter(note => note.newNote)
161+
if(newNotesInChangedNotes.length > 0) {
162+
this.saveNewNotes()
163+
this.removeFromChangedNotes(...newNotesInChangedNotes.map(note => note.noteId))
164+
changedNotes = changedNotes.filter(note => !note.newNote)
165+
}
166+
if(changedNotes.length <= 0) {
167+
return
168+
}
169+
162170
console.log('Change notes', changedNotes)
163171
this.$axios.post(
164172
'notes/update',

pages/_pageID.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export default {
4646
},
4747
...mapState('notes', ['currentNotes']),
4848
...mapState('pages', ['currentPage']),
49+
...mapState('auth', ['currentUser']),
4950
...mapGetters('pages', ['rootPages'])
5051
},
5152
watch: {
@@ -74,7 +75,7 @@ export default {
7475
...mapActions('pages', ['setCurrentPage', 'editPage']),
7576
...mapActions('notes', ['getNotes']),
7677
addNote() {
77-
this.$store.commit('notes/addNote', this.pageId)
78+
this.$store.commit('notes/addNote', { pageId: this.pageId, author: this.currentUser.userId })
7879
setTimeout(() => {
7980
this.$refs[
8081
this.currentNotes[this.currentNotes.length - 1]?.noteId

store/notes/actions.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ export default {
44
.filter((note) => note.newNote)
55
.forEach((note) => {
66
commit('editNote', {
7-
...note,
8-
newNote: false,
9-
preventApiReq: true
7+
page: note.page,
8+
author: note.author,
9+
noteId: note.noteId,
10+
updated: { newNote: false }
1011
})
1112
})
1213
},

store/notes/mutations.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { v4 as uuid4 } from 'uuid'
22

33
export default {
4-
addNote(state, pageId) {
4+
addNote(state, { pageId, author }) {
55
const noteId = uuid4()
66
state.newNotes.push(noteId)
77
state.notes[pageId].push({
@@ -10,7 +10,7 @@ export default {
1010
position: state.notes[pageId].length,
1111
content: null,
1212
styles: { someV: 'someV' },
13-
author: 'a',
13+
author,
1414
newNote: true
1515
})
1616
},
@@ -23,29 +23,25 @@ export default {
2323
clearChangedNotes(state) {
2424
state.changedNotes = []
2525
},
26+
removeFromChangedNotes(state, ...notesIds) {
27+
notesIds.forEach(noteId => {
28+
state.changedNotes.splice(state.changedNotes.indexOf(noteId), 1)
29+
})
30+
},
2631
setCurrentNotes(state, pageId) {
2732
state.currentNotes = state.notes[pageId] || []
2833
},
29-
editNote(
30-
state,
31-
{ page, noteId, styles, author, content, position, newNote, preventApiReq }
32-
) {
34+
editNote(state, { page, noteId, author, updated }) {
35+
console.log(page, noteId, author, updated, 'editNote')
3336
if (state.removedNotes.includes(noteId)) {
3437
return
3538
}
3639

3740
const foundNote = state.notes[page].find((note) => note.noteId === noteId)
38-
state.notes[page][state.notes[page].indexOf(foundNote)] = {
39-
noteId,
40-
page,
41-
styles,
42-
author,
43-
content,
44-
position,
45-
newNote
46-
}
41+
const foundNoteIndex = state.notes[page].indexOf(foundNote)
42+
state.notes[page][foundNoteIndex] = { ...foundNote, ...updated, page, noteId, author }
4743

48-
if (!state.changedNotes.includes(noteId) && !preventApiReq) {
44+
if (!state.changedNotes.includes(noteId)) {
4945
state.changedNotes.push(noteId)
5046
}
5147
},

0 commit comments

Comments
 (0)