ConnectyCube / connectycube-reactnative-samples

Chat and Video Chat code samples for React Native, ConnectyCube
https://connectycube.com
Apache License 2.0
124 stars 111 forks source link

Messages history does not include new message, always 1 message behind. #174

Closed stephanoparaskeva closed 3 years ago

stephanoparaskeva commented 3 years ago

I am trying to build 1-to-1 chat, when user sends message, useMutation (from react-query) is fired. onSuccess, the chat history is refetched. But the new message is not there:

Working example:

  const getDialogHistory = () => dialog._id && ConnectyCube.chat.message.list(params);

  const history = useQuery<THistory>(['history.query', dialog._id], getDialogHistory);

  const MessageInput = () => {
    const [text, setText] = useState('');

    const send = async (msg: string) => {
      if (!msg) return;

      return cubeWrap(() => ({
        method: 'post',
        url: 'https://api.connectycube.com/chat/Message',
        data: {
          recipient_id: recipient?.cubeId,
          type: 3,
          markable: 1,
          message: msg,
          send_to_chat: 1,
        },
      }));
    };

    const msgMutation = useMutation(send, {
      onSuccess: (result: any) => {
        console.warn(result);
        history.refetch();
      },
    });

    return (
      <KeyboardAvoidingView behavior='padding'>
        <TouchableOpacity
          onPress={() => msgMutation.mutate(text)}
          style={styles.sendButton}>
          <CommonText style={styles.sendButtonText}>Send</CommonText>
        </TouchableOpacity>
        <TextInput onChangeText={setText} style={styles.textInput} />
      </KeyboardAvoidingView>
    );
  };

Below, is the message list. This is 1 message behind when you press send:

  const MessageList = () => (
    <FlatList
      data={history?.data?.items}
      keyExtractor={item => item?._id}
      renderItem={({ item }) => <Text>{item.message || ''}</Text>}
    />
  );
FriscPlusPlus commented 3 years ago

I also have the same issue. Did you find a solution?

I'm building the chat in OpenUI5 and when the onMessageListener events get fired I reload the model with the new data but the latest message is not there.

DaveLomber commented 3 years ago

@stephanoparaskeva

DaveLomber commented 3 years ago

@Friscas FYI