From 9374ad53e305e105ff84115473bf3ba231ee4c95 Mon Sep 17 00:00:00 2001 From: Drew Rautenberg Date: Sat, 18 Jul 2026 18:46:53 -0500 Subject: [PATCH] Added edit customer detail page --- barkmanui/src/App.tsx | 2 + .../src/features/customers/CustomerDetail.tsx | 2 +- .../src/features/customers/EditCustomer.tsx | 134 ++++++++++++++++++ 3 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 barkmanui/src/features/customers/EditCustomer.tsx diff --git a/barkmanui/src/App.tsx b/barkmanui/src/App.tsx index 809d0c9..16fcc53 100644 --- a/barkmanui/src/App.tsx +++ b/barkmanui/src/App.tsx @@ -13,6 +13,7 @@ import '@mantine/notifications/styles.css'; import CustomerList from "@/features/customers/CustomerList.tsx"; import AddCustomer from "@/features/customers/AddCustomer.tsx"; import CustomerDetail from "@/features/customers/CustomerDetail.tsx"; +import EditCustomer from "@/features/customers/EditCustomer.tsx"; // Create a client const queryClient = new QueryClient() @@ -52,6 +53,7 @@ function App() { }/> }/> }/> + }/> diff --git a/barkmanui/src/features/customers/CustomerDetail.tsx b/barkmanui/src/features/customers/CustomerDetail.tsx index 7933492..5d1d297 100644 --- a/barkmanui/src/features/customers/CustomerDetail.tsx +++ b/barkmanui/src/features/customers/CustomerDetail.tsx @@ -36,7 +36,7 @@ function CustomerDetail() { Address: ${data.address} Notes: {data.notes} - Edit + Edit ) diff --git a/barkmanui/src/features/customers/EditCustomer.tsx b/barkmanui/src/features/customers/EditCustomer.tsx new file mode 100644 index 0000000..f1cb229 --- /dev/null +++ b/barkmanui/src/features/customers/EditCustomer.tsx @@ -0,0 +1,134 @@ +import {Button, Group, TextInput, Container, Title, Flex} from '@mantine/core'; +import {useForm} from '@mantine/form'; +import {useNavigate, useParams} from "react-router"; +import {useMutation, useQuery, useQueryClient} from "@tanstack/react-query"; +import {Customer} from "./types.ts"; +import {useEffect} from "react"; +import {notifications} from "@mantine/notifications"; +import {IconCheck, IconX} from "@tabler/icons-react"; + +type EditableCustomer= Omit; + +function EditCustomer() { + const params = useParams(); + const navigate = useNavigate(); + + const editCustomerForm = useForm({ + mode: 'uncontrolled', + initialValues: { + name: "", + company: "", + email: "", + phone: "", + address: "", + billingTerms: "", + notes: "", + }, + + validate: {}, + }); + + const {isPending, error, data, isFetching} = useQuery({ + queryKey: ['customer', params.customerId], + queryFn: async (): Promise => { + const response = await fetch( + import.meta.env.VITE_API_URL + '/customers/' + params.customerId, + ) + + if (!response.ok) throw new Error('Failed to fetch customer ' + response.statusText) + + return await response.json() + }, + }); + + const queryClient = useQueryClient(); + + const updateCustomer = useMutation({ + mutationFn: async (values: EditableCustomer) => { + + // await + const result = await fetch(import.meta.env.VITE_API_URL + '/customers/' + params.customerId, { + method: 'PUT', + body: JSON.stringify(values), + headers: { + 'Content-Type': 'application/json' + } + }); + + if (result.ok) { + notifications.show({ + icon: , + color:"teal", + title: "All good!", + message: "Customer Updated", + position: 'top-center', + }); + navigate("/customers"); + + } + + if (!result.ok) { + notifications.show({ + icon: , + color:"red", + title: "Bummer!", + message: "Something went wrong", + position: 'top-center', + }); + throw new Error('Failed to update customer'); + } + + // invalidate the queries so they pull updated information + // this is a prefix, so it covers both the query that pulls a list, and also `['customers', customerId]` in this file + await queryClient.invalidateQueries({ + queryKey: ['customers'] + }); + } + }) + + useEffect(() => { + if (data) { + // Even if query.data changes, form will be initialized only once + editCustomerForm.initialize(data); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [data]); + + if (isPending) return 'Loading...' + + if (error) return 'An error has occurred: ' + error.message + + return ( +
await + updateCustomer.mutateAsync(values))}> + + + +
{isFetching ? 'Updating...' : ''}
+ Edit Customer + + + + + + + + + +
+
+
+ ); +} + +export default EditCustomer; \ No newline at end of file