From dffdcfcd606b26985e88558b687bb503e6e6e197 Mon Sep 17 00:00:00 2001 From: brian Date: Mon, 15 Mar 2021 18:04:18 -0400 Subject: [PATCH] fixed syncing a note's temp_text with its actual text, in different sitations; updating text updates 'modified' time on the mock server --- Note.js | 2 +- api-stub.js | 1 + handlers/App.js | 4 ++-- handlers/Note.js | 4 ++-- index.js | 8 ++++---- 5 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Note.js b/Note.js index 6b8323e..a887b7b 100644 --- a/Note.js +++ b/Note.js @@ -8,7 +8,7 @@ function Note(vnode_init){ const {is_editing, note} = note_state; if(is_editing){ return m('.note', [ - m('textarea', {key: 'textarea', onchange: text_change_handler.bind(null, note_state, dispatch) }, note.text), + m('textarea', {key: 'textarea', onchange: text_change_handler.bind(null, note_state, dispatch) }, note_state.temp_text), m('.buttons', {key: 'editing-buttons'}, [ m('button', {key: 'cancel-button', onclick: cancel_handler.bind(null, note_state, dispatch) }, 'Cancel'), m('button', {key: 'save-button', onclick: save_handler.bind(null, note_state, dispatch) }, 'Save') diff --git a/api-stub.js b/api-stub.js index b704c18..d264224 100644 --- a/api-stub.js +++ b/api-stub.js @@ -35,6 +35,7 @@ endpoints.post['/save'] = function(resolve, reject, body){ const note = state.notes.find( n => n.id===note_id ); if(note){ note.text = text; + note.modified = Date.now(); } else{ state.notes.push({id: note_id, text, modified: Date.now()}); diff --git a/handlers/App.js b/handlers/App.js index 6633404..7c48668 100644 --- a/handlers/App.js +++ b/handlers/App.js @@ -1,6 +1,6 @@ import nanoid from '../nanoid.min.js'; -import api from '../api.js'; -//import api from '../api-stub.js'; +//import api from '../api.js'; +import api from '../api-stub.js'; const load_notes = function(state, dispatch){ api.post('/load-notes', {bin_id: state.bin.id}) diff --git a/handlers/Note.js b/handlers/Note.js index a48f899..911be87 100644 --- a/handlers/Note.js +++ b/handlers/Note.js @@ -1,5 +1,5 @@ -import api from '../api.js'; -//import api from '../api-stub.js'; +//import api from '../api.js'; +import api from '../api-stub.js'; const edit_handler = function(note_state, dispatch){ dispatch({type: 'update-note-editing', note_id: note_state.note.id, is_editing: true}); diff --git a/index.js b/index.js index 09fe7a7..4c71024 100644 --- a/index.js +++ b/index.js @@ -12,7 +12,7 @@ function search_reducer(old_state, new_state, action){ new_state.search_term = action.search_term; } else if(action.type === 'update-search-results'){ - new_state.notes = action.notes.map(note=>({is_editing: false, note})); + new_state.notes = action.notes.map(note=>({is_editing: false, temp_text: '', note})); new_state.search_term = old_state.search_term; } else{ @@ -22,10 +22,10 @@ function search_reducer(old_state, new_state, action){ function notes_reducer(old_state, new_state, action){ if(action.type === 'add-note'){ - new_state.notes = old_state.notes.concat([{is_editing: true, note: {id: action.id, text: '', modified: Date.now()}}]) + new_state.notes = ([{is_editing: true, note: {id: action.id, text: '', modified: Date.now()}}]).concat(old_state.notes) } else if(action.type === 'notes-loaded'){ - new_state.notes = action.notes.map(note=>({is_editing: false, note})); + new_state.notes = action.notes.map(note=>({is_editing: false, temp_text: note.text, note})); } else if(action.type === 'update-note-text'){ const i = old_state.notes.findIndex(note_state => note_state.note.id === action.note_id); @@ -35,7 +35,7 @@ function notes_reducer(old_state, new_state, action){ else if(action.type === 'update-note-editing'){ const i = old_state.notes.findIndex(note_state => note_state.note.id === action.note_id); new_state.notes = old_state.notes.slice(); - new_state.notes[i] = {...new_state.notes[i], is_editing: action.is_editing}; + new_state.notes[i] = {...new_state.notes[i], is_editing: action.is_editing, temp_text: new_state.notes[i].note.text}; } else{ new_state.notes = old_state.notes;