You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pastebin/index.js

83 lines
2.8 KiB
JavaScript

import App from './App.js';
import nanoid from './nanoid.min.js';
var root = document.body;
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}))