updated living space added

This commit is contained in:
2025-12-04 15:46:24 +03:00
parent 56b42bb906
commit 53e1f1e4fc
70 changed files with 1128 additions and 824 deletions

View File

@@ -22,6 +22,7 @@ const PageLivingSpaceAdd = () => {
const [partID, setPartID] = useState<string | null>(null);
const [companyID, setCompanyID] = useState<string | null>(null);
const [personID, setPersonID] = useState<string | null>(null);
const [isProperty, setIsProperty] = useState<boolean | null>(null);
const form = createForm({ buildID, userTypeID, partID, companyID, personID });
@@ -60,7 +61,7 @@ const PageLivingSpaceAdd = () => {
<PageLivingSpaceBuildsTableSection buildID={buildID} setBuildID={setBuildID} setIsUserTypeEnabled={setIsUserTypeEnabled} />
</TabsContent>
{isUserTypeEnabled && <TabsContent value="usertype">
<PageLivingSpaceUserTypesTableSection userTypeID={userTypeID} setUserTypeID={setUserTypeID} setIsPartsEnabled={setIsPartsEnabled} setIsHandleCompanyAndPersonEnable={setIsHandleCompanyAndPersonEnable} />
<PageLivingSpaceUserTypesTableSection userTypeID={userTypeID} setUserTypeID={setUserTypeID} setIsPartsEnabled={setIsPartsEnabled} setIsHandleCompanyAndPersonEnable={setIsHandleCompanyAndPersonEnable} setIsProperty={setIsProperty} />
</TabsContent>}
{isPartsEnabled && buildID && <TabsContent value="parts">
<PageLivingSpacePartsTableSection buildId={buildID} partID={partID} setPartID={setPartID} setIsPartsEnabled={setIsPartsEnabled} />

View File

@@ -11,7 +11,6 @@ const PageLivingSpaceList = () => {
const router = useRouter();
const [buildID, setBuildID] = useState<string | null>(null);
// const [collectionToken, setCollectionToken] = useState<string | null>(null);
const [isUserTypeEnabled, setIsUserTypeEnabled] = useState(false);
const [page, setPage] = useState(1);
const [limit, setLimit] = useState(10);
@@ -32,9 +31,7 @@ const PageLivingSpaceList = () => {
<div className="flex flex-col gap-2.5">
<PageLivingSpaceBuildsTableSection buildID={buildID} setBuildID={setBuildID} setIsUserTypeEnabled={setIsUserTypeEnabled} additionButtons={additionButtons} />
<h1 className="text-center justify-center">Living Space Added to selected Build with collectionToken: </h1><h1 className="text-center justify-center text-2xl font-bold">{buildID}</h1>
{
buildID && <LivingSpaceDataTable data={data?.data || []} totalCount={data?.totalCount || 0} currentPage={page} pageSize={limit} onPageChange={handlePageChange} onPageSizeChange={handlePageSizeChange} refetchTable={refetch} />
}
{buildID && <LivingSpaceDataTable data={data?.data || []} totalCount={data?.totalCount || 0} currentPage={page} pageSize={limit} onPageChange={handlePageChange} onPageSizeChange={handlePageSizeChange} refetchTable={refetch} buildID={buildID} />}
</div>
</>;
}

View File

@@ -11,6 +11,19 @@ const fetchGraphQlLivingSpaceList = async (buildID: string, params: ListArgument
} catch (error) { console.error('Error fetching test data:', error); throw error }
};
const fetchGraphQlLivingSpaceDetail = async (uuid: string, buildID: string): Promise<any> => {
console.log('Fetching test data from local API');
try {
const res = await fetch(`/api/living-space/detail`, { method: 'POST', cache: 'no-store', credentials: "include", body: JSON.stringify({ uuid, buildID }) });
if (!res.ok) { const errorText = await res.text(); console.error('Test data API error:', errorText); throw new Error(`API error: ${res.status} ${res.statusText}`) }; const data = await res.json();
return { data: data.data }
} catch (error) { console.error('Error fetching test data:', error); throw error }
};
export function useGraphQlLivingSpaceList(buildID: string, params: ListArguments) {
return useQuery({ queryKey: ['graphql-living-space-list', buildID, params], queryFn: () => fetchGraphQlLivingSpaceList(buildID, params) })
}
export function useGraphQlLivingSpaceDetail(uuid: string, buildID: string) {
return useQuery({ queryKey: ['graphql-living-space-detail', uuid, buildID], queryFn: () => fetchGraphQlLivingSpaceDetail(uuid, buildID) })
}

View File

@@ -23,31 +23,62 @@ export function DraggableRow({ row, selectedID }: { row: Row<z.infer<typeof sche
)
}
function getColumns(router: any, deleteHandler: (id: string) => void): ColumnDef<schemaType>[] {
function getColumns(router: any, deleteHandler: (id: string) => void, buildID: string): ColumnDef<schemaType>[] {
return [
{
accessorKey: "uuid",
header: "UUID",
},
{
accessorKey: "build",
header: "Build",
accessorKey: "person.firstName",
header: "First Name",
},
{
accessorKey: "part",
header: "Part",
accessorKey: "person.middleName",
header: "Middle Name",
},
{
accessorKey: "userType",
accessorKey: "person.surname",
header: "Surname",
},
{
accessorKey: "person.birthDate",
header: "Birth Date",
cell: ({ getValue }) => dateToLocaleString(getValue() as string),
},
{
accessorKey: "build.info.buildName",
header: "Build Name",
},
{
accessorKey: "build.info.buildNo",
header: "Build No",
},
{
accessorKey: "part.no",
header: "Part No",
},
{
accessorKey: "part.level",
header: "Level",
},
{
accessorKey: "part.humanLivability",
header: "Livability",
cell: ({ getValue }) => getValue() ? <div className="text-green-500 px-2 py-1 rounded">Yes</div> : <div className="text-red-500 px-2 py-1 rounded">No</div>
},
{
accessorKey: "person.birthPlace",
header: "Birth Place",
},
{
accessorKey: "userType.description",
header: "User Type",
},
{
accessorKey: "company",
header: "Company",
},
{
accessorKey: "person",
header: "Person",
accessorKey: "userType.isProperty",
header: "Property",
cell: ({ getValue }) => getValue() ? <div className="text-green-500 px-2 py-1 rounded">Yes</div> : <div className="text-red-500 px-2 py-1 rounded">No</div>
},
{
accessorKey: "createdAt",
@@ -75,10 +106,10 @@ function getColumns(router: any, deleteHandler: (id: string) => void): ColumnDef
cell: ({ row }) => {
return (
<div>
<Button className="bg-amber-400 text-black border-amber-400" variant="outline" size="sm" onClick={() => { router.push(`/living-spaces/update?uuid=${row.original.uuid}&buildID=${row.original.build}`) }}>
<Button className="bg-amber-400 text-black border-amber-400" variant="outline" size="sm" onClick={() => { router.push(`/living-spaces/update?uuid=${row.original.uuid}&buildID=${buildID}`) }}>
<Pencil />
</Button>
<Button className="bg-red-700 text-white border-red-700 mx-4" variant="outline" size="sm" onClick={() => { deleteHandler(row.original.uuid || "") }}>
<Button className="bg-red-700 text-white border-red-700 mx-4" variant="outline" size="sm" onClick={() => { deleteHandler(buildID) }}>
<Trash />
</Button>
</div>

View File

@@ -79,6 +79,7 @@ export function LivingSpaceDataTable({
onPageChange,
onPageSizeChange,
refetchTable,
buildID
}: {
data: schemaType[],
totalCount: number,
@@ -87,6 +88,7 @@ export function LivingSpaceDataTable({
onPageChange: (page: number) => void,
onPageSizeChange: (size: number) => void,
refetchTable: () => void,
buildID: string
}) {
const router = useRouter()
@@ -99,7 +101,7 @@ export function LivingSpaceDataTable({
const dataIds = React.useMemo<UniqueIdentifier[]>(() => data?.map(({ _id }) => _id) || [], [data])
const deleteMutation = useDeleteLivingSpacesMutation()
const deleteHandler = (id: string) => { deleteMutation.mutate({ uuid: id }); setTimeout(() => { refetchTable() }, 400) }
const columns = getColumns(router, deleteHandler);
const columns = getColumns(router, deleteHandler, buildID);
const pagination = React.useMemo(() => ({ pageIndex: currentPage - 1, pageSize: pageSize }), [currentPage, pageSize])
const totalPages = Math.ceil(totalCount / pageSize)

View File

@@ -18,4 +18,3 @@ export function useDeleteLivingSpacesMutation() {
onError: (error) => { console.error("Delete living space failed:", error) },
})
}

View File

@@ -7,10 +7,10 @@ import { useUpdateLivingSpaceMutation } from "./queries";
import { Button } from "@/components/ui/button";
import { IconArrowLeftToArc } from "@tabler/icons-react";
import { useRouter } from "next/navigation";
import { useGraphQlLivingSpaceList } from "../list/queries";
import { useGraphQlLivingSpaceDetail } from "../list/queries";
import { useSearchParams } from "next/navigation";
import PageLivingSpaceBuildsTableSection from "../tables/builds/page";
// import PageLivingSpaceBuildsTableSection from "../tables/builds/page";
import PageLivingSpaceUserTypesTableSection from "../tables/userType/page";
import PageLivingSpacePartsTableSection from "../tables/part/page";
import PageLivingSpacePersonTableSection from "../tables/person/page";
@@ -22,14 +22,10 @@ const PageLivingSpaceUpdate = () => {
const searchParams = useSearchParams()
const uuid = searchParams?.get('uuid') || null
const buildIDFromUrl = searchParams?.get('buildID') || null
const [page, setPage] = useState(1);
const [limit, setLimit] = useState(10);
const [sort, setSort] = useState({ createdAt: 'desc' });
const [filters, setFilters] = useState({});
const backToBuildAddress = <><div>UUID not found in search params</div><Button onClick={() => router.push('/living-spaces')}>Back to Living Space List</Button></>
const { data, isLoading, refetch } = useGraphQlLivingSpaceList(buildIDFromUrl || '', { limit, skip: (page - 1) * limit, sort, filters: { ...filters, uuid } });
const initData = data?.data?.[0] || null;
const { data, isLoading, refetch } = useGraphQlLivingSpaceDetail(uuid || '', buildIDFromUrl || '');
const initData = data?.data || null;
const [userTypeID, setUserTypeID] = useState<string | null>(null);
const isPartInit = initData?.part !== null ? true : false;
const [isProperty, setIsProperty] = useState<boolean | null>(isPartInit);
@@ -45,6 +41,7 @@ const PageLivingSpaceUpdate = () => {
setUserTypeID(initData?.userType || ""); setPartID(initData?.part || ""); setCompanyID(initData?.company || ""); setPersonID(initData?.person || "");
}
}, [initData])
useEffect(() => {
form.setValue("userTypeID", userTypeID || ""); form.setValue("partID", partID || ""); form.setValue("companyID", companyID || ""); form.setValue("personID", personID || "");
}, [userTypeID, partID, companyID, personID, form]);