metatrondelivery/app/routes/app.blackouts._index.tsx
metatroncubeswdev 21f0ee704b feat: Phase 1 — core data model & admin CRUD
Adds the scheduling core to Prisma (Shop, Location, Method enum,
SlotTemplate, SlotOverride, BlackoutDate — IMPLEMENTATION_PLAN.md §4) and
Polaris admin screens to manage them:

- /app/locations: list/create/edit/delete locations, one-click vertical
  template seeding (bakery/florist/grocer)
- /app/slots: weekly slot template editor, scoped per location
- /app/blackouts: blackout dates, scoped to one location or all of them

services/templates.server.ts keeps the vertical presets as a pure,
unit-tested function (getVerticalTemplate) separate from the thin I/O
wrapper (seedVerticalTemplate) that does the actual Prisma writes, per
CLAUDE.md's "pure functions, inject data, no I/O in the math" rule.

Switched dev DB from SQLite to Postgres (docker-compose.yml, ports 5433/6380
to avoid clashing with other local projects already on 5432/6379): SQLite
doesn't support Prisma enums at all, and IMPLEMENTATION_PLAN.md's schema
relies on them (Method) plus Postgres-only array fields in later phases
(Zone.postalCodes, ProductRule.allowedLocationIds). Only one throwaway
migration existed, so switching now avoids compounding the rework later.

Also fixed a Remix/Polaris integration issue hit while building these forms:
this Polaris version's TextField/Checkbox are fully controlled (no
defaultValue/defaultChecked), and `data()` imported from `@remix-run/react`
(rather than `@remix-run/node`) breaks useActionData's type inference —
both are now handled correctly across the new routes.

Verified: lint, typecheck, unit tests (incl. template-seeding fixtures),
build, and a live end-to-end run of seedVerticalTemplate against the
Postgres container all pass.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-23 17:47:59 -04:00

171 lines
5.5 KiB
TypeScript

import { data, type ActionFunctionArgs, type LoaderFunctionArgs } from "@remix-run/node";
import { Form, useLoaderData, useNavigation } from "@remix-run/react";
import {
Page,
Card,
BlockStack,
InlineStack,
Text,
Button,
Select,
TextField,
IndexTable,
EmptyState,
} from "@shopify/polaris";
import { TitleBar } from "@shopify/app-bridge-react";
import type { Method } from "@prisma/client";
import { authenticate } from "../shopify.server";
import db from "../db.server";
const METHODS: Method[] = ["SHIPPING", "LOCAL_DELIVERY", "PICKUP"];
export const loader = async ({ request }: LoaderFunctionArgs) => {
const { session } = await authenticate.admin(request);
const [locations, blackouts] = await Promise.all([
db.location.findMany({ where: { shopDomain: session.shop }, orderBy: { createdAt: "asc" } }),
db.blackoutDate.findMany({
where: { shopDomain: session.shop },
include: { location: true },
orderBy: { date: "asc" },
}),
]);
return { locations, blackouts };
};
export const action = async ({ request }: ActionFunctionArgs) => {
const { session } = await authenticate.admin(request);
const formData = await request.formData();
const intent = formData.get("intent");
if (intent === "delete") {
const id = String(formData.get("id") || "");
await db.blackoutDate.deleteMany({ where: { id, shopDomain: session.shop } });
return data({ ok: true });
}
const date = String(formData.get("date") || "");
const locationId = String(formData.get("locationId") || "") || null;
const method = (String(formData.get("method") || "") || null) as Method | null;
const reason = String(formData.get("reason") || "").trim() || null;
if (!date) {
return data({ errors: { date: "Date is required" } });
}
await db.blackoutDate.create({
data: {
shopDomain: session.shop,
date: new Date(`${date}T00:00:00.000Z`),
locationId,
method,
reason,
},
});
return data({ ok: true });
};
export default function BlackoutsIndex() {
const { locations, blackouts } = useLoaderData<typeof loader>();
const navigation = useNavigation();
const isSubmitting = navigation.state === "submitting";
if (locations.length === 0) {
return (
<Page>
<TitleBar title="Blackout dates" />
<Card>
<EmptyState
heading="Add a location first"
action={{ content: "Add location", url: "/app/locations/new" }}
image="https://cdn.shopify.com/s/files/1/0757/9955/files/empty-state.svg"
>
<Text as="p">Blackout dates can be scoped to one location, or left blank for all locations.</Text>
</EmptyState>
</Card>
</Page>
);
}
return (
<Page>
<TitleBar title="Blackout dates" />
<BlockStack gap="400">
<Card padding="0">
{blackouts.length === 0 ? (
<div style={{ padding: 16 }}>
<Text as="p" tone="subdued">
No blackout dates yet.
</Text>
</div>
) : (
<IndexTable
itemCount={blackouts.length}
headings={[
{ title: "Date" },
{ title: "Location" },
{ title: "Method" },
{ title: "Reason" },
{ title: "" },
]}
selectable={false}
>
{blackouts.map((b, index) => (
<IndexTable.Row id={b.id} key={b.id} position={index}>
<IndexTable.Cell>{b.date.slice(0, 10)}</IndexTable.Cell>
<IndexTable.Cell>{b.location?.name ?? "All locations"}</IndexTable.Cell>
<IndexTable.Cell>{b.method?.replace("_", " ") ?? "All methods"}</IndexTable.Cell>
<IndexTable.Cell>{b.reason ?? "—"}</IndexTable.Cell>
<IndexTable.Cell>
<Form method="post">
<input type="hidden" name="intent" value="delete" />
<input type="hidden" name="id" value={b.id} />
<Button submit variant="plain" tone="critical">
Remove
</Button>
</Form>
</IndexTable.Cell>
</IndexTable.Row>
))}
</IndexTable>
)}
</Card>
<Card>
<Form method="post">
<BlockStack gap="300">
<Text as="h3" variant="headingSm">
Add a blackout date
</Text>
<InlineStack gap="300" wrap>
<TextField label="Date" name="date" type="date" autoComplete="off" />
<Select
label="Location"
name="locationId"
options={[{ label: "All locations", value: "" }, ...locations.map((l) => ({ label: l.name, value: l.id }))]}
/>
<Select
label="Method"
name="method"
options={[
{ label: "All methods", value: "" },
...METHODS.map((m) => ({ label: m.replace("_", " "), value: m })),
]}
/>
<TextField label="Reason (optional)" name="reason" autoComplete="off" />
</InlineStack>
<div>
<Button submit variant="primary" loading={isSubmitting}>
Add blackout date
</Button>
</div>
</BlockStack>
</Form>
</Card>
</BlockStack>
</Page>
);
}