Store value before resetting text input (#4748)

This commit is contained in:
Miguel Alatzar 2020-08-28 11:15:08 -07:00 committed by GitHub
parent 537acefb2d
commit 2666c7c541
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 14 deletions

View file

@ -238,9 +238,12 @@ export default class PostDraft extends PureComponent {
);
};
doSubmitMessage = () => {
doSubmitMessage = (message = null) => {
const {createPost, currentUserId, channelId, files, handleClearFiles, rootId} = this.props;
const value = this.input.current?.getValue() || '';
let value = message;
if (!value) {
value = this.input.current?.getValue() || '';
}
const postFiles = files.filter((f) => !f.failed);
const post = {
user_id: currentUserId,
@ -329,10 +332,10 @@ export default class PostDraft extends PureComponent {
return;
}
const value = this.input.current.getValue();
this.input.current.resetTextInput();
requestAnimationFrame(() => {
const value = this.input.current.getValue();
if (!this.isSendButtonEnabled()) {
this.input.current.setValue(value);
return;
@ -373,12 +376,12 @@ export default class PostDraft extends PureComponent {
onPress: () => {
// Remove only failed files
handleClearFailedFiles(channelId, rootId);
this.sendMessage();
this.sendMessage(value);
},
}],
);
} else {
this.sendMessage();
this.sendMessage(value);
}
});
};
@ -487,8 +490,7 @@ export default class PostDraft extends PureComponent {
return {groupMentionsSet, memberNotifyCount, channelTimezoneCount};
}
sendMessage = () => {
const value = this.input.current?.getValue() || '';
sendMessage = (value = '') => {
const {enableConfirmNotificationsToChannel, membersCount, useGroupMentions, useChannelMentions} = this.props;
const notificationsToChannel = enableConfirmNotificationsToChannel && useChannelMentions;
const notificationsToGroups = enableConfirmNotificationsToChannel && useGroupMentions;
@ -504,10 +506,10 @@ export default class PostDraft extends PureComponent {
if (memberNotifyCount > 0) {
this.showSendToGroupsAlert(Array.from(groupMentionsSet), memberNotifyCount, channelTimezoneCount, value);
} else {
this.doSubmitMessage();
this.doSubmitMessage(value);
}
} else {
this.doSubmitMessage();
this.doSubmitMessage(value);
}
};

View file

@ -13,6 +13,8 @@ jest.mock('react-native-image-picker', () => ({
launchCamera: jest.fn(),
}));
jest.useFakeTimers();
describe('PostDraft', () => {
const baseProps = {
addReactionToLatestPost: jest.fn(),
@ -95,10 +97,12 @@ describe('PostDraft', () => {
getValue: () => message,
setValue: jest.fn(),
changeDraft: jest.fn(),
resetTextInput: jest.fn(),
},
};
instance.sendMessage();
instance.handleSendMessage();
jest.runOnlyPendingTimers();
expect(Alert.alert).toBeCalled();
expect(Alert.alert).toHaveBeenCalledWith('Confirm sending notifications to entire channel', expect.anything(), expect.anything());
});
@ -118,9 +122,11 @@ describe('PostDraft', () => {
getValue: () => message,
setValue: jest.fn(),
changeDraft: jest.fn(),
resetTextInput: jest.fn(),
},
};
instance.sendMessage();
instance.handleSendMessage();
jest.runOnlyPendingTimers();
expect(Alert.alert).toBeCalled();
});
@ -139,10 +145,12 @@ describe('PostDraft', () => {
getValue: () => message,
setValue: jest.fn(),
changeDraft: jest.fn(),
resetTextInput: jest.fn(),
},
};
instance.sendMessage();
instance.handleSendMessage();
jest.runOnlyPendingTimers();
expect(Alert.alert).not.toBeCalled();
});
@ -162,10 +170,12 @@ describe('PostDraft', () => {
getValue: () => message,
setValue: jest.fn(),
changeDraft: jest.fn(),
resetTextInput: jest.fn(),
},
};
instance.sendMessage();
instance.handleSendMessage();
jest.runOnlyPendingTimers();
expect(Alert.alert).not.toHaveBeenCalled();
});
@ -185,10 +195,12 @@ describe('PostDraft', () => {
getValue: () => message,
setValue: jest.fn(),
changeDraft: jest.fn(),
resetTextInput: jest.fn(),
},
};
instance.sendMessage();
instance.handleSendMessage();
jest.runOnlyPendingTimers();
expect(Alert.alert).not.toHaveBeenCalled();
});