import { useState } from "react"; 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(); const navigation = useNavigation(); const isSubmitting = navigation.state === "submitting"; const [date, setDate] = useState(""); const [reason, setReason] = useState(""); if (locations.length === 0) { return ( Blackout dates can be scoped to one location, or left blank for all locations. ); } return ( {blackouts.length === 0 ? (
No blackout dates yet.
) : ( {blackouts.map((b, index) => ( {b.date.slice(0, 10)} {b.location?.name ?? "All locations"} {b.method?.replace("_", " ") ?? "All methods"} {b.reason ?? "—"}
))}
)}
Add a blackout date ({ label: m.replace("_", " "), value: m })), ]} />
); }