1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Update state and view after note has been saved to db

This commit is contained in:
Laurent Cozic 2017-05-11 21:02:47 +00:00
parent 3d46cf4b55
commit 38990d1125
2 changed files with 29 additions and 16 deletions

View File

@ -13,7 +13,9 @@ class BaseModel {
if (isNew) o.id = createUuid(); if (isNew) o.id = createUuid();
if (isNew) { if (isNew) {
let q = Database.insertQuery(this.tableName(), o); let q = Database.insertQuery(this.tableName(), o);
return this.db().insert(q.sql, q.params); return this.db().insert(q.sql, q.params).then(() => {
return o;
});
} else { } else {
Log.error('NOT EIMPLEMETNED'); Log.error('NOT EIMPLEMETNED');
// TODO: update // TODO: update

View File

@ -43,15 +43,32 @@ const reducer = (state = defaultState, action) => {
break; break;
case 'NOTES_LOADED': // Replace all the notes with the provided array
case 'NOTES_UPDATE_ALL':
newState = Object.assign({}, state); newState = Object.assign({}, state);
newState.notes = action.notes; newState.notes = action.notes;
break; break;
case 'SAVE_NOTE': // Insert the note into the note list if it's new, or
// update it if it already exists.
case 'NOTES_UPDATE_ONE':
let newNotes = state.notes.splice(0);
let found = false;
for (let i = 0; i < newNotes.length; i++) {
let n = newNotes[i];
if (n.id == action.note.id) {
newNotes[i] = action.note;
found = true;
break;
}
}
if (!found) newNotes.push(action.note);
newState = Object.assign({}, state);
newState.notes = newNotes;
break; break;
} }
@ -124,20 +141,14 @@ class NoteScreenComponent extends React.Component {
saveNoteButton_press = () => { saveNoteButton_press = () => {
// TODO: if state changes are asynchronous, how to be sure that, when // TODO: if state changes are asynchronous, how to be sure that, when
// the button is presssed, this.state.note contains the actual note? // the button is presssed, this.state.note contains the actual note?
// - Save to database Note.save(this.state.note).then((note) => {
// - Dispatch "noteSaved" when done this.props.dispatch({
// -* Move i^p on state type: 'NOTES_UPDATE_ONE',
note: note,
Note.save(this.state.note).then(() => { });
Log.info('NOTE INSERTED'); }).catch((error) => {
}).catch((error) => { Log.warn('Cannot save note', error);
Log.warn('CANONT INSERT NOTE', error);
}); });
// this.props.dispatch({
// type: 'SAVE_NOTE',
// note: this.state.note,
// });
} }
render() { render() {
@ -170,7 +181,7 @@ class AppComponent extends React.Component {
componentDidMount() { componentDidMount() {
Note.previews().then((notes) => { Note.previews().then((notes) => {
this.props.dispatch({ this.props.dispatch({
type: 'NOTES_LOADED', type: 'NOTES_UPDATE_ALL',
notes: notes, notes: notes,
}); });
}).catch((error) => { }).catch((error) => {