-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tom
committed
Jan 10, 2025
1 parent
57e3958
commit fe6aece
Showing
10 changed files
with
172 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
"use client"; | ||
import { ColumnDef, createColumnHelper } from "@tanstack/react-table"; | ||
import { memo, useEffect, useMemo } from "react"; | ||
import { csvFormatRows } from "d3-dsv"; | ||
import { | ||
useLabourListRows, | ||
useProjectCurrency, | ||
useHouses, | ||
LabourListRow, | ||
} from "@opensystemslab/buildx-core"; | ||
import { getColorClass } from "~/analyse/ui/colors"; | ||
import { capitalizeFirstLetters } from "~/utils/functions"; | ||
import PaginatedTable from "../PaginatedTable"; | ||
import { pipe } from "fp-ts/lib/function"; | ||
import { A } from "~/utils/functions"; | ||
|
||
type Props = { | ||
setCsvDownloadUrl: (s: string) => void; | ||
}; | ||
|
||
export const useLabourListDownload = (labourListRows: LabourListRow[]) => | ||
useMemo(() => { | ||
if (labourListRows.length > 0) { | ||
const headers = Object.keys(labourListRows[0]).filter( | ||
(x) => !["houseId"].includes(x) | ||
) as Array<keyof LabourListRow>; | ||
|
||
const rows = labourListRows.map((row) => | ||
headers.map((header) => row[header]?.toString() ?? "") | ||
); | ||
|
||
const csvData = [headers, ...rows]; | ||
const csvContent = csvFormatRows(csvData); | ||
const blob = new Blob([csvContent], { type: "text/csv;charset=utf-8;" }); | ||
return { url: URL.createObjectURL(blob), blob }; | ||
} | ||
}, [labourListRows]); | ||
|
||
const LabourTable = (props: Props) => { | ||
const { setCsvDownloadUrl } = props; | ||
const { symbol, format } = useProjectCurrency(); | ||
|
||
const labourListRows = useLabourListRows(); | ||
const houses = useHouses(); | ||
const allHouseIds = houses.map((x) => x.houseId); | ||
|
||
const labourListDownload = useLabourListDownload(labourListRows); | ||
|
||
useEffect(() => { | ||
if (labourListDownload) { | ||
setCsvDownloadUrl(labourListDownload.url); | ||
} | ||
}, [labourListDownload, setCsvDownloadUrl]); | ||
|
||
const totalCost = pipe( | ||
labourListRows, | ||
A.reduce(0, (acc, row) => acc + row.cost) | ||
); | ||
|
||
const columnHelper = createColumnHelper<LabourListRow>(); | ||
|
||
const columns: ColumnDef<LabourListRow, any>[] = useMemo( | ||
() => [ | ||
columnHelper.accessor("buildingName", { | ||
id: "Building Name", | ||
cell: (info) => <div>{capitalizeFirstLetters(info.getValue())}</div>, | ||
header: () => null, | ||
}), | ||
columnHelper.accessor("labourType", { | ||
cell: (info) => <span>{info.getValue()}</span>, | ||
header: () => <span>Labour Type</span>, | ||
}), | ||
columnHelper.accessor("hours", { | ||
cell: (info) => <span>{info.getValue().toFixed(1)}h</span>, | ||
header: () => <span>Hours</span>, | ||
}), | ||
columnHelper.accessor("cost", { | ||
cell: (info) => <span>{format(info.getValue())}</span>, | ||
header: () => <span>Cost</span>, | ||
footer: () => <span>{format(totalCost)}</span>, | ||
}), | ||
], | ||
[columnHelper, format, totalCost] | ||
); | ||
|
||
const enhancedRows = labourListRows.map((row) => ({ | ||
...row, | ||
colorClass: getColorClass(allHouseIds, row.houseId), | ||
})); | ||
|
||
return <PaginatedTable data={enhancedRows} columns={columns} />; | ||
}; | ||
|
||
export default memo(LabourTable); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
"use client"; | ||
import { useState } from "react"; | ||
import LabourTable from "./LabourTable"; | ||
import { Download } from "@carbon/icons-react"; | ||
|
||
const LabourApp = () => { | ||
const [csvDownloadUrl, setCsvDownloadUrl] = useState<string | null>(null); | ||
|
||
return ( | ||
<div> | ||
<div className="flex justify-between px-3 py-5"> | ||
<div> | ||
<h1>Labour</h1> | ||
<p className="max-w-3xl mt-2"> | ||
Lorem ipsum dolor sit amet consectetur, adipisicing elit. Ab illum | ||
vel sapiente placeat obcaecati laudantium officia tenetur eum, quod, | ||
animi rem accusantium culpa doloribus voluptatum autem at recusandae | ||
molestias deleniti. | ||
</p> | ||
</div> | ||
<div> | ||
{csvDownloadUrl !== null && ( | ||
<a | ||
href={csvDownloadUrl} | ||
download={`materials-list.csv`} | ||
className="flex font-semibold items-center" | ||
> | ||
<span>Download CSV</span> | ||
<span className="ml-2"> | ||
<Download size={"20"} /> | ||
</span> | ||
</a> | ||
)} | ||
</div> | ||
</div> | ||
<LabourTable setCsvDownloadUrl={setCsvDownloadUrl} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default LabourApp; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import dynamic from "next/dynamic"; | ||
|
||
const LabourApp = dynamic(() => import("./app"), { ssr: false }); | ||
|
||
const LabourPage = () => { | ||
return <LabourApp />; | ||
}; | ||
|
||
export default LabourPage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.