gear-orders/flask/vite/src/SubOrderSets.tsx

104 lines
3.2 KiB
TypeScript
Raw Normal View History

2026-01-30 16:18:49 +00:00
import {
Container,
Title,
Card,
Text,
Button,
Flex,
Badge,
} from "@mantine/core";
import { TimeValue } from "@mantine/dates";
2026-01-30 19:15:20 +00:00
import { IconPencil, IconPlus, IconTrash } from "@tabler/icons-react";
2026-01-29 21:30:51 +00:00
import React from "react";
2026-01-30 19:15:20 +00:00
import { Params, useLoaderData, useParams, useFetcher } from "react-router";
import { ConfirmDialogButton } from "./ConfirmDialogButton";
import { NavigateButton } from "./NavigateButton";
2026-01-29 21:30:51 +00:00
export const subOrderSetsLoader = async ({
params: { username },
}: {
params: Params<string>;
}) => fetch(`/api/subs/${username}/sets`).then((response) => response.json());
export const SubOrderSets: React.FC = () => {
2026-01-30 19:15:20 +00:00
const fetcher = useFetcher();
2026-01-29 21:30:51 +00:00
const { username: sub_username } = useParams();
2026-01-30 16:18:49 +00:00
const orderSets = useLoaderData<
(Pick<
OrderSet,
"id" | "name" | "scheduled" | "time" | "weekends" | "weekdays" | "orders"
> & {
orders: Pick<OrderSetOrder, "id" | "name">[];
})[]
2026-01-29 21:30:51 +00:00
>();
2026-01-30 19:15:20 +00:00
const handleDelete = React.useCallback(
(id: number) => {
fetcher.submit(null, {
action: `/dashboard/subs/${sub_username}/${id}`,
method: "DELETE",
});
},
[fetcher],
);
2026-01-29 21:30:51 +00:00
return (
<Container>
2026-01-29 22:23:20 +00:00
<Title order={1} mb="lg">
2026-01-30 16:18:49 +00:00
Order Sets for {sub_username}
2026-01-29 22:23:20 +00:00
</Title>
2026-01-30 16:18:49 +00:00
<Flex gap="md" wrap="wrap">
{orderSets
? orderSets.map(
({ id, name, scheduled, orders, time, weekdays, weekends }) => (
<Card
key={id}
shadow="sm"
padding="lg"
radius="md"
withBorder
bg="gray.2"
w="400px"
>
<Flex direction="column" gap="md">
<Title order={4}>{name}</Title>
{scheduled ? (
<Flex gap="md" align="center">
<Text>
Scheduled: <TimeValue value={time} format="12h" />
</Text>
{weekdays ? <Badge color="blue">Weekdays</Badge> : null}
{weekends ? <Badge color="blue">Weekends</Badge> : null}
</Flex>
) : null}
<Text mb="md">Orders: {orders.length}</Text>
</Flex>
2026-01-30 19:15:20 +00:00
<Flex gap="md" justify="end">
<ConfirmDialogButton
buttonColor="red"
buttonText="Delete"
text={`Are you sure you want to delete ${name}?`}
onConfirm={() => handleDelete(id)}
>
<IconTrash />
</ConfirmDialogButton>
2026-01-30 16:18:49 +00:00
<NavigateButton
to={`/dashboard/subs/${sub_username}/${id}`}
>
<IconPencil style={{ marginRight: "0.5rem" }} />
Edit
</NavigateButton>
</Flex>
</Card>
),
)
: null}
</Flex>
2026-01-30 16:52:32 +00:00
<NavigateButton to={`/dashboard/subs/${sub_username}/new`}>
<IconPlus style={{ marginRight: "0.5rem" }} />
New
</NavigateButton>
2026-01-29 21:30:51 +00:00
</Container>
);
};