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

major changes

parent 888e41c8
......@@ -160,8 +160,10 @@ export default function ChatScreen({ navigation }) {
useEffect(() => {
const interval = setInterval(() => {
loadChats();
}, 2000);
if (chatDetails) {
loadChats();
}
}, 3000);
return () => {
clearInterval(interval);
};
......@@ -171,7 +173,7 @@ export default function ChatScreen({ navigation }) {
await getList("users")
.then((res) => res.json())
.then((res) => {
console.log({ users: res });
// console.log({ users: res });
setUsers(res.results);
});
await getList("conversations")
......@@ -179,9 +181,9 @@ export default function ChatScreen({ navigation }) {
return res.json();
})
.then((res) => {
console.log({ res });
// console.log({ res });
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) => {
if (res.from_user == user_id) {
return res;
......@@ -208,8 +210,8 @@ export default function ChatScreen({ navigation }) {
};
const loadChats = () => {
// setLoading(true);
getList("chats", { conversation: chatDetails?.id })
console.log({ chatDetails });
getList("chats", { conversation: chatDetails ? chatDetails.id : 1 })
.then((res) => {
return res.json();
})
......
// export const BACKEND_URL = "http://192.168.8.103:8000"
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}`
......@@ -164,8 +164,10 @@ export default function ChatScreen({ navigation }) {
useEffect(() => {
const interval = setInterval(() => {
loadChats();
}, 2000);
if (chatDetails) {
loadChats();
}
}, 3000);
return () => {
clearInterval(interval);
};
......@@ -237,13 +239,14 @@ export default function ChatScreen({ navigation }) {
};
const loadChats = () => {
getList("chats", { conversation: chatDetails?.id })
console.log({ chatDetails });
getList("chats", { conversation: chatDetails ? chatDetails.id : 1 })
.then((res) => {
return res.json();
})
.then((res) => {
console.log("load chats");
const chats = res.results || [];
const chats = res.results.slice(-20) || [];
const sections = [
...new Set(
chats.map((chat) => new Date(chat.timestamp).setHours(0, 0, 0, 0))
......@@ -256,7 +259,10 @@ export default function ChatScreen({ navigation }) {
),
}));
setChats(sectionChats);
}).catch(e => {console.log(e)});
})
.catch((e) => {
console.log(e);
});
setLoading(false);
};
......@@ -273,7 +279,7 @@ export default function ChatScreen({ navigation }) {
});
setLoading(true);
setInput("");
loadChats();
// loadChats();
} catch (e) {
Toast.show({
title: "Error sending message. try again!",
......
......@@ -163,12 +163,16 @@ class ChatViewSet(viewsets.ModelViewSet):
conv_id = request.query_params.get("conversation")
if pk == None:
chats = Chat.objects \
.filter(conversation_id=int(conv_id)) \
.filter(Q(from_user_id=request.user.id) | Q(
to_user_id=request.user.id))
else:
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)
if page is not None:
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