Skip to content

Commit

Permalink
Basic chat session created
Browse files Browse the repository at this point in the history
  • Loading branch information
bhujoshi committed Mar 11, 2024
1 parent fb2083f commit f7921fb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
5 changes: 5 additions & 0 deletions network/chat_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ export const getChatSession = async (chatSessionId, lastMessageId) => {
`chat_sessions/${chatSessionId}/new_messages?last_message_id=${lastMessageId}&format=json`
);
};

export const followUpChatSession = async (id, chatSession) => {
const axiosClient = await getAxiosClient();
return axiosClient.patch(`chat_sessions/${id}.json`, chatSession);
};
21 changes: 11 additions & 10 deletions screens/ChatSessionScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useState, useEffect } from 'react';
import { material } from 'react-native-typography';
import { logEvent } from '../libs/Helpers';
import { useTranslation } from 'react-i18next';
import { createChatSession, getChatSession } from '../network/chat_session';
import { createChatSession, followUpChatSession, getChatSession } from '../network/chat_session';
import { retrieveUserSession } from '../libs/EncryptedStoreage';
import ChatCard from '../components/ChatCard';

Expand Down Expand Up @@ -35,20 +35,21 @@ const ChatSessionScreen = ({ navigation }) => {
}
};
try {
const chatSession = await createChatSession(message);
setMessages([...messages, ...chatSession.data.messages]);
let chatSession;
if (messages.length === 0) {
chatSession = await createChatSession(message);
} else {
chatSession = await followUpChatSession(messages[0].chat_session_id, message);
}
const latestMessage = chatSession.data.messages[chatSession.data.messages.length - 1];
setMessages([...messages, latestMessage]);
const interval = setInterval(async () => {
const messageResponse = await getChatSession(
chatSession.data.id,
chatSession.data.messages[0].id
);
const messageResponse = await getChatSession(chatSession.data.id, latestMessage.id);
if (messageResponse.data.length > 0) {
console.log('messageResponse', messageResponse);
clearInterval(interval);
setMessages((messages) => [...messages, ...messageResponse.data]);
}
}, 1000);
// setMessages([...messages, newMessage]);
}, 2000);
setInputText('');
} catch (error) {
console.error('Error sending message', error);
Expand Down

0 comments on commit f7921fb

Please sign in to comment.