Commit f40a894b authored by Malsha Rathnasiri's avatar Malsha Rathnasiri

fix chat send

parent a9219204
......@@ -20,6 +20,19 @@ const getHeaders = async () => {
}
}
export const addChats = async (values) => {
const headers = await getHeaders()
console.log({ headers }, 'create')
console.log({values})
try {
return fetch(`${BACKEND_URL}/chats/add_chats/`, { method: 'POST', body: JSON.stringify(values), headers })
// return Axios.post(`${BACKEND_URL}/${resource}/`, values)
}
catch (e) {
console.log(e)
}
}
export const create = async (resource, values) => {
const headers = await getHeaders()
console.log({ headers }, 'create')
......
// 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://438c-2401-dd00-10-20-ccb8-3726-acbd-ade4.ap.ngrok.io"
export const BACKEND_ADDRESS = Platform.OS == 'web' ? "http://127.0.0.1:8000" : "https://cbc3-2401-dd00-10-20-7542-a875-30e7-8931.ap.ngrok.io"
export const BACKEND_URL = `${BACKEND_ADDRESS}`
......@@ -6,7 +6,7 @@ import _ from 'lodash'
import EditScreenInfo from '../components/EditScreenInfo';
// import { Text, View } from '../components/Themed';
import { RootTabScreenProps } from '../types';
import { create, getList, getOne } from '../api/api';
import { addChats, create, getList, getOne } from '../api/api';
import { BACKEND_ADDRESS } from '../api/constants';
import Ionicons from '@expo/vector-icons/Ionicons';
import { CONVO_DEFAULT_ICON_COLOR, styles } from '../util/styles';
......@@ -132,7 +132,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.id }).then(response => {
addChats({message: input, from_user: chatDetails.from_user, to_user: chatDetails.to_user, conversation: chatDetails.id }).then(response => {
// console.log(response)
})
setLoading(true)
......
from sre_constants import SUCCESS
from tkinter import N
from django.contrib.auth.models import User, Group
from grpc import Status
from rest_framework import viewsets
from rest_framework import permissions
from backend.cms.serializers import MyTokenObtainPairSerializer
......@@ -9,6 +12,7 @@ from rest_framework.decorators import action
from rest_framework.response import Response
import os
from rest_framework.parsers import MultiPartParser
from rest_framework import serializers
from datetime import datetime, timedelta
......@@ -176,6 +180,28 @@ class ChatViewSet(viewsets.ModelViewSet):
return result_set
@action(methods=['POST'], detail=False)
def send_chat(self, request, pk=None):
try:
print(request.data)
return Response({'success': True})
except(e):
return Response({'success': False})
@action(methods=['POST'], detail=False)
def add_chats(self, request):
item = ChatSerializer(data=request.data)
# validating for already existing data
if Chat.objects.filter(**request.data).exists():
raise serializers.ValidationError('This data already exists')
if item.is_valid():
item.save()
return Response(item.data)
else:
return Response(status=Status.HTTP_404_NOT_FOUND)
class ConversationViewSet(viewsets.ModelViewSet):
queryset = Conversation.objects.all().order_by('id')
serializer_class = ConversationSerializer
......
No preview for this file type
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