updated build add update delete

This commit is contained in:
2025-12-02 17:13:25 +03:00
parent 0394d42d02
commit 5bb6021102
87 changed files with 1470 additions and 1158 deletions

View File

@@ -9,6 +9,8 @@ import { DateTimePicker } from "@/components/ui/date-time-picker"
import { BuildAdd, buildAddSchema } from "./schema"
import { useAddBuildMutation } from "./queries"
import { Checkbox } from "@/components/ui/checkbox"
import { PageBuildSelectionBuildTypes } from "../selections/build-types/page"
import { useState, useEffect } from "react"
const BuildsForm = ({ refetchTable }: { refetchTable: () => void }) => {
@@ -16,7 +18,6 @@ const BuildsForm = ({ refetchTable }: { refetchTable: () => void }) => {
resolver: zodResolver(buildAddSchema),
defaultValues: {
buildType: "",
collectionToken: "",
info: {
govAddressCode: "",
buildName: "",
@@ -37,354 +38,316 @@ const BuildsForm = ({ refetchTable }: { refetchTable: () => void }) => {
}
},
});
const [buildTypesID, setBuildTypesID] = useState('');
useEffect(() => { form.setValue("buildType", buildTypesID) }, [buildTypesID]);
const { handleSubmit } = form;
const mutation = useAddBuildMutation();
function onSubmit(values: BuildAdd) { mutation.mutate({ data: values }); setTimeout(() => refetchTable(), 400) };
return (
<Form {...form}>
<form onSubmit={handleSubmit(onSubmit)} className="space-y-6 p-4" >
<div>
<PageBuildSelectionBuildTypes buildTypesID={buildTypesID} setBuildTypesID={setBuildTypesID} />
<Form {...form}>
<form onSubmit={handleSubmit(onSubmit)} className="space-y-6 p-4" >
{/* ROW 1 */}
{/* ROW 1 */}
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<FormField
control={form.control}
name="buildType"
render={({ field }) => (
<FormItem>
<FormLabel>Token</FormLabel>
<FormControl>
<Input placeholder="Token" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
<FormField
control={form.control}
name="info.govAddressCode"
render={({ field }) => (
<FormItem>
<FormLabel>Gov Address Code</FormLabel>
<FormControl>
<Input placeholder="Gov Address Code" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.buildName"
render={({ field }) => (
<FormItem>
<FormLabel>Build Name</FormLabel>
<FormControl>
<Input placeholder="Build Name" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.buildNo"
render={({ field }) => (
<FormItem>
<FormLabel>Build No</FormLabel>
<FormControl>
<Input placeholder="Build No" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<FormField
control={form.control}
name="collectionToken"
render={({ field }) => (
<FormItem>
<FormLabel>Collection Token</FormLabel>
<FormControl>
<Input placeholder="Collection Token" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<FormField
control={form.control}
name="info.maxFloor"
render={({ field }) => (
<FormItem>
<FormLabel>Max Floor</FormLabel>
<FormControl>
<Input
type="number"
placeholder="Max Floor"
{...field}
onBlur={(e) => { field.onBlur(); const numValue = parseFloat(e.target.value); if (!isNaN(numValue)) { field.onChange(numValue) } }}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.undergroundFloor"
render={({ field }) => (
<FormItem>
<FormLabel>Underground Floor</FormLabel>
<FormControl>
<Input
type="number"
placeholder="Underground Floor"
{...field}
onBlur={(e) => { field.onBlur(); const numValue = parseFloat(e.target.value); if (!isNaN(numValue)) { field.onChange(numValue) } }}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<FormField
control={form.control}
name="info.govAddressCode"
render={({ field }) => (
<FormItem>
<FormLabel>Gov Address Code</FormLabel>
<FormControl>
<Input placeholder="Gov Address Code" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.buildName"
render={({ field }) => (
<FormItem>
<FormLabel>Build Name</FormLabel>
<FormControl>
<Input placeholder="Build Name" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.buildNo"
render={({ field }) => (
<FormItem>
<FormLabel>Build No</FormLabel>
<FormControl>
<Input placeholder="Build No" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<FormField
control={form.control}
name="info.taxNo"
render={({ field }) => (
<FormItem>
<FormLabel>Tax No</FormLabel>
<FormControl>
<Input placeholder="Tax No" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.liftCount"
render={({ field }) => (
<FormItem>
<FormLabel>Lift Count</FormLabel>
<FormControl>
<Input
type="number"
placeholder="Lift Count"
{...field}
onBlur={(e) => {
field.onBlur();
const numValue = parseFloat(e.target.value);
if (!isNaN(numValue)) {
field.onChange(numValue);
}
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<div className="flex flex-row justify-evenly">
<FormField
control={form.control}
name="info.heatingSystem"
render={({ field }) => (
<FormItem className="flex flex-row items-center space-x-3 space-y-0">
<FormControl>
<Checkbox checked={field.value} onCheckedChange={field.onChange} />
</FormControl>
<div className="leading-none">
<FormLabel>
Heating System
</FormLabel>
</div>
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.coolingSystem"
render={({ field }) => (
<FormItem className="flex flex-row items-center space-x-3 space-y-0">
<FormControl>
<Checkbox checked={field.value} onCheckedChange={field.onChange} />
</FormControl>
<div className="leading-none">
<FormLabel>
Cooling System
</FormLabel>
</div>
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.hotWaterSystem"
render={({ field }) => (
<FormItem className="flex flex-row items-center space-x-3 space-y-0">
<FormControl>
<Checkbox checked={field.value} onCheckedChange={field.onChange} />
</FormControl>
<div className="leading-none">
<FormLabel>
Hot Water System
</FormLabel>
</div>
</FormItem>
)}
/>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<FormField
control={form.control}
name="info.maxFloor"
render={({ field }) => (
<FormItem>
<FormLabel>Max Floor</FormLabel>
<FormControl>
<Input
type="number"
placeholder="Max Floor"
{...field}
onBlur={(e) => {
field.onBlur();
const numValue = parseFloat(e.target.value);
if (!isNaN(numValue)) {
field.onChange(numValue);
}
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.undergroundFloor"
render={({ field }) => (
<FormItem>
<FormLabel>Underground Floor</FormLabel>
<FormControl>
<Input
type="number"
placeholder="Underground Floor"
{...field}
onBlur={(e) => {
field.onBlur();
const numValue = parseFloat(e.target.value);
if (!isNaN(numValue)) {
field.onChange(numValue);
}
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<Separator />
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<FormField
control={form.control}
name="info.buildDate"
render={({ field }) => (
<FormItem>
<FormLabel>Build Date</FormLabel>
<FormControl>
<DateTimePicker {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.decisionPeriodDate"
render={({ field }) => (
<FormItem>
<FormLabel>Decision Period Date</FormLabel>
<FormControl>
<DateTimePicker {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<FormField
control={form.control}
name="info.taxNo"
render={({ field }) => (
<FormItem>
<FormLabel>Tax No</FormLabel>
<FormControl>
<Input placeholder="Tax No" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.liftCount"
render={({ field }) => (
<FormItem>
<FormLabel>Lift Count</FormLabel>
<FormControl>
<Input
type="number"
placeholder="Lift Count"
{...field}
onBlur={(e) => {
field.onBlur();
const numValue = parseFloat(e.target.value);
if (!isNaN(numValue)) {
field.onChange(numValue);
}
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<div className="grid grid-cols-1 md:grid-cols-4 gap-4">
<FormField
control={form.control}
name="info.blockServiceManCount"
render={({ field }) => (
<FormItem>
<FormLabel>Block Service Man Count</FormLabel>
<FormControl>
<Input
type="number"
placeholder="Block Service Man Count"
{...field}
onBlur={(e) => {
field.onBlur();
const numValue = parseFloat(e.target.value);
if (!isNaN(numValue)) {
field.onChange(numValue);
}
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.securityServiceManCount"
render={({ field }) => (
<FormItem>
<FormLabel>Security Service Man Count</FormLabel>
<FormControl>
<Input
type="number"
placeholder="Security Service Man Count"
{...field}
onBlur={(e) => {
field.onBlur();
const numValue = parseFloat(e.target.value);
if (!isNaN(numValue)) {
field.onChange(numValue);
}
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.garageCount"
render={({ field }) => (
<FormItem>
<FormLabel>Total Garage Count In Numbers</FormLabel>
<FormControl>
<Input
type="number"
placeholder="Total Garage Count"
{...field}
onBlur={(e) => {
field.onBlur();
const numValue = parseFloat(e.target.value);
if (!isNaN(numValue)) {
field.onChange(numValue);
}
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.managementRoomId"
render={({ field }) => (
<FormItem>
<FormLabel>Management Room ID Assign</FormLabel>
<FormControl>
<Input placeholder="Management Room ID" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
{buildTypesID && <Button type="submit" className="w-full">Add Build </Button>}
</form>
</Form>
</div>
<div className="flex flex-row justify-evenly">
<FormField
control={form.control}
name="info.heatingSystem"
render={({ field }) => (
<FormItem className="flex flex-row items-center space-x-3 space-y-0">
<FormControl>
<Checkbox checked={field.value} onCheckedChange={field.onChange} />
</FormControl>
<div className="leading-none">
<FormLabel>
Heating System
</FormLabel>
</div>
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.coolingSystem"
render={({ field }) => (
<FormItem className="flex flex-row items-center space-x-3 space-y-0">
<FormControl>
<Checkbox checked={field.value} onCheckedChange={field.onChange} />
</FormControl>
<div className="leading-none">
<FormLabel>
Cooling System
</FormLabel>
</div>
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.hotWaterSystem"
render={({ field }) => (
<FormItem className="flex flex-row items-center space-x-3 space-y-0">
<FormControl>
<Checkbox checked={field.value} onCheckedChange={field.onChange} />
</FormControl>
<div className="leading-none">
<FormLabel>
Hot Water System
</FormLabel>
</div>
</FormItem>
)}
/>
</div>
<Separator />
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<FormField
control={form.control}
name="info.buildDate"
render={({ field }) => (
<FormItem>
<FormLabel>Build Date</FormLabel>
<FormControl>
<DateTimePicker {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.decisionPeriodDate"
render={({ field }) => (
<FormItem>
<FormLabel>Decision Period Date</FormLabel>
<FormControl>
<DateTimePicker {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<div className="grid grid-cols-1 md:grid-cols-4 gap-4">
<FormField
control={form.control}
name="info.blockServiceManCount"
render={({ field }) => (
<FormItem>
<FormLabel>Block Service Man Count</FormLabel>
<FormControl>
<Input
type="number"
placeholder="Block Service Man Count"
{...field}
onBlur={(e) => {
field.onBlur();
const numValue = parseFloat(e.target.value);
if (!isNaN(numValue)) {
field.onChange(numValue);
}
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.securityServiceManCount"
render={({ field }) => (
<FormItem>
<FormLabel>Security Service Man Count</FormLabel>
<FormControl>
<Input
type="number"
placeholder="Security Service Man Count"
{...field}
onBlur={(e) => {
field.onBlur();
const numValue = parseFloat(e.target.value);
if (!isNaN(numValue)) {
field.onChange(numValue);
}
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.garageCount"
render={({ field }) => (
<FormItem>
<FormLabel>Total Garage Count In Numbers</FormLabel>
<FormControl>
<Input
type="number"
placeholder="Total Garage Count"
{...field}
onBlur={(e) => {
field.onBlur();
const numValue = parseFloat(e.target.value);
if (!isNaN(numValue)) {
field.onChange(numValue);
}
}}
/>
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="info.managementRoomId"
render={({ field }) => (
<FormItem>
<FormLabel>Management Room ID Assign</FormLabel>
<FormControl>
<Input placeholder="Management Room ID" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
</div>
<Button type="submit" className="w-full">Add Build </Button>
</form>
</Form>
);
};

View File

@@ -10,14 +10,12 @@ const PageAddBuilds = () => {
const [sort, setSort] = useState({ createdAt: 'desc' });
const [filters, setFilters] = useState({});
const { data, isLoading, error, refetch } = useGraphQlBuildsList({ limit, skip: (page - 1) * limit, sort, filters });
const { data, isFetching, isLoading, error, refetch } = useGraphQlBuildsList({ limit, skip: (page - 1) * limit, sort, filters });
return (
<>
<BuildDataTableAdd
data={data?.data || []} totalCount={data?.totalCount || 0} currentPage={page} pageSize={limit} onPageChange={setPage} onPageSizeChange={setLimit} refetchTable={refetch}
/>
<BuildsForm refetchTable={refetch} />
<BuildDataTableAdd data={data?.data || []} totalCount={data?.totalCount || 0} currentPage={page} pageSize={limit} onPageChange={setPage} onPageSizeChange={setLimit} refetchTable={refetch} tableIsLoading={isFetching || isLoading} />
<BuildsForm refetchTable={refetch} />
</>
)
}

View File

@@ -7,7 +7,6 @@ const fetchGraphQlBuildAdd = async (record: BuildAdd): Promise<{ data: BuildAdd
console.log('Fetching test data from local API');
record.info.buildDate = toISOIfNotZ(record.info.buildDate);
record.info.decisionPeriodDate = toISOIfNotZ(record.info.decisionPeriodDate);
console.dir({ record })
try {
const res = await fetch('/api/builds/add', { method: 'POST', cache: 'no-store', credentials: "include", body: JSON.stringify(record) });
if (!res.ok) { const errorText = await res.text(); console.error('Test data API error:', errorText); throw new Error(`API error: ${res.status} ${res.statusText}`) }

View File

@@ -3,7 +3,6 @@ import { z } from "zod"
export const buildAddSchema = z.object({
buildType: z.string(),
collectionToken: z.string(),
info: z.object({
govAddressCode: z.string(),
buildName: z.string(),

View File

@@ -48,11 +48,6 @@ function getColumns(router: any, deleteHandler: (id: string) => void): ColumnDef
header: "Token",
cell: ({ getValue }) => getValue(),
},
{
accessorKey: "collectionToken",
header: "Collection Token",
cell: ({ getValue }) => getValue(),
},
{
accessorKey: "info.govAddressCode",
header: "Gov Address Code",
@@ -101,17 +96,17 @@ function getColumns(router: any, deleteHandler: (id: string) => void): ColumnDef
{
accessorKey: "info.heatingSystem",
header: "Heating System",
cell: ({ getValue }) => getValue(),
cell: ({ getValue }) => getValue() ? (<div className="text-green-600 font-medium">Yes</div>) : (<div className="text-red-600 font-medium">No</div>),
},
{
accessorKey: "info.coolingSystem",
header: "Cooling System",
cell: ({ getValue }) => getValue(),
cell: ({ getValue }) => getValue() ? (<div className="text-green-600 font-medium">Yes</div>) : (<div className="text-red-600 font-medium">No</div>),
},
{
accessorKey: "info.hotWaterSystem",
header: "Hot Water System",
cell: ({ getValue }) => getValue(),
cell: ({ getValue }) => getValue() ? (<div className="text-green-600 font-medium">Yes</div>) : (<div className="text-red-600 font-medium">No</div>),
},
{
accessorKey: "info.blockServiceManCount",

View File

@@ -46,6 +46,7 @@ import { getColumns, DraggableRow } from "./columns"
import { useRouter } from "next/navigation"
import { Home } from "lucide-react"
import { useDeleteBuildMutation } from "@/pages/builds/queries"
import { TableSkeleton } from "@/components/skeletons/tableSkeleton"
export function BuildDataTableAdd({
data,
@@ -55,6 +56,7 @@ export function BuildDataTableAdd({
onPageChange,
onPageSizeChange,
refetchTable,
tableIsLoading,
}: {
data: schemaType[],
totalCount: number,
@@ -62,7 +64,8 @@ export function BuildDataTableAdd({
pageSize: number,
onPageChange: (page: number) => void,
onPageSizeChange: (size: number) => void,
refetchTable: () => void
refetchTable: () => void,
tableIsLoading: boolean
}) {
const router = useRouter();
@@ -75,7 +78,7 @@ export function BuildDataTableAdd({
const dataIds = React.useMemo<UniqueIdentifier[]>(() => data?.map(({ _id }) => _id) || [], [data])
const deleteMutation = useDeleteBuildMutation()
const deleteHandler = (id: string) => { deleteMutation.mutate({ uuid: id }); setTimeout(() => { refetchTable() }, 400) }
const deleteHandler = (id: string) => { deleteMutation.mutate({ uuid: id, refetchTable }) }
const columns = getColumns(router, deleteHandler);
const pagination = React.useMemo(() => ({ pageIndex: currentPage - 1, pageSize: pageSize, }), [currentPage, pageSize])
const totalPages = Math.ceil(totalCount / pageSize)
@@ -148,9 +151,7 @@ export function BuildDataTableAdd({
</Button>
</div>
</div>
<TabsContent value="outline"
className="relative flex flex-col gap-4 overflow-auto px-4 lg:px-6"
>
<TabsContent value="outline" className="relative flex flex-col gap-4 overflow-auto px-4 lg:px-6">
<div className="overflow-hidden rounded-lg border">
<DndContext collisionDetection={closestCenter} modifiers={[restrictToVerticalAxis]} sensors={sensors} id={sortableId} >
<Table>
@@ -168,11 +169,9 @@ export function BuildDataTableAdd({
))}
</TableHeader>
<TableBody className="**:data-[slot=table-cell]:first:w-8">
{table.getRowModel().rows?.length ? (<SortableContext items={dataIds} strategy={verticalListSortingStrategy} >
{table.getRowModel().rows.map((row) => <DraggableRow key={row.id} row={row} />)}
</SortableContext>) : (
<TableRow><TableCell colSpan={columns.length} className="h-24 text-center">No results.</TableCell></TableRow>
)}
{tableIsLoading ?
<TableSkeleton columnCount={table.getAllColumns().length} rowCount={pageSize} hasActions={true} /> :
<SortableContext items={dataIds} strategy={verticalListSortingStrategy}>{table.getRowModel().rows.map(row => <DraggableRow key={row.id} row={row} />)}</SortableContext>}
</TableBody>
</Table>
</DndContext>
@@ -194,12 +193,8 @@ export function BuildDataTableAdd({
</SelectContent>
</Select>
</div>
<div className="flex w-fit items-center justify-center text-sm font-medium">
Page {currentPage} of {totalPages}
</div>
<div className="flex w-fit items-center justify-center text-sm font-medium">
Total Count: {totalCount}
</div>
<div className="flex w-fit items-center justify-center text-sm font-medium">Page {currentPage} of {totalPages}</div>
<div className="flex w-fit items-center justify-center text-sm font-medium">Total Count: {totalCount}</div>
<div className="ml-auto flex items-center gap-2 lg:ml-0">
<Button
variant="outline"

View File

@@ -1,24 +0,0 @@
interface PeopleAdd {
firstName: string;
surname: string;
middleName?: string;
sexCode: string;
personRef?: string;
personTag?: string;
fatherName?: string;
motherName?: string;
countryCode: string;
nationalIdentityId: string;
birthPlace: string;
birthDate: string;
taxNo?: string;
birthname?: string;
expiryStarts?: string;
expiryEnds?: string;
}
export type { PeopleAdd };