From 58f3c770de9e52d1f8531720b2279ea13eb594e0 Mon Sep 17 00:00:00 2001 From: Richard Griffiths Date: Thu, 23 Jan 2025 13:27:29 +0000 Subject: [PATCH] UI updates --- .../components/cases/NodeAttributes.tsx | 169 ++++++++++++++++++ next_frontend/components/common/EditForm.tsx | 52 ------ next_frontend/components/common/NodeEdit.tsx | 14 +- 3 files changed, 181 insertions(+), 54 deletions(-) create mode 100644 next_frontend/components/cases/NodeAttributes.tsx diff --git a/next_frontend/components/cases/NodeAttributes.tsx b/next_frontend/components/cases/NodeAttributes.tsx new file mode 100644 index 00000000..d96dea6e --- /dev/null +++ b/next_frontend/components/cases/NodeAttributes.tsx @@ -0,0 +1,169 @@ +'use client' + +import React, { Dispatch, SetStateAction, useState } from 'react' +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form" +import { z } from "zod" +import { zodResolver } from "@hookform/resolvers/zod" +import { useForm } from "react-hook-form" +import { Textarea } from "../ui/textarea" +import { Button } from '../ui/button' +import useStore from '@/data/store'; +import { updateAssuranceCase, updateAssuranceCaseNode } from '@/lib/case-helper' +import { useSession } from 'next-auth/react' +import { MinusIcon, PlusIcon } from 'lucide-react' + +const formSchema = z.object({ + assumption: z.string().optional(), + justification: z.string().optional() +}) + +interface NodeAttributesProps { + node: any; + actions: any + onClose: () => void + setUnresolvedChanges: Dispatch> +}; + +const NodeAttributes: React.FC = ({ + node, + actions, + onClose, + setUnresolvedChanges +}) => { + const { assuranceCase, setAssuranceCase } = useStore(); + const { data: session } = useSession() + const [loading, setLoading] = useState(false) + const [newAssumption, setNewAssumption] = useState(false) + const [newJustification, setNewJustification] = useState(false) + + const { setSelectedLink, setAction } = actions + + const reset = () => { + setSelectedLink(false) + setAction('') + } + + const handleCancel = () => { + form.reset(); // Reset the form state + reset(); // Perform additional reset actions + }; + + const form = useForm>({ + resolver: zodResolver(formSchema), + defaultValues: node.data || { + assumption: '', + justification: '' + } + }); + + async function onSubmit(values: z.infer) { + setLoading(true) + // Update item via api + const updateItem = { + assumption: values.assumption, + justification: values.justification, + } + + const updated = await updateAssuranceCaseNode(node.type, node.data.id, session?.key ?? '', updateItem) + + if(updated) { + // Assurance Case Update + const updatedAssuranceCase = await updateAssuranceCase(node.type, assuranceCase, updateItem, node.data.id, node) + if(updatedAssuranceCase) { + setAssuranceCase(updatedAssuranceCase) + setLoading(false) + onClose() + } + } + } + + let readOnly = (assuranceCase.permissions === 'view' || assuranceCase.permissions === 'review') ? true : false + + // useEffect(() => { + // form.watch((values, { name }) => { + // if (name === 'assumption') { + // setUnresolvedChanges(true); + // } + // }); + // }, [form.watch, setUnresolvedChanges]); + + return ( +
+
+ Please use this section to manage attributes for this element. +
+ +
+ {!node.data.assumption && ( + + )} + {!node.data.justification && node.type === 'strategy' && ( + + )} +
+ +
+ + {(node.data.assumption || newAssumption) && ( + ( + + Assumption + +