Skip to content
Merged
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
21 changes: 17 additions & 4 deletions src/pages/home/report/ReportActionItemMessageEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ReportActionItemMessageEdit extends React.Component {
super(props);
this.updateDraft = this.updateDraft.bind(this);
this.deleteDraft = this.deleteDraft.bind(this);
this.debouncedSaveDraft = _.debounce(this.debouncedSaveDraft.bind(this), 1000, true);
this.debouncedSaveDraft = _.debounce(this.debouncedSaveDraft.bind(this), 1000);
this.publishDraft = this.publishDraft.bind(this);
this.triggerSaveOrCancel = this.triggerSaveOrCancel.bind(this);
this.onSelectionChange = this.onSelectionChange.bind(this);
Expand Down Expand Up @@ -81,7 +81,14 @@ class ReportActionItemMessageEdit extends React.Component {
updateDraft(newDraft) {
this.textInput.setNativeProps({text: newDraft});
this.setState({draft: newDraft});
this.debouncedSaveDraft(newDraft);

// This component is rendered only when draft is set to a non-empty string. In order to prevent component
// unmount when user deletes content of textarea, we set previous message instead of empty string.
if (newDraft.trim().length > 0) {
Comment thread
kursat marked this conversation as resolved.
Resolved
this.debouncedSaveDraft(newDraft);
} else {
this.debouncedSaveDraft(this.props.action.message[0].html);
}
}

/**
Expand All @@ -96,16 +103,22 @@ class ReportActionItemMessageEdit extends React.Component {
/**
* Save the draft of the comment. This debounced so that we're not ceaselessly saving your edit. Saving the draft
* allows one to navigate somewhere else and come back to the comment and still have it in edit mode.
* @param {String} newDraft
*/
debouncedSaveDraft() {
Report.saveReportActionDraft(this.props.reportID, this.props.action.reportActionID, this.state.draft);

debouncedSaveDraft(newDraft) {
Report.saveReportActionDraft(this.props.reportID, this.props.action.reportActionID, newDraft);
}

/**
* Save the draft of the comment to be the new comment message. This will take the comment out of "edit mode" with
* the new content.
*/
publishDraft() {
// To prevent re-mount after user saves edit before debounce duration (example: within 1 second), we cancel
// debounce here.
this.debouncedSaveDraft.cancel();
Comment thread
kursat marked this conversation as resolved.
Resolved

const trimmedNewDraft = this.state.draft.trim();
Report.editReportComment(this.props.reportID, this.props.action, trimmedNewDraft);
this.deleteDraft();
Expand Down