diff --git a/eap_backend/eap_api/urls.py b/eap_backend/eap_api/urls.py index a44b6697..279c2739 100644 --- a/eap_backend/eap_api/urls.py +++ b/eap_backend/eap_api/urls.py @@ -22,10 +22,11 @@ name="property_claim_list", ), path( - "comments//", + "cases//comments/", views.comment_list, name="comment_list", ), + path("comments//", views.comment_detail, name="comment_detail"), path( "comments//reply/", views.reply_to_comment, diff --git a/eap_backend/eap_api/views.py b/eap_backend/eap_api/views.py index b53b377e..4901d305 100644 --- a/eap_backend/eap_api/views.py +++ b/eap_backend/eap_api/views.py @@ -641,12 +641,45 @@ def comment_list(request, assurance_case_id): return Response(serializer.data) elif request.method == "POST": - serializer = CommentSerializer(data=request.data) + data = request.data.copy() + data["assurance_case_id"] = ( + assurance_case_id # Ensure assurance_case_id is set in the data + ) + serializer = CommentSerializer(data=data) if serializer.is_valid(): # Ensure the author is set to the current user serializer.save(author=request.user) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + return JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + +@api_view(["GET", "PUT", "DELETE"]) +def comment_detail(request, pk): + """ + Retrieve, update or delete a specific comment. + """ + try: + comment = Comment.objects.get(id=pk) + except Comment.DoesNotExist: + return HttpResponse(status=404) + + if request.method == "GET": + serializer = CommentSerializer(comment) + return Response(serializer.data) + + elif request.method == "PUT": + serializer = CommentSerializer(comment, data=request.data, partial=True) + if serializer.is_valid(): + serializer.save() + return Response(serializer.data) + return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) + + elif request.method == "DELETE": + comment.delete() + return HttpResponse(status=204) + return JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST) diff --git a/next_frontend/components/cases/NotesEditForm.tsx b/next_frontend/components/cases/NotesEditForm.tsx new file mode 100644 index 00000000..a36fb463 --- /dev/null +++ b/next_frontend/components/cases/NotesEditForm.tsx @@ -0,0 +1,116 @@ +'use client' + +import React, { Dispatch, SetStateAction, useState } from 'react' +import { boolean, z } from "zod" +import { zodResolver } from "@hookform/resolvers/zod" +import { useForm } from "react-hook-form" +import { Button } from "@/components/ui/button" +import { + Form, + FormControl, + FormDescription, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form" +import { Textarea } from '../ui/textarea' +import { useLoginToken } from '@/hooks/useAuth' +import useStore from '@/data/store' + +type NotesEditFormProps = { + note: any, + setEdit: Dispatch> +} + +const formSchema = z.object({ + comment: z.string().min(2).max(50), +}) + +const NotesEditForm = ({ note, setEdit } : NotesEditFormProps ) => { + const [token] = useLoginToken(); + const { assuranceCase, setAssuranceCase } = useStore() + const [loading, setLoading] = useState(false) + + const { id, content } = note + + const form = useForm>({ + resolver: zodResolver(formSchema), + defaultValues: { + comment: content, + } + }) + + async function onSubmit(values: z.infer) { + setLoading(true) + + const newComment = { + content: values.comment + } + + try { + let url = `${process.env.NEXT_PUBLIC_API_URL}/api/comments/${id}/` + + const requestOptions: RequestInit = { + method: "PUT", + headers: { + Authorization: `Token ${token}`, + "Content-Type": "application/json", + }, + body: JSON.stringify(newComment), + }; + const response = await fetch(url, requestOptions); + + if(!response.ok) { + console.log('error') + } + + const updatedComment = await response.json(); + + // Find the index of the updated comment in the existing comments array + const updatedComments = assuranceCase.comments.map((comment:any) => + comment.id === updatedComment.id ? updatedComment : comment + ); + + const updatedAssuranceCase = { + ...assuranceCase, + comments: updatedComments, + }; + + setAssuranceCase(updatedAssuranceCase); + setEdit(false); + setLoading(false) + } catch (error) { + console.log('Error', error) + setLoading(false) + } + } + + return ( +
+ + ( + + {/* Username */} + +