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

changes

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