Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor updating note content to stop directly mutating note object #1634

Merged
merged 1 commit into from
Oct 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions lib/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -320,12 +320,28 @@ export const App = connect(
});
};

onUpdateContent = (note, content) =>
this.props.actions.updateNoteContent({
noteBucket: this.props.noteBucket,
note,
content,
});
onUpdateContent = (note, content) => {
if (!note) {
return;
}

const updatedNote = {
...note,
data: {
...note.data,
content,
modificationDate: Math.floor(Date.now() / 1000),
},
};

// update the bucket but don't force sync right away
// as this happens per keystroke when the user is editing
// a note. The NoteEditor will notify via props when
// it's time to sync via Simperium
const { noteBucket } = this.props;

noteBucket.update(note.id, updatedNote.data, {}, { sync: false });
};

syncNote = noteId => {
this.props.noteBucket.touch(noteId);
Expand Down
24 changes: 0 additions & 24 deletions lib/flux/app-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,6 @@ export const actionMap = new ActionMap({
return;
}

state.note.data = data;

dispatch(
this.action('loadAndSelectNote', {
noteBucket,
Expand Down Expand Up @@ -439,28 +437,6 @@ export const actionMap = new ActionMap({
},
},

updateNoteContent: {
creator({ noteBucket, note, content }) {
return (dispatch, getState) => {
if (note) {
note.data.content = content;
note.data.modificationDate = Math.floor(Date.now() / 1000);

// update the bucket don't sync right away
// as this happens per keystroke when the user is editing
// a note. The NoteEditor notify via props when its time
// to sync via Simperium
noteBucket.update(note.id, note.data, {}, { sync: false });

// Check if note is still selected (to avoid race conditions)
if (get(getState().appState, 'note.id') === note.id) {
dispatch(this.action('selectNote', { note }));
}
}
};
},
},

trashNote: {
creator({ noteBucket, note, previousIndex }) {
return dispatch => {
Expand Down
22 changes: 10 additions & 12 deletions lib/note-detail/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export class NoteDetail extends Component {
spellCheckEnabled: PropTypes.bool.isRequired,
storeFocusEditor: PropTypes.func,
storeHasFocus: PropTypes.func,
updateNoteContent: PropTypes.func.isRequired,
};

static defaultProps = {
Expand Down Expand Up @@ -130,25 +129,25 @@ export class NoteDetail extends Component {
hasFocus = () => this.editorHasFocus && this.editorHasFocus();

onPreviewClick = event => {
const { note, onChangeContent, syncNote } = this.props;

for (let node = event.target; node !== null; node = node.parentNode) {
// open markdown preview links in a new window
if (node.tagName === 'A') {
event.preventDefault();
viewExternalUrl(node.href);
break;
}

// handle task list items
if (node.className === 'task-list-item') {
event.preventDefault();
const { note, noteBucket, updateNoteContent } = this.props;
toggleTask({
taskNode: node,
text: note.data.content,
})
.then(newNoteContent => {
updateNoteContent({ noteBucket, note, content: newNoteContent });
})
.catch(console.log);
toggleTask({ taskNode: node, text: note.data.content }).then(
newContent => {
onChangeContent(note, newContent);
syncNote(note.id);
}
);
break;
}
}
Expand Down Expand Up @@ -258,11 +257,10 @@ const mapStateToProps = ({ appState: state, settings }) => ({
spellCheckEnabled: settings.spellCheckEnabled,
});

const { setShouldPrintNote, updateNoteContent } = appState.actionCreators;
const { setShouldPrintNote } = appState.actionCreators;

const mapDispatchToProps = {
onNotePrinted: () => setShouldPrintNote({ shouldPrint: false }),
updateNoteContent,
};

export default connect(
Expand Down