-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Note edit and delete #507
Note edit and delete #507
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that we're out of "crunch-mode", please at least at some unit tests for the new backend code. At least at the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cptanalatriste this is where I was hoping you'd be able to help There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @RichGriff of course! I can add a couple of tests to the branch |
||
""" | ||
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) | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<SetStateAction<boolean | undefined>> | ||
} | ||
|
||
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<boolean>(false) | ||
|
||
const { id, content } = note | ||
|
||
const form = useForm<z.infer<typeof formSchema>>({ | ||
resolver: zodResolver(formSchema), | ||
defaultValues: { | ||
comment: content, | ||
} | ||
}) | ||
|
||
async function onSubmit(values: z.infer<typeof formSchema>) { | ||
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 ( | ||
<Form {...form}> | ||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> | ||
<FormField | ||
control={form.control} | ||
name="comment" | ||
render={({ field }) => ( | ||
<FormItem> | ||
{/* <FormLabel>Username</FormLabel> */} | ||
<FormControl> | ||
<Textarea placeholder="Type your message here." {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<div className='flex justify-end items-center gap-2'> | ||
<Button variant={'ghost'} onClick={() => setEdit(false)}>Cancel</Button> | ||
<Button type="submit" disabled={loading}> | ||
{loading ? 'Saving' : 'Save'} | ||
</Button> | ||
</div> | ||
</form> | ||
</Form> | ||
) | ||
} | ||
|
||
export default NotesEditForm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, this shouldn't be needed. Aren't you already sending the assurance case ID in the payload body?