metatrondelivery/app/routes/app.locations.new.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

71 lines
2.4 KiB
TypeScript

import { useState } from "react";
import { redirect, type ActionFunctionArgs } from "@remix-run/node";
import { Form, useActionData, useNavigation } from "@remix-run/react";
import { Page, Card, BlockStack, FormLayout, TextField, Button } from "@shopify/polaris";
import { TitleBar } from "@shopify/app-bridge-react";
import { authenticate } from "../shopify.server";
import db from "../db.server";
export const action = async ({ request }: ActionFunctionArgs) => {
const { session } = await authenticate.admin(request);
const formData = await request.formData();
const name = String(formData.get("name") || "").trim();
const address = String(formData.get("address") || "").trim();
const timezone = String(formData.get("timezone") || "").trim();
const errors: Record<string, string> = {};
if (!name) errors.name = "Name is required";
if (!timezone) errors.timezone = "Timezone is required";
if (Object.keys(errors).length > 0) {
return { errors };
}
const location = await db.location.create({
data: { shopDomain: session.shop, name, address, timezone },
});
return redirect(`/app/locations/${location.id}`);
};
export default function NewLocation() {
const actionData = useActionData<typeof action>();
const navigation = useNavigation();
const [timezone, setTimezone] = useState(Intl.DateTimeFormat().resolvedOptions().timeZone);
return (
<Page>
<TitleBar title="Add location" />
<Card>
<Form method="post">
<FormLayout>
<TextField
label="Location name"
name="name"
autoComplete="off"
error={actionData?.errors?.name}
requiredIndicator
/>
<TextField label="Address" name="address" autoComplete="off" multiline={2} />
<TextField
label="Timezone (IANA, e.g. America/Toronto)"
name="timezone"
autoComplete="off"
value={timezone}
onChange={setTimezone}
error={actionData?.errors?.timezone}
requiredIndicator
helpText="All slot math for this location is computed in this timezone."
/>
<BlockStack>
<Button submit variant="primary" loading={navigation.state === "submitting"}>
Create location
</Button>
</BlockStack>
</FormLayout>
</Form>
</Card>
</Page>
);
}