faux-api calls, separate event handler modules

This commit is contained in:
brian
2021-03-15 15:32:55 -04:00
parent f191cc998f
commit 6e7ab2ea67
8 changed files with 255 additions and 95 deletions
+32
View File
@@ -0,0 +1,32 @@
import nanoid from '../nanoid.min.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})
.then(res=>{
dispatch({type: 'notes-loaded', notes: res.notes});
});
};
const runSearch = function(state, dispatch){
api.post('/search', {search_term: state.search_term})
.then(res=>{
dispatch({type:'update-search-results', notes: res.notes});
});
};
const new_note_handler = function(state, dispatch){
dispatch({type:'add-note', id: nanoid()});
};
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});
}
};
const sorting_change_handler = function(state, dispatch, e){
dispatch({type:'update-sorting', sorting: e.target.value});
};
export {new_note_handler, search_term_change_handler, sorting_change_handler, load_notes};
+21
View File
@@ -0,0 +1,21 @@
//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});
};
const cancel_handler = function(note_state, dispatch){
dispatch({type: 'update-note-editing', note_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});
api.post('/save', {note_id: note_state.note.id, text: note_state.temp_text})
};
export { edit_handler, cancel_handler, text_change_handler, save_handler };