Added edit customer detail page

This commit is contained in:
2026-07-18 18:46:53 -05:00
parent 34d421719c
commit 9374ad53e3
3 changed files with 137 additions and 1 deletions
+2
View File
@@ -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() {
<Route path="customers" element={<CustomerList/>}/>
<Route path="customers/addCustomer" element={<AddCustomer/>}/>
<Route path="customers/customerDetail/:customerId" element={<CustomerDetail/>}/>
<Route path="customers/editCustomer/:customerId" element={<EditCustomer/>}/>
</Routes>
</>
@@ -36,7 +36,7 @@ function CustomerDetail() {
<Text>Address: ${data.address}</Text>
<Text>Notes: {data.notes}</Text>
<Link to={`/inventory/editItem/${data.id}`}>Edit</Link>
<Link to={`/customers/editCustomer/${data.id}`}>Edit</Link>
</Container>
</>
)
@@ -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<Customer, 'id'>;
function EditCustomer() {
const params = useParams();
const navigate = useNavigate();
const editCustomerForm = useForm<EditableCustomer>({
mode: 'uncontrolled',
initialValues: {
name: "",
company: "",
email: "",
phone: "",
address: "",
billingTerms: "",
notes: "",
},
validate: {},
});
const {isPending, error, data, isFetching} = useQuery({
queryKey: ['customer', params.customerId],
queryFn: async (): Promise<Customer> => {
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 <call the api, do the things>
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: <IconCheck size={20} />,
color:"teal",
title: "All good!",
message: "Customer Updated",
position: 'top-center',
});
navigate("/customers");
}
if (!result.ok) {
notifications.show({
icon: <IconX size={20} />,
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 (
<form onSubmit={editCustomerForm.onSubmit(async (values) => await
updateCustomer.mutateAsync(values))}>
<Container m="lg">
<Flex mih={50}
gap="md"
justify="flex-start"
align="flex-start"
direction="column"
wrap="wrap">
<div>{isFetching ? 'Updating...' : ''}</div>
<Title order={1}>Edit Customer</Title>
<TextInput withAsterisk key={editCustomerForm.key('name')} size="md" label="Name"
placeholder="Name" {...editCustomerForm.getInputProps('name')}/>
<TextInput size="md" key={editCustomerForm.key('company')} label="Company"
placeholder="Company"{...editCustomerForm.getInputProps('company')}/>
<TextInput withAsterisk size="md" key={editCustomerForm.key('email')} label="Email"
placeholder="Email" {...editCustomerForm.getInputProps('email')}/>
<TextInput size="md" key={editCustomerForm.key('phone')} label="Phone"
placeholder="Phone" {...editCustomerForm.getInputProps('phone')}/>
<TextInput size="md" key={editCustomerForm.key('address')} label="Address"
placeholder="Address" {...editCustomerForm.getInputProps('address')}/>
<Group justify="flex-end" mt="md">
<Button type="submit">Submit</Button>
</Group>
</Flex>
</Container>
</form>
);
}
export default EditCustomer;