import App from './App.js'; import nanoid from './nanoid.min.js'; var root = document.body; function bin_reducer(old_state, new_state, action){ if(action.type === 'new-bin'){ new_state.bin = action.bin; } else{ 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, temp_text: '', bin_id: old_state.bin.id, 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 = ([{is_editing: true, temp_text: '', bin_id: old_state.bin.id, 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, temp_text: note.text, bin_id: action.bin_id, 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, temp_text: new_state.notes[i].note.text}; } 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, temp_text: '', bin_id: '', 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}))