-
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.
Add experimental gear inventory page
At this point it only allows user to generate a CSV dump of the gear, and select which types should be inventoried.
- Loading branch information
Showing
3 changed files
with
97 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { API_HOST } from "apiClient/client"; | ||
import { editGearType, GearType } from "apiClient/gear"; | ||
import { useSetPageTitle } from "hooks"; | ||
import { useGetGearTypesQuery } from "redux/api"; | ||
|
||
export function GearInventoryPage() { | ||
useSetPageTitle("Gear Inventory"); | ||
const { data: allGearTypes, refetch } = useGetGearTypesQuery(); | ||
return ( | ||
<div className="row"> | ||
<h1>Gear inventory</h1> | ||
<h2>Gear types to inventory</h2> | ||
<div className="row mb-4"> | ||
{allGearTypes?.map((gearType) => { | ||
return ( | ||
<div | ||
key={gearType.id} | ||
className="col-4" | ||
style={{ border: "grey dotted 1px" }} | ||
> | ||
<label> | ||
<input | ||
type="checkbox" | ||
checked={gearType.shouldInventory} | ||
onChange={() => { | ||
editGearType(gearType.id, { | ||
shouldInventory: !gearType.shouldInventory, | ||
}).then(refetch); | ||
}} | ||
/>{" "} | ||
{gearType.typeName} | ||
</label> | ||
</div> | ||
); | ||
})} | ||
</div> | ||
<h2>Export CSV files</h2> | ||
<a | ||
className="btn btn-primary col-3 me-3" | ||
href={API_HOST + "/gear-inventory/export"} | ||
> | ||
Export gear CSV | ||
</a> | ||
<button | ||
className="btn btn-primary col-3" | ||
onClick={() => { | ||
if (allGearTypes == null) { | ||
return; | ||
} | ||
const csvContent = makeGearTypesCSV( | ||
allGearTypes.filter((gearType) => gearType.shouldInventory), | ||
); | ||
downloadCSVFile(csvContent, "gear-types.csv"); | ||
}} | ||
> | ||
Export gear types CSV | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
function makeGearTypesCSV(types: GearType[]) { | ||
return [ | ||
["ID", "Type Name"], | ||
...types.map((gearType) => [gearType.id, gearType.typeName].join(",")), | ||
].join("\n"); | ||
} | ||
|
||
function downloadCSVFile(content: string, name: string) { | ||
const blob = new Blob([content], { type: "text/csv" }); | ||
const blobUrl = URL.createObjectURL(blob); | ||
const link = document.createElement("a"); | ||
link.href = blobUrl; | ||
link.download = name; | ||
document.body.appendChild(link); | ||
link.dispatchEvent( | ||
new MouseEvent("click", { | ||
bubbles: true, | ||
cancelable: true, | ||
view: window, | ||
}), | ||
); | ||
document.body.removeChild(link); | ||
} |