[HOLD for payment 2021-12-15] [$250] Edit comment - Message being edited is cancelled on pressing any key - Reported by: @parasharrajat
If you haven’t already, check out our contributing guidelines for onboarding and email [email protected] to request to join our Slack channel!
Action Performed:
- Navigate to a conversation
- Send a message
- Edit the message
- Clear the message in the edit box
- Wait a few seconds
- Start writing something
Expected Result:
Text should be displayed after user starts typing.
Actual Result:
The edit comment box is cancelled.
Workaround:
Unknown.
Platform:
Where is this issue occurring?
- Web
Version Number: 1.1.12-0
Reproducible in staging?: Yes Reproducible in production?: Yes
Logs: https://stackoverflow.com/c/expensify/questions/4856
Notes/Photos/Videos: Any additional supporting documentation
https://user-images.githubusercontent.com/44479856/139748311-39b12f45-23ef-4494-8436-be3c8f9166f2.mp4
Upwork job https://www.upwork.com/jobs/~013a8cf464cdc1cf31
Issue reported by: @parasharrajat Slack conversation: https://expensify.slack.com/archives/C01GTK53T8Q/p1635692466285700
Triggered auto assignment to @TomatoToaster (Engineering), see https://stackoverflow.com/c/expensify/questions/4319 for more details.
Good find @parasharrajat, this is a weird bug. Dropping the External label on this one, and just letting you know in case you had a fix in mind.
Triggered auto assignment to @michaelhaxhiu (External), see https://stackoverflow.com/c/expensify/questions/8582 for more details.
Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!
Easily reproducible, this one is a nice find. Posted to upwork. @parasharrajat Feel free to make a solution proposal if you want to solve it. Otherwise, this is open to all.
RE: upwork proposal - https://www.upwork.com/jobs/~013a8cf464cdc1cf31
It sounds like there is an event handler in the application that's overriding the desired behavior for the text field. If it's occurring in the larger application context, then perhaps some state must be maintained so that the event handler is ignored when the text field is in focus. Either way, the event handling will have to be altered so that key events are ignored while the text field is in focus, somehow, in a way that fits with the app nicely.
Best regards, John
Triggered auto assignment to @thienlnam (Exported), see https://stackoverflow.com/c/expensify/questions/7972 for more details.
We are placing all non-critical issues on hold while we're on a company offsite. The hold is expected to be lifted on 11/19 (but could go longer). For any PRs that are submitted before or during the hold, we will add a $250 bonus payment.
Upwork Proposal - https://www.upwork.com/jobs/~013a8cf464cdc1cf31
I detected the issue. When you delete content of textInput, draft is saved to localStorage as empty string. This causes ReportActionItem to render ReportActionItemMessage instead of ReportActionItemMessageEdit.
An if statement that checks length of draft before setting it to localStorage will do it.
@opal6502 Thanks for your proposal - this is a pretty broad outline but doesn't really encapsulate why the issue is happening in this specific context or the specific changes that need to be made. The proposal is more like a series of steps that you will take to solve the issue and the PR just follows the changes in your proposal
@kursat You seem to be on the right track here, but checking the length of the draft wouldn't allow you to set it to an empty string. Is there another solution here that could address that?
Hey @thienlnam, we can solve this in two ways.
1. We continue to keep draftMessage as string
In this scenario, user will see the original message, after setting text area to empty string and refreshing the page.
Steps:
- We should update
debouncedSaveDraftfunction to use its parameter. Right now it doesn't usenewDraftparameter we send fromupdateDraft.
/**
* 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(newDraft) {
saveReportActionDraft(this.props.reportID, this.props.action.reportActionID, newDraft);
}
- We should change
updateDraftto check draft length and if it's empty set original message to onyx.
/**
* Update the value of the draft in Onyx
*
* @param {String} newDraft
*/
updateDraft(newDraft) {
this.textInput.setNativeProps({text: newDraft});
this.setState({draft: newDraft});
if (newDraft.trim().length > 0) {
this.debouncedSaveDraft(newDraft);
} else {
this.debouncedSaveDraft(this.props.action.originalMessage.html);
}
}
- We should update binding of
debouncedSaveDraftat constructor, because when immediate parameter set, it fires at the beginning of the sequence. In short it doesn't take last changes.
this.debouncedSaveDraft = _.debounce(this.debouncedSaveDraft.bind(this), 1000);
- Lastly we need to cancel debounce when we are publishing the draft. If we don't, it leads to another bug that opens edit box after save before debounce duration (1s).
/**
* 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() {
this.debouncedSaveDraft.cancel();
const trimmedNewDraft = this.state.draft.trim();
editReportComment(this.props.reportID, this.props.action, trimmedNewDraft);
this.deleteDraft();
}
2. We change draftMessage to object format
In this scenario, user will be seeing an empty textarea, after removing text and refreshing page.
To accomplish this, we need to keep draftMessage in a format similar to this:
{
text: '',
active: true,
}
Steps:
- In
ContextMenuActions,ReportActionComposeandReportActionItemMessageEditfiles, we need to updatesaveReportActionDraftfunction calls
const editAction = () => saveReportActionDraft(
reportID,
reportAction.reportActionID,
{
text: _.isEmpty(draftMessage.text) ? getActionText(reportAction) : '',
active: true,
},
);
saveReportActionDraft(this.props.reportID, reportActionID, {text: _.last(message).html, active: true});
- In
ReportActionItem, we need to update all draftMessage checks tothis.props.draftMessage.active. Eg:
shouldComponentUpdate(nextProps, nextState) {
return this.props.displayAsGroup !== nextProps.displayAsGroup
|| !_.isEqual(this.props.draftMessage, nextProps.draftMessage)
|| this.props.isMostRecentIOUReportAction !== nextProps.isMostRecentIOUReportAction
|| this.props.hasOutstandingIOU !== nextProps.hasOutstandingIOU
|| this.props.shouldDisplayNewIndicator !== nextProps.shouldDisplayNewIndicator
|| !_.isEqual(this.props.action, nextProps.action)
|| this.state.isContextMenuActive !== nextState.isContextMenuActive;
}
preventDefaultContentMenu={!this.props.draftMessage.active}
style={getReportActionItemStyle(
hovered
|| this.state.isContextMenuActive
|| this.props.draftMessage.active,
)}
- In
ReportActionItemMessageEditcomponent, we should create draftMessage (state) with following line:
const draftMessage = parser.htmlToMarkdown(this.props.draftMessage.text);
- Since third and fourth tasks from previous option are bug-fix, we need to do them in this scenario too.
Note: Number 1, 2 has more changes, I wanted to keep it clean, so didn't write all of them. If you want to see, I can create a fork, and share link with all changes.
Oops. My proposal got removed somehow... so posting it again. It does seem like that the above proposal's 1 match my proposal but I am not sure how my proposal got removed earlier.
Proposal
We need to discuss this first. This issue happens due to the reason that we update the draft message as user types and the existence of a draft message for action determines that a message is being edited.
So When the user clears a message, the related draft will be set to ' ' empty string which is equal to the report is not being edited. As soon as the user presses a key, the component re-renders, and the edit action is cleared.
Now, this is impossible to keep the real-time drafts for actions and show the edited action when the draft becomes empty ' '. This is the limitation of the current implementation.
I will explain my approach step by step:
- We skip saving the draft message to empty
' 'by putting a check inupdateDraft. https://github.com/Expensify/App/blob/b12eb5f9f18ba6578790b55ad248b682724f77ae/src/pages/home/report/ReportActionItemMessageEdit.js#L84
if (newDraft !== '') {
this.debouncedSaveDraft(newDraft);
}
- Fix up the
debouncedSaveDraftso that it takes the draft as a parameter instead of from state. - Now as a side-effect when we switch the chats or refresh the chat, the last typed char (before clearing) will be shown as a draft instead of the last edit
' '. An empty draft' 'is not really a draft so I suggest that when the user clears a message, instead of saving the' 'Empty draft (which we fixed in step 1), save the full message (of action) so that when the user refreshes the chat, the full message is shown as a draft. - When we save the full message as a draft instead of Empty
' ', it does not update the edit box in the UI. So the user will be shown his input which is' 'and he can continue typing the new message and it will replace the draft in storage as normal. Only when he changes the chat or refreshes the page after clearing the message on the action, he will see the full message string (which makes absolute sense). - One last step we need to update the debouncing technique here https://github.com/Expensify/App/blob/b12eb5f9f18ba6578790b55ad248b682724f77ae/src/pages/home/report/ReportActionItemMessageEdit.js#L50
by setting the third parameter to
false. Otherwise, it skips the last edit.
@parasharrajat, I have seen your proposal, too. Looks like mentioning #6283 replaced your proposal with that link.
Looks like Step 4 on my proposal required for your proposal too, in order not to cause another bug.
Thank you @kursat and @parasharrajat, this is a little difficult since I don't see your original proposal. However, since @kursat did a great investigation into the issue and additionally provided extra details needed for implementation I will continue to have them implement the changes (the first proposal of continuing to keep draftMessage as string).
Sorry about that @parasharrajat, we will still of course pay out the bounty for reporting the bug
@michaelhaxhiu Could you please hire @kursat in Upwork?
Yep! @kursat can you please apply for the Upwork job here - https://www.upwork.com/jobs/~013a8cf464cdc1cf31 ?
Also @parasharrajat can you apply for this same upwork job, so we can disburse payment for reporting the bug when it's implemented? I'll hire you both on this 1 job essentially.
Done. @michaelhaxhiu
@michaelhaxhiu, I've applied too.