Commit 95824fb9 authored by Malsha Rathnasiri's avatar Malsha Rathnasiri

changes

parent 16228ea8
......@@ -88,6 +88,7 @@ export const login = async (username, password) => {
// Actions.main();
})
.catch(error => {
console.log('error in login authentication')
throw Error(error)
});
......
......@@ -13,12 +13,17 @@ const getHeaders = async () => {
var token = await AsyncStorage.getItem('access_token')
return {
Authorization: `JWT ${token}`, 'Content-Type': 'application/json',
// "Access-Control-Allow-Methods": "DELETE, POST, GET, OPTIONS",
// "Access-Control-Allow-Headers": "Content-Type, Authorization, Access-Control-Allow-Methods",
// "Access-Control-Allow-Origin": "*",
}
}
export const create = async (resource, values) => {
const headers = getHeaders()
const headers = await getHeaders()
console.log({ headers }, 'create')
console.log({values})
try {
return fetch(`${BACKEND_URL}/${resource}/`, { method: 'POST', body: JSON.stringify(values), headers })
// return Axios.post(`${BACKEND_URL}/${resource}/`, values)
......
// export const BACKEND_URL = "http://192.168.8.103:8000"
import { Platform } from 'react-native'
export const BACKEND_ADDRESS = Platform.OS == 'web' ? "127.0.0.1:8000" : "93e8-2401-dd00-10-20-e170-748c-b618-e8f5.in.ngrok.io"
export const BACKEND_ADDRESS = Platform.OS == 'web' ? "127.0.0.1:8000" : "0afe-112-134-223-169.ap.ngrok.io"
export const BACKEND_URL = `http://${BACKEND_ADDRESS}`
......@@ -15,6 +15,7 @@ import Slider from '@react-native-community/slider';
import { PlayMessage } from '../components/PlayMessage';
import { Toast } from 'native-base';
import { ERROR_TOAST_PROPS } from '../util/util';
import AsyncStorage from '@react-native-async-storage/async-storage';
export default function ChatScreen({ navigation }) {
......@@ -97,7 +98,20 @@ export default function ChatScreen({ navigation }) {
return res.json()
}).then(res => {
console.log(res)
setChatDetails(res)
AsyncStorage.getItem('user_id').then((user_id) => {
console.log('chat details', user_id)
//change from user and to user depending on the current user
if (res.from_user == user_id) {
setChatDetails(res)
}
else {
const n_res = { ...res, from_user: res.to_user, to_user: res.from_user }
setChatDetails(n_res)
}
})
})
}
......@@ -117,7 +131,7 @@ export default function ChatScreen({ navigation }) {
const onSendPress = () => {
try {
create('chats', { message: input, from_user: chatDetails.from_user, to_user: chatDetails.to_user, conversation: chatDetails.conversation_id }).then(response => {
create('chats', { message: input, from_user: chatDetails.from_user, to_user: chatDetails.to_user, conversation: chatDetails.id }).then(response => {
// console.log(response)
})
setLoading(true)
......@@ -151,7 +165,7 @@ export default function ChatScreen({ navigation }) {
inverted
sections={chats}
keyExtractor={(item, index) => item + index}
renderItem={({ item }) => {
// console.log({ item })
const timeString = new Date(item.timestamp).toLocaleTimeString()
......
......@@ -178,12 +178,17 @@ class MlModelViewSet(viewsets.ViewSet):
class ChatViewSet(viewsets.ModelViewSet):
queryset = Chat.objects.all().order_by('-timestamp')
serializer_class = ChatSerializer
permission_classes = [permissions.IsAuthenticated]
permission_classes = [permissions.IsAuthenticated] #change to permissions.isAutheniticated
@action(methods='POST', detail=True)
def getChats(self, request, *args, **kwargs):
chats = Chat.objects.filter(Q(from_user__user_id=request.user.id) | Q(
to_user__user_id=request.user.id)).order_by('-timestamp').values()
#conversation_id hardcoded as we dont have many conversations at the moment
chats = Chat.objects \
.filter(conversation_id=1) \
.filter(
(Q(from_user__user_id=request.user.id)
| Q(to_user__user_id=request.user.id))
).order_by('-timestamp').values()
return Response({chats})
......@@ -191,7 +196,9 @@ class ChatViewSet(viewsets.ModelViewSet):
if pk == None:
chats = Chat.objects.filter(Q(from_user_id=request.user.id) | Q(
chats = Chat.objects \
.filter(conversation_id=1) \
.filter(Q(from_user_id=request.user.id) | Q(
to_user_id=request.user.id))
else:
chats = Chat.objects.get(id=pk)
......
......@@ -46,13 +46,12 @@ INSTALLED_APPS = [
]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
......@@ -133,7 +132,7 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
'PAGE_SIZE': 100,
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication',
'rest_framework.authentication.SessionAuthentication',
......
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