import App from './App.js'; import nanoid from './nanoid.min.js'; const produce = immer.produce; immer.setAutoFreeze(false); // needed for high-frequency updated values, like onkeyup->note.temp_text; only once 'save' is called will it produce a new immutable state tree var root = document.body; /* Ruthlessly taken from [https://gist.github.com/kitze/fb65f527803a93fb2803ce79a792fff8]: */ const handleActions = (actionsMap, defaultState) => (state=defaultState, {type, payload}) => produce(state, draft => { const action = actionsMap[type]; action && action(draft, payload); }); const reducer = handleActions({ 'new-bin': (s, bin) => { s.bin=bin; }, '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})); }, 'add-note': (s, {id, date}) => { s.notes.unshift({is_editing: true, temp_text: '', bin_id: s.bin.id, note: {id: id, text: '', modified: date}}); }, 'notes-loaded': (s, _notes) => { s.notes = _notes.map(n=>({is_editing: false, temp_text: n.text, bin_id: s.bin_id, note:n})); }, 'update-note-text': (s, {id, text}) => { const note_s=s.notes.find(n=>n.note.id===id); note_s.note.text=text; }, // updates underlying note text (i.e. the "model", not the app note_state) "in the background" (e.g. from a server-pushed update), regardless of whether it's being edited; "save" is a separate action, below 'update-note-editing': (s, {id, is_editing}) => { const note_s=s.notes.find(n=>n.note.id===id); note_s.is_editing=is_editing; note_s.temp_text=note_s.note.text; }, 'save-note-edit': (s, {id, text}) => { const note_s=s.notes.find(n=>n.note.id===id); note_s.note.text=text; note_s.temp_text=text; note_s.is_editing=false; }, 'update-sorting': (s, sorting) => { s.sorting=sorting; } }, { 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: [] }); // create Redux store, with Redux DevTools enabled: const store = Redux.createStore(reducer, /* preloadedState, */ window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()); const dispatch = function(action_type, payload){ store.dispatch({type:action_type, payload}); }; 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}))