import { useState } from "react"; import { data, type ActionFunctionArgs, type LoaderFunctionArgs } from "@remix-run/node"; import { Form, useLoaderData, useNavigation } from "@remix-run/react"; import { Page, Layout, Card, BlockStack, InlineStack, Text, Button, ButtonGroup, ResourceList, ResourceItem, Badge, EmptyState, Select, } from "@shopify/polaris"; import { TitleBar } from "@shopify/app-bridge-react"; import { authenticate } from "../shopify.server"; import db from "../db.server"; import { listVerticalTemplates, seedVerticalTemplate, type VerticalKey } from "../services/templates.server"; import { canAddLocation, getShopTier, locationLimitFor } from "../services/billing.server"; export const loader = async ({ request }: LoaderFunctionArgs) => { const { session } = await authenticate.admin(request); const locations = await db.location.findMany({ where: { shopDomain: session.shop }, include: { _count: { select: { slotTemplates: true, blackouts: true } } }, orderBy: { createdAt: "asc" }, }); return { locations, verticals: listVerticalTemplates() }; }; export const action = async ({ request }: ActionFunctionArgs) => { const { session } = await authenticate.admin(request); const formData = await request.formData(); const intent = formData.get("intent"); if (intent === "seed-template") { if (!(await canAddLocation(session.shop))) { const tier = await getShopTier(session.shop); return data( { error: `Your ${tier} plan allows up to ${locationLimitFor(tier)} location(s). Upgrade to add more.` }, { status: 403 }, ); } const vertical = formData.get("vertical") as VerticalKey; await seedVerticalTemplate(session.shop, vertical); return data({ ok: true }); } if (intent === "delete-location") { const id = formData.get("id") as string; await db.location.deleteMany({ where: { id, shopDomain: session.shop } }); return data({ ok: true }); } return data({ ok: false }, { status: 400 }); }; export default function LocationsIndex() { const { locations, verticals } = useLoaderData(); const navigation = useNavigation(); const isSubmitting = navigation.state === "submitting"; const [vertical, setVertical] = useState(verticals[0]); return ( {locations.length === 0 ? ( Start from a vertical template — it seeds a location with a realistic weekly Pickup/Delivery schedule you can then edit, or add a location from scratch.
) : ( ( {location.name} {location.address || "No address set"} · {location.timezone} {`${location._count.slotTemplates} slot templates`} {location.active ? "Active" : "Inactive"} )} /> )} {locations.length > 0 && ( )} ); } function capitalize(s: string) { return s.charAt(0).toUpperCase() + s.slice(1); }