faux-api calls, separate event handler modules
parent
f191cc998f
commit
6e7ab2ea67
@ -0,0 +1,54 @@
|
|||||||
|
import nanoid from './nanoid.min.js';
|
||||||
|
|
||||||
|
const api_stub = {};
|
||||||
|
|
||||||
|
// for mocking persistent state, for use by the fake endpoints, just as a real server endpoint would have DB access for state:
|
||||||
|
const state = {
|
||||||
|
notes: [
|
||||||
|
{id: nanoid(), text: 'Note one', modified: Date.now()},
|
||||||
|
{id: nanoid(), text: 'Note two', modified: Date.now()},
|
||||||
|
{id: nanoid(), text: 'Note three', modified: Date.now()}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
const endpoints = {
|
||||||
|
post: {}
|
||||||
|
};
|
||||||
|
|
||||||
|
endpoints.post['/load-notes'] = function(resolve, reject, body){
|
||||||
|
resolve( {status: 'ok', notes: state.notes} );
|
||||||
|
};
|
||||||
|
|
||||||
|
endpoints.post['/search'] = function(resolve, reject, body){
|
||||||
|
const notes = state.notes.filter(n => n.text.indexOf(body.search_term) !== -1 );
|
||||||
|
resolve( {status: 'ok', notes} );
|
||||||
|
};
|
||||||
|
|
||||||
|
endpoints.post['/save'] = function(resolve, reject, body){
|
||||||
|
const {note_id, text} = body;
|
||||||
|
const note = state.notes.find( n => n.id===note_id );
|
||||||
|
if(note){
|
||||||
|
note.text = text;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
state.notes.push({id: note_id, text, modified: Date.now()});
|
||||||
|
}
|
||||||
|
resolve( {status: 'ok'} );
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
api_stub.post = function(url, body){
|
||||||
|
if(endpoints.post[url]){
|
||||||
|
return new Promise((resolve, reject)=>{
|
||||||
|
endpoints.post[url](resolve, reject, body);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
return new Promise((resolve, reject)=>{
|
||||||
|
reject( {error: 'no such endpoint'} );
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default api_stub;
|
@ -0,0 +1,11 @@
|
|||||||
|
const api = {};
|
||||||
|
|
||||||
|
api.post = function(url, body){
|
||||||
|
return m.request({
|
||||||
|
method: 'POST',
|
||||||
|
url,
|
||||||
|
body
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export default api;
|
@ -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};
|
@ -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 };
|
@ -1,4 +1,83 @@
|
|||||||
import App from './App.js';
|
import App from './App.js';
|
||||||
|
import nanoid from './nanoid.min.js';
|
||||||
|
|
||||||
var root = document.body;
|
var root = document.body;
|
||||||
m.mount(root, App);
|
|
||||||
|
function bin_reducer(old_state, new_state, action){
|
||||||
|
new_state.bin = old_state.bin;
|
||||||
|
}
|
||||||
|
|
||||||
|
function search_reducer(old_state, new_state, action){
|
||||||
|
if(action.type === 'update-search-term'){
|
||||||
|
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.search_term = old_state.search_term;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
new_state.search_term = old_state.search_term;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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()}}])
|
||||||
|
}
|
||||||
|
else if(action.type === 'notes-loaded'){
|
||||||
|
new_state.notes = action.notes.map(note=>({is_editing: false, note}));
|
||||||
|
}
|
||||||
|
else if(action.type === 'update-note-text'){
|
||||||
|
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], note: {...new_state.notes[i].note, text: action.text, modified: Date.now()}};
|
||||||
|
}
|
||||||
|
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};
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
new_state.notes = old_state.notes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function sorting_reducer(old_state, new_state, action){
|
||||||
|
if(action.type === 'update-sorting'){
|
||||||
|
new_state.sorting = action.sorting;
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
new_state.sorting = old_state.sorting;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function reducer(old_state, action){
|
||||||
|
const new_state = {};
|
||||||
|
bin_reducer(old_state, new_state, action);
|
||||||
|
notes_reducer(old_state, new_state, action);
|
||||||
|
search_reducer(old_state, new_state, action);
|
||||||
|
sorting_reducer(old_state, new_state, action);
|
||||||
|
return new_state;
|
||||||
|
}
|
||||||
|
|
||||||
|
// create Redux store, with Redux DevTools enabled:
|
||||||
|
const store = Redux.createStore(reducer, /* preloadedState, */ {
|
||||||
|
bin: {id: nanoid()},
|
||||||
|
notes: [
|
||||||
|
//{is_editing: false, note: {id: nanoid(), text: 'Note one', modified: 1}},
|
||||||
|
],
|
||||||
|
search_term: '',
|
||||||
|
sorting: 'new->old'
|
||||||
|
//search_result_notes: []
|
||||||
|
},
|
||||||
|
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__());
|
||||||
|
const dispatch = function(...args){ store.dispatch(...args); };
|
||||||
|
|
||||||
|
store.subscribe(()=>{
|
||||||
|
m.render(root, m(App, {state: store.getState(), dispatch}));
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
// we don't want Mithril auto-redraw system in place, since Redux will manually re-render when necessary with store.subscribe():
|
||||||
|
// m.mount(root, App);
|
||||||
|
m.render(root, m(App, {state: store.getState(), dispatch}))
|
@ -0,0 +1 @@
|
|||||||
|
export default (t=21)=>{let e="",r=crypto.getRandomValues(new Uint8Array(t));for(;t--;){let n=63&r[t];e+=n<36?n.toString(36):n<62?(n-26).toString(36).toUpperCase():n<63?"_":"-"}return e};
|
Loading…
Reference in New Issue