Skip to content

Commit 3fddf06

Browse files
author
Dan Carbonell
committed
add loading state for message send
1 parent 870c018 commit 3fddf06

File tree

4 files changed

+42
-27
lines changed

4 files changed

+42
-27
lines changed

examples/NativeMessaging/ios/Podfile.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ PODS:
5656
- React-cxxreact (= 0.60.5)
5757
- React-jsi (= 0.60.5)
5858
- React-jsinspector (0.60.5)
59-
- react-native-document-picker (3.3.3):
59+
- react-native-document-picker (3.5.2):
6060
- React
6161
- react-native-image-picker (1.1.0):
6262
- React
@@ -87,7 +87,7 @@ PODS:
8787
- React-Core (= 0.60.5)
8888
- RNGestureHandler (1.6.1):
8989
- React
90-
- RNReanimated (1.8.0):
90+
- RNReanimated (1.9.0):
9191
- React
9292
- yoga (0.60.5.React)
9393

@@ -189,7 +189,7 @@ SPEC CHECKSUMS:
189189
React-jsi: 4d8c9efb6312a9725b18d6fc818ffc103f60fec2
190190
React-jsiexecutor: 90ad2f9db09513fc763bc757fdc3c4ff8bde2a30
191191
React-jsinspector: e08662d1bf5b129a3d556eb9ea343a3f40353ae4
192-
react-native-document-picker: dd96ce05bf1453b110d7a3912097bf6d298d2cb6
192+
react-native-document-picker: 3026780fc04e7ad27534a0f35d5daefb550aa03a
193193
react-native-image-picker: 7a85cf7b0a53845f03ae52fb4592a2748ded069b
194194
react-native-netinfo: ddaca8bbb9e6e914b1a23787ccb879bc642931c9
195195
React-RCTActionSheet: b0f1ea83f4bf75fb966eae9bfc47b78c8d3efd90
@@ -203,9 +203,9 @@ SPEC CHECKSUMS:
203203
React-RCTVibration: 2105b2e0e2b66a6408fc69a46c8a7fb5b2fdade0
204204
React-RCTWebSocket: cd932a16b7214898b6b7f788c8bddb3637246ac4
205205
RNGestureHandler: 8f09cd560f8d533eb36da5a6c5a843af9f056b38
206-
RNReanimated: 955cf4068714003d2f1a6e2bae3fb1118f359aff
206+
RNReanimated: b5ccb50650ba06f6e749c7c329a1bc3ae0c88b43
207207
yoga: 312528f5bbbba37b4dcea5ef00e8b4033fdd9411
208208

209209
PODFILE CHECKSUM: 065a6a4c98f1b2dac9e014ba39d9e216c45e149f
210210

211-
COCOAPODS: 1.8.4
211+
COCOAPODS: 1.9.3

native-package/yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6266,10 +6266,10 @@ stream-browserify@^2.0.1:
62666266
inherits "~2.0.1"
62676267
readable-stream "^2.0.2"
62686268

6269-
6270-
version "0.12.2"
6271-
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-0.12.2.tgz#130f1416cff05dceb6bd6b0603f62dd954c91a0b"
6272-
integrity sha512-dgnUXgjNlA0WjmzsPHu0lDH/9blIv3JrwG9P6HpnSppEFddoK2lvq0Q7KrC+YTH1xBA68rYM9VT1Mgy2nqahQQ==
6269+
6270+
version "0.12.3"
6271+
resolved "https://registry.yarnpkg.com/stream-chat-react-native-core/-/stream-chat-react-native-core-0.12.3.tgz#d180d51a1661d578dbb6253791fa59dc23f71b6e"
6272+
integrity sha512-/uE95FKsct9jghHw29JVLVZxlq/IodQOjES29aIJh3XfqKyOS9MPtu/wKJpBPDGpxrr2Xc+H8XU0Ocs3UgjMZA==
62736273
dependencies:
62746274
"@babel/runtime" "^7.1.2"
62756275
"@stream-io/react-native-simple-markdown" "^1.1.3"

