streamlined dispatch api
This commit is contained in:
+7
-7
@@ -5,35 +5,35 @@ import api from '../api-stub.js';
|
|||||||
const load_notes = function(state, dispatch){
|
const load_notes = function(state, dispatch){
|
||||||
api.post('/load-notes', {bin_id: state.bin.id})
|
api.post('/load-notes', {bin_id: state.bin.id})
|
||||||
.then(res=>{
|
.then(res=>{
|
||||||
dispatch({type: 'notes-loaded', payload: res.notes});
|
dispatch('notes-loaded', res.notes);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
const runSearch = function(state, dispatch){
|
const runSearch = function(state, dispatch){
|
||||||
api.post('/search', {search_term: state.search_term, sorting: state.sorting, bin_id: state.bin.id})
|
api.post('/search', {search_term: state.search_term, sorting: state.sorting, bin_id: state.bin.id})
|
||||||
.then(res=>{
|
.then(res=>{
|
||||||
dispatch({type:'update-search-results', payload: res.notes});
|
dispatch('update-search-results', res.notes);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
const new_note_handler = function(state, dispatch){
|
const new_note_handler = function(state, dispatch){
|
||||||
dispatch({type:'add-note', payload:{id: nanoid(), date: Date.now()}});
|
dispatch('add-note', {id: nanoid(), date: Date.now()});
|
||||||
};
|
};
|
||||||
const search_term_change_handler = function(state, dispatch, e){
|
const search_term_change_handler = function(state, dispatch, e){
|
||||||
if(e.code === 'Enter'){
|
if(e.code === 'Enter'){
|
||||||
runSearch(state, dispatch);
|
runSearch(state, dispatch);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
dispatch({type:'update-search-term', payload: e.target.value});
|
dispatch('update-search-term', e.target.value);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
const sorting_change_handler = function(state, dispatch, e){
|
const sorting_change_handler = function(state, dispatch, e){
|
||||||
runSearch(state, dispatch);
|
runSearch(state, dispatch);
|
||||||
dispatch({type:'update-sorting', payload: e.target.value});
|
dispatch('update-sorting', e.target.value);
|
||||||
};
|
};
|
||||||
const new_bin_handler = function(state, dispatch){
|
const new_bin_handler = function(state, dispatch){
|
||||||
const id = nanoid();
|
const id = nanoid();
|
||||||
// TODO: consolidate: this will cause two redraws:
|
// TODO: consolidate: this will cause two redraws:
|
||||||
dispatch({type: 'new-bin', payload:{id}});
|
dispatch('new-bin', {id});
|
||||||
dispatch({type: 'notes-loaded', payload: []});
|
dispatch('notes-loaded', []);
|
||||||
};
|
};
|
||||||
|
|
||||||
export {new_note_handler, search_term_change_handler, sorting_change_handler, load_notes, new_bin_handler};
|
export {new_note_handler, search_term_change_handler, sorting_change_handler, load_notes, new_bin_handler};
|
||||||
+3
-3
@@ -2,16 +2,16 @@
|
|||||||
import api from '../api-stub.js';
|
import api from '../api-stub.js';
|
||||||
|
|
||||||
const edit_handler = function(note_state, dispatch){
|
const edit_handler = function(note_state, dispatch){
|
||||||
dispatch({type: 'update-note-editing', payload:{id: note_state.note.id, is_editing: true}});
|
dispatch('update-note-editing', {id: note_state.note.id, is_editing: true});
|
||||||
};
|
};
|
||||||
const cancel_handler = function(note_state, dispatch){
|
const cancel_handler = function(note_state, dispatch){
|
||||||
dispatch({type: 'update-note-editing', payload:{id: note_state.note.id, is_editing: false}});
|
dispatch('update-note-editing', {id: note_state.note.id, is_editing: false});
|
||||||
};
|
};
|
||||||
const text_change_handler = function(note_state, dispatch, e){
|
const text_change_handler = function(note_state, dispatch, e){
|
||||||
note_state.temp_text = e.target.value;
|
note_state.temp_text = e.target.value;
|
||||||
};
|
};
|
||||||
const save_handler = function(note_state, dispatch){
|
const save_handler = function(note_state, dispatch){
|
||||||
dispatch({type: 'save-note-edit', payload:{id: note_state.note.id, text: note_state.temp_text}});
|
dispatch('save-note-edit', {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})
|
api.post('/save', {bin_id: note_state.bin_id, note_id: note_state.note.id, text: note_state.temp_text})
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ const handleActions = (actionsMap, defaultState) =>
|
|||||||
});
|
});
|
||||||
|
|
||||||
const reducer = handleActions({
|
const reducer = handleActions({
|
||||||
'new-bin': (s, bin) => s.bin=bin,
|
'new-bin': (s, bin) => { s.bin=bin; },
|
||||||
'update-search-term': (s, search_term) => { s.search_term=search_term; },
|
'update-search-term': (s, search_term) => { s.search_term=search_term; },
|
||||||
'update-search-results': (s, _notes) => { s.notes =_notes.map(n=>({is_editing: false, temp_text: '', bin_id: s.bin.id, note:n})); },
|
'update-search-results': (s, _notes) => { s.notes =_notes.map(n=>({is_editing: false, temp_text: '', bin_id: s.bin.id, note:n})); },
|
||||||
'add-note': (s, {id, date}) => { s.notes.unshift({is_editing: true, temp_text: '', bin_id: s.bin.id, note: {id: id, text: '', modified: date}}); },
|
'add-note': (s, {id, date}) => { s.notes.unshift({is_editing: true, temp_text: '', bin_id: s.bin.id, note: {id: id, text: '', modified: date}}); },
|
||||||
@@ -36,7 +36,7 @@ const reducer = handleActions({
|
|||||||
// create Redux store, with Redux DevTools enabled:
|
// create Redux store, with Redux DevTools enabled:
|
||||||
const store = Redux.createStore(reducer, /* preloadedState, */
|
const store = Redux.createStore(reducer, /* preloadedState, */
|
||||||
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__());
|
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__());
|
||||||
const dispatch = function(...args){ store.dispatch(...args); };
|
const dispatch = function(action_type, payload){ store.dispatch({type:action_type, payload}); };
|
||||||
|
|
||||||
store.subscribe(()=>{
|
store.subscribe(()=>{
|
||||||
m.render(root, m(App, {state: store.getState(), dispatch}));
|
m.render(root, m(App, {state: store.getState(), dispatch}));
|
||||||
|
|||||||
Reference in New Issue
Block a user