@ -1,72 +1,29 @@
import App from './App.js' ;
import App from './App.js' ;
import nanoid from './nanoid.min.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 ;
var root = document . body ;
function bin _reducer ( old _state , new _state , action ) {
/* Ruthlessly taken from [https://gist.github.com/kitze/fb65f527803a93fb2803ce79a792fff8]: */
if ( action . type === 'new-bin' ) {
const handleActions = ( actionsMap , defaultState ) =>
new _state . bin = action . bin ;
( state = defaultState , { type , payload } ) =>
}
produce ( state , draft => {
else {
const action = actionsMap [ type ] ;
new _state . bin = old _state . bin ;
action && action ( draft , payload ) ;
}
} ) ;
}
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 reducer = handleActions ( {
const store = Redux . createStore ( reducer , /* preloadedState, */ {
'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 ( ) } ,
bin : { id : nanoid ( ) } ,
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}},
@ -74,7 +31,10 @@ const store = Redux.createStore(reducer, /* preloadedState, */ {
search _term : '' ,
search _term : '' ,
sorting : 'new->old'
sorting : 'new->old'
//search_result_notes: []
//search_result_notes: []
} ,
} ) ;
// create Redux store, with Redux DevTools enabled:
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 ( ... args ) { store . dispatch ( ... args ) ; } ;