Commit 2a73b4d0 authored by Malsha Rathnasiri's avatar Malsha Rathnasiri

major changes

parent 888e41c8
...@@ -160,8 +160,10 @@ export default function ChatScreen({ navigation }) { ...@@ -160,8 +160,10 @@ export default function ChatScreen({ navigation }) {
useEffect(() => { useEffect(() => {
const interval = setInterval(() => { const interval = setInterval(() => {
if (chatDetails) {
loadChats(); loadChats();
}, 2000); }
}, 3000);
return () => { return () => {
clearInterval(interval); clearInterval(interval);
}; };
...@@ -171,7 +173,7 @@ export default function ChatScreen({ navigation }) { ...@@ -171,7 +173,7 @@ export default function ChatScreen({ navigation }) {
await getList("users") await getList("users")
.then((res) => res.json()) .then((res) => res.json())
.then((res) => { .then((res) => {
console.log({ users: res }); // console.log({ users: res });
setUsers(res.results); setUsers(res.results);
}); });
await getList("conversations") await getList("conversations")
...@@ -179,9 +181,9 @@ export default function ChatScreen({ navigation }) { ...@@ -179,9 +181,9 @@ export default function ChatScreen({ navigation }) {
return res.json(); return res.json();
}) })
.then((res) => { .then((res) => {
console.log({ res }); // console.log({ res });
AsyncStorage.getItem("user_id").then((user_id) => { AsyncStorage.getItem("user_id").then((user_id) => {
console.log("chat details", user_id); // console.log("chat details", user_id);
const converted = res.results.map((res) => { const converted = res.results.map((res) => {
if (res.from_user == user_id) { if (res.from_user == user_id) {
return res; return res;
...@@ -208,8 +210,8 @@ export default function ChatScreen({ navigation }) { ...@@ -208,8 +210,8 @@ export default function ChatScreen({ navigation }) {
}; };
const loadChats = () => { const loadChats = () => {
// setLoading(true); console.log({ chatDetails });
getList("chats", { conversation: chatDetails?.id }) getList("chats", { conversation: chatDetails ? chatDetails.id : 1 })
.then((res) => { .then((res) => {
return res.json(); return res.json();
}) })
......
// export const BACKEND_URL = "http://192.168.8.103:8000" // export const BACKEND_URL = "http://192.168.8.103:8000"
import { Platform } from 'react-native' import { Platform } from 'react-native'
export const BACKEND_ADDRESS = Platform.OS == 'web' ? "http://127.0.0.1:8000" : "https://21a1-112-134-220-172.ap.ngrok.io" export const BACKEND_ADDRESS = Platform.OS == 'web' ? "http://127.0.0.1:8000" : "https://d2e9-112-134-220-172.ap.ngrok.io"
export const BACKEND_URL = `${BACKEND_ADDRESS}` export const BACKEND_URL = `${BACKEND_ADDRESS}`
...@@ -164,8 +164,10 @@ export default function ChatScreen({ navigation }) { ...@@ -164,8 +164,10 @@ export default function ChatScreen({ navigation }) {
useEffect(() => { useEffect(() => {
const interval = setInterval(() => { const interval = setInterval(() => {
if (chatDetails) {
loadChats(); loadChats();
}, 2000); }
}, 3000);
return () => { return () => {
clearInterval(interval); clearInterval(interval);
}; };
...@@ -237,13 +239,14 @@ export default function ChatScreen({ navigation }) { ...@@ -237,13 +239,14 @@ export default function ChatScreen({ navigation }) {
}; };
const loadChats = () => { const loadChats = () => {
getList("chats", { conversation: chatDetails?.id }) console.log({ chatDetails });
getList("chats", { conversation: chatDetails ? chatDetails.id : 1 })
.then((res) => { .then((res) => {
return res.json(); return res.json();
}) })
.then((res) => { .then((res) => {
console.log("load chats"); console.log("load chats");
const chats = res.results || []; const chats = res.results.slice(-20) || [];
const sections = [ const sections = [
...new Set( ...new Set(
chats.map((chat) => new Date(chat.timestamp).setHours(0, 0, 0, 0)) chats.map((chat) => new Date(chat.timestamp).setHours(0, 0, 0, 0))
...@@ -256,7 +259,10 @@ export default function ChatScreen({ navigation }) { ...@@ -256,7 +259,10 @@ export default function ChatScreen({ navigation }) {
), ),
})); }));
setChats(sectionChats); setChats(sectionChats);
}).catch(e => {console.log(e)}); })
.catch((e) => {
console.log(e);
});
setLoading(false); setLoading(false);
}; };
...@@ -273,7 +279,7 @@ export default function ChatScreen({ navigation }) { ...@@ -273,7 +279,7 @@ export default function ChatScreen({ navigation }) {
}); });
setLoading(true); setLoading(true);
setInput(""); setInput("");
loadChats(); // loadChats();
} catch (e) { } catch (e) {
Toast.show({ Toast.show({
title: "Error sending message. try again!", title: "Error sending message. try again!",
......
...@@ -163,12 +163,16 @@ class ChatViewSet(viewsets.ModelViewSet): ...@@ -163,12 +163,16 @@ class ChatViewSet(viewsets.ModelViewSet):
conv_id = request.query_params.get("conversation") conv_id = request.query_params.get("conversation")
if pk == None: if pk == None:
chats = Chat.objects \ chats = Chat.objects \
.filter(conversation_id=int(conv_id)) \
.filter(Q(from_user_id=request.user.id) | Q( .filter(Q(from_user_id=request.user.id) | Q(
to_user_id=request.user.id)) to_user_id=request.user.id))
else: else:
chats = Chat.objects.get(id=pk) chats = Chat.objects.get(id=pk)
if conv_id:
chats = chats.filter(conversation_id=int(conv_id))
else:
chats = Chat.objects.none()
page = self.paginate_queryset(chats) page = self.paginate_queryset(chats)
if page is not None: if page is not None:
serializer = self.get_serializer(page, many=True) serializer = self.get_serializer(page, many=True)
......
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment