switch to immer.js

This commit is contained in:
brian
2021-03-17 09:21:23 -04:00
parent 8bac25ab2c
commit a248600c21
5 changed files with 43 additions and 83 deletions
+8 -7
View File
@@ -5,34 +5,35 @@ import api from '../api-stub.js';
const load_notes = function(state, dispatch){
api.post('/load-notes', {bin_id: state.bin.id})
.then(res=>{
dispatch({type: 'notes-loaded', notes: res.notes, bin_id: state.bin.id});
dispatch({type: 'notes-loaded', payload: res.notes});
});
};
const runSearch = function(state, dispatch){
api.post('/search', {search_term: state.search_term, sorting: state.sorting, bin_id: state.bin.id})
.then(res=>{
dispatch({type:'update-search-results', notes: res.notes});
dispatch({type:'update-search-results', payload: res.notes});
});
};
const new_note_handler = function(state, dispatch){
dispatch({type:'add-note', id: nanoid()});
dispatch({type:'add-note', payload:{id: nanoid(), date: Date.now()}});
};
const search_term_change_handler = function(state, dispatch, e){
if(e.code === 'Enter'){
runSearch(state, dispatch);
}
else{
dispatch({type:'update-search-term', search_term: e.target.value});
dispatch({type:'update-search-term', payload: e.target.value});
}
};
const sorting_change_handler = function(state, dispatch, e){
runSearch(state, dispatch);
dispatch({type:'update-sorting', sorting: e.target.value});
dispatch({type:'update-sorting', payload: e.target.value});
};
const new_bin_handler = function(state, dispatch){
const id = nanoid();
dispatch({type: 'new-bin', bin: {id}});
dispatch({type: 'notes-loaded', notes: [], bin_id: id});
// TODO: consolidate: this will cause two redraws:
dispatch({type: 'new-bin', payload:{id}});
dispatch({type: 'notes-loaded', payload: []});
};
export {new_note_handler, search_term_change_handler, sorting_change_handler, load_notes, new_bin_handler};
+3 -6
View File
@@ -2,19 +2,16 @@
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});
dispatch({type: 'update-note-editing', payload:{id: note_state.note.id, is_editing: true}});
};
const cancel_handler = function(note_state, dispatch){
dispatch({type: 'update-note-editing', note_id: note_state.note.id, is_editing: false});
dispatch({type: 'update-note-editing', payload:{id: note_state.note.id, is_editing: false}});
};
const text_change_handler = function(note_state, dispatch, e){
note_state.temp_text = e.target.value;
};
const save_handler = function(note_state, dispatch){
// TODO: consolidate: this will cause two redraws:
dispatch({type: 'update-note-text', note_id: note_state.note.id, text: note_state.temp_text});
//note.text = temp_text;
dispatch({type: 'update-note-editing', note_id: note_state.note.id, is_editing: false});
dispatch({type: 'save-note-edit', payload:{id: note_state.note.id, text: note_state.temp_text}});
api.post('/save', {bin_id: note_state.bin_id, note_id: note_state.note.id, text: note_state.temp_text})
};