src/components/AutoCompleteInput.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class AutoCompleteInput extends React.PureComponent {
4141
super(props);
4242

4343
this.state = {
44-
text: props.value,
4544
selectionStart: 0,
4645
selectionEnd: 0,
4746
currentTrigger: null,
@@ -52,7 +51,6 @@ class AutoCompleteInput extends React.PureComponent {
5251

5352
componentDidUpdate(prevProps) {
5453
if (prevProps.value !== this.props.value) {
55-
this.setState({ text: this.props.value });
5654
this.handleChange(this.props.value, true);
5755
}
5856
}
@@ -77,7 +75,7 @@ class AutoCompleteInput extends React.PureComponent {
7775
const triggers = this.props.triggerSettings;
7876
await triggers[this.state.currentTrigger].dataProvider(
7977
q,
80-
this.state.text,
78+
this.props.value,
8179
(data, query) => {
8280
// Make sure that the result is still relevant for current query
8381
if (this.state.currentTokenForSuggestions !== query) {
@@ -109,8 +107,8 @@ class AutoCompleteInput extends React.PureComponent {
109107
};
110108

111109
onSelectSuggestion = (item) => {
112-
const { text, currentTrigger } = this.state;
113-
const { selectionEnd } = this.state;
110+
const { value: text } = this.props;
111+
const { currentTrigger, selectionEnd } = this.state;
114112
const triggers = this.props.triggerSettings;
115113
const newToken = triggers[currentTrigger].output(item);
116114
// const { onChange, trigger } = this.props;
@@ -218,7 +216,7 @@ class AutoCompleteInput extends React.PureComponent {
218216
handleSuggestions = (text) => {
219217
// react native is not consistent in order of execution of onSelectionChange and onTextChange
220218
// with android and iOS. onSelectionChange gets executed first on iOS (which is ideal for our scenario)
221-
// Although on android, this order is reveresed. So need to add following 0 timeout to make sure that
219+
// Although on android, this order is reversed. So need to add following 0 timeout to make sure that
222220
// onSelectionChange is executed first before we proceed with handleSuggestions.
223221
setTimeout(() => {
224222
const { selectionEnd: selectionEnd } = this.state;
@@ -236,7 +234,7 @@ class AutoCompleteInput extends React.PureComponent {
236234
};
237235

238236
render() {
239-
const { t } = this.props;
237+
const { t, value } = this.props;
240238

241239
return (
242240
<InputBox
@@ -245,7 +243,7 @@ class AutoCompleteInput extends React.PureComponent {
245243
onChangeText={(text) => {
246244
this.handleChange(text);
247245
}}
248-
value={this.state.text}
246+
value={value}
249247
onSelectionChange={this.handleSelectionChange}
250248
multiline
251249
{...this.props.additionalTextInputProps}

src/components/MessageInput.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class MessageInput extends PureComponent {
128128
props.editing,
129129
props.initialValue,
130130
);
131-
this.state = { ...state };
131+
this.state = { ...state, sending: false };
132132
}
133133

134134
static themePath = 'messageInput';
@@ -405,7 +405,14 @@ class MessageInput extends PureComponent {
405405
return false;
406406
};
407407

408-
sendMessage = () => {
408+
sendMessage = async () => {
409+
if (this.state.sending) return;
410+
411+
const { text } = this.state;
412+
await this.setState({ sending: true, text: '' }, () =>
413+
this.inputBox.clear(),
414+
);
415+
409416
const attachments = [];
410417
for (const id of this.state.imageOrder) {
411418
const image = this.state.imageUploads[id];
@@ -414,7 +421,7 @@ class MessageInput extends PureComponent {
414421
}
415422
if (image.state === FileState.UPLOADING) {
416423
// TODO: show error to user that they should wait until image is uploaded
417-
return;
424+
return this.setState({ sending: false });
418425
}
419426
attachments.push({
420427
type: 'image',
@@ -430,7 +437,7 @@ class MessageInput extends PureComponent {
430437
}
431438
if (upload.state === FileState.UPLOADING) {
432439
// TODO: show error to user that they should wait until image is uploaded
433-
return;
440+
return this.setState({ sending: false });
434441
}
435442
attachments.push({
436443
type: 'file',
@@ -442,12 +449,14 @@ class MessageInput extends PureComponent {
442449
}
443450

444451
// Disallow sending message if its empty.
445-
if (!this.state.text && attachments.length === 0) return;
452+
if (!text && attachments.length === 0) {
453+
return this.setState({ sending: false });
454+
}
446455

447456
if (this.props.editing) {
448457
const updatedMessage = { ...this.props.editing };
449458

450-
updatedMessage.text = this.state.text;
459+
updatedMessage.text = text;
451460
updatedMessage.attachments = attachments;
452461
updatedMessage.mentioned_users = this.state.mentioned_users.map(
453462
(mu) => mu.id,
@@ -459,24 +468,29 @@ class MessageInput extends PureComponent {
459468
.editMessage(updatedMessage)
460469
.then(this.props.clearEditingState);
461470
logChatPromiseExecution(updateMessagePromise, 'update message');
471+
472+
this.setState({ sending: false });
462473
} else {
463474
try {
464-
this.props.sendMessage({
465-
text: this.state.text,
475+
await this.props.sendMessage({
476+
text,
466477
parent: this.props.parent,
467478
mentioned_users: uniq(this.state.mentioned_users),
468479
attachments,
469480
});
481+
470482
this.setState({
471483
text: '',
472484
imageUploads: Immutable({}),
473485
imageOrder: Immutable([]),
474486
fileUploads: Immutable({}),
475487
fileOrder: Immutable([]),
476488
mentioned_users: [],
489+
sending: false,
477490
});
478491
} catch (err) {
479-
console.log('Fialed');
492+
this.setState({ sending: false, text });
493+
console.log('Failed');
480494
}
481495
}
482496
};
@@ -719,6 +733,7 @@ class MessageInput extends PureComponent {
719733
};
720734

721735
onChangeText = (text) => {
736+
if (this.state.sending) return;
722737
this.setState({ text });
723738

724739
if (text) {
@@ -898,7 +913,9 @@ class MessageInput extends PureComponent {
898913
title={t('Send message')}
899914
sendMessage={this.sendMessage}
900915
editing={this.props.editing}
901-
disabled={disabled || !this.isValidMessage()}
916+
disabled={
917+
disabled || this.state.sending || !this.isValidMessage()
918+
}
902919
/>
903920
</>
904921
)}

0 commit comments

Comments
 (0)