@ -1,5 +1,6 @@
import App from './App.js' ;
import App from './App.js' ;
import nanoid from './nanoid.min.js' ;
import nanoid from './nanoid.min.js' ;
import { load _bin _handler , hash _change _handler } from './handlers/index.js' ;
const produce = immer . produce ;
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
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
@ -13,8 +14,18 @@ const handleActions = (actionsMap, defaultState) =>
action && action ( draft , payload ) ;
action && action ( draft , payload ) ;
} ) ;
} ) ;
// get bin id from URL, and load notes from that bin; if a bin id isn't specified in the URL, create a new one and update the URL
let bin _id = window . location . hash . substring ( 1 ) ; // extract the leading '#'
if ( bin _id === '' ) {
bin _id = nanoid ( ) ;
window . history . replaceState ( null , '' , '#' + bin _id ) ;
}
// the actual loading from server is done later, after store and dispatch are defined
const reducer = handleActions ( {
const reducer = handleActions ( {
'new-bin' : ( s , bin ) => { s . bin = bin ; } ,
'new-bin' : ( s , bin ) => { s . bin = bin ; s . notes = [ ] ; } ,
'bin-requested' : ( s , bin _id ) => { s . bin = { id : bin _id } ; s . notes = [ ] ; } ,
'bin-loaded' : ( s , { bin , _notes } ) => { s . bin = bin ; s . notes = _notes . map ( n => ( { is _editing : false , temp _text : n . text , bin _id : bin . id , note : n } ) ) ; } ,
'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 , is _focused : true , note : { id : id , text : '' , modified : date } } ) ; } ,
'add-note' : ( s , { id , date } ) => { s . notes . unshift ( { is _editing : true , temp _text : '' , bin _id : s . bin . id , is _focused : true , note : { id : id , text : '' , modified : date } } ) ; } ,
@ -24,7 +35,7 @@ const reducer = handleActions({
'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 ; } ,
'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 ; }
'update-sorting' : ( s , sorting ) => { s . sorting = sorting ; }
} , {
} , {
bin : { id : nanoid( ) } ,
bin : { id : bin_id } ,
notes : [
notes : [
//{is_editing: false, temp_text: '', bin_id: '', note: {id: nanoid(), text: 'Note one', modified: 1}},
//{is_editing: false, temp_text: '', bin_id: '', note: {id: nanoid(), text: 'Note one', modified: 1}},
] ,
] ,
@ -42,7 +53,11 @@ store.subscribe(()=>{
m . render ( root , m ( App , { state : store . getState ( ) , dispatch } ) ) ;
m . render ( root , m ( App , { state : store . getState ( ) , dispatch } ) ) ;
} ) ;
} ) ;
window . addEventListener ( "hashchange" , ( e ) => hash _change _handler ( store . getState ( ) , dispatch , e ) , false ) ;
let state = store . getState ( ) ;
load _bin _handler ( state , dispatch , bin _id ) ;
// we don't want Mithril auto-redraw system in place, since Redux will manually re-render when necessary with store.subscribe():
// 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.mount(root, App);
m . render ( root , m ( App , { state : store . getState ( ) , dispatch } ) )
m . render ( root , m ( App , { state , dispatch } ) )