Files
meezi/web/dashboard/src/components/coupons/coupons-screen.tsx
T
soroush.asadi 15def7ff1c
CI/CD / CI · API (dotnet build + test) (push) Successful in 1m10s
CI/CD / CI · Admin API (dotnet build) (push) Successful in 52s
CI/CD / CI · Dashboard (tsc) (push) Successful in 1m5s
CI/CD / CI · Admin Web (tsc) (push) Successful in 35s
CI/CD / CI · Website (tsc) (push) Successful in 45s
CI/CD / CI · Koja (tsc) (push) Successful in 55s
CI/CD / Deploy · all services (push) Successful in 3m29s
feat: delete actions for warehouse/reservations/coupons/customers + Koja listing toggle
Delete (every manageable entity that only had "add" now has delete):
- Ingredients (warehouse): new DELETE /inventory/ingredients/{id} (soft-delete via
  the global DeletedAt filter — no FK trouble with recipes/movements) + NoOp stub +
  trash button in the materials cards.
- Reservations: new DELETE /reservations/{id} (soft-delete) + per-card delete button.
- Coupons & Customers: backend DELETE already existed; wired delete buttons in the UI.
- Shared ConfirmDialog component used by all delete flows (RTL-aware).
- Audit result: tables/branches/taxes/kitchen-stations/expenses/menu/terminals already
  had delete; HR has no "add" so no delete needed; shifts intentionally excluded
  (financial open/close records, not add-style entities).

Koja visibility:
- New Cafe.ShowOnKoja flag, default TRUE (DB default true so existing cafés stay
  listed). Discover query now filters IsVerified && !Deleted && ShowOnKoja.
- public-profile GET/PUT expose showOnKoja; dashboard public-profile panel has an
  on-by-default toggle that persists immediately. Platform IsVerified gate unchanged.
- EF migration AddCafeShowOnKoja (defaultValue: true).

Also: added the missing errors.generic i18n key (fa/en/ar) so useApiError's fallback
resolves instead of rendering the literal "errors.generic". 81 API tests pass.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-02 16:14:40 +03:30

181 lines
6.5 KiB
TypeScript

"use client";
import { useState } from "react";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { useTranslations } from "next-intl";
import { Plus, Trash2 } from "lucide-react";
import { apiDelete, apiGet, apiPost } from "@/lib/api/client";
import type { Coupon, CouponType } from "@/lib/api/types";
import { useAuthStore } from "@/lib/stores/auth.store";
import { formatNumber } from "@/lib/format";
import { notify } from "@/lib/notify";
import { useApiError } from "@/lib/use-api-error";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { LabeledField } from "@/components/ui/labeled-field";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Badge } from "@/components/ui/badge";
import { ConfirmDialog } from "@/components/ui/confirm-dialog";
export function CouponsScreen() {
const t = useTranslations("coupons");
const tCommon = useTranslations("common");
const apiError = useApiError();
const cafeId = useAuthStore((s) => s.user?.cafeId);
const queryClient = useQueryClient();
const [showForm, setShowForm] = useState(false);
const [deleteTarget, setDeleteTarget] = useState<Coupon | null>(null);
const [code, setCode] = useState("");
const [type, setType] = useState<CouponType>("Percentage");
const [value, setValue] = useState("10");
const { data: coupons = [], isLoading } = useQuery({
queryKey: ["coupons", cafeId],
queryFn: () => apiGet<Coupon[]>(`/api/cafes/${cafeId}/coupons`),
enabled: !!cafeId,
});
const createCoupon = useMutation({
mutationFn: () =>
apiPost<Coupon>(`/api/cafes/${cafeId}/coupons`, {
code,
type,
value: Number(value),
isActive: true,
}),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["coupons", cafeId] });
setShowForm(false);
setCode("");
setValue("10");
},
});
const deleteCoupon = useMutation({
mutationFn: (id: string) => apiDelete(`/api/cafes/${cafeId}/coupons/${id}`),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["coupons", cafeId] });
setDeleteTarget(null);
notify.success(t("deleted"));
},
onError: (err) => notify.error(apiError(err)),
});
if (!cafeId) return null;
return (
<div className="space-y-4">
<div className="flex items-center justify-between">
<h2 className="text-xl font-bold">{t("title")}</h2>
<Button onClick={() => setShowForm(!showForm)}>
<Plus className="h-4 w-4" />
{t("addCoupon")}
</Button>
</div>
{showForm && (
<Card>
<CardContent className="grid gap-3 pt-6 sm:grid-cols-3">
<LabeledField label={t("code")} htmlFor="coupon-code">
<Input
id="coupon-code"
value={code}
onChange={(e) => setCode(e.target.value)}
dir="ltr"
className="text-end"
/>
</LabeledField>
<LabeledField label={t("type")} htmlFor="coupon-type">
<select
id="coupon-type"
className="w-full rounded-md border border-input bg-background px-3 py-2 text-sm"
value={type}
onChange={(e) => setType(e.target.value as CouponType)}
>
<option value="Percentage">{t("types.Percentage")}</option>
<option value="FixedAmount">{t("types.FixedAmount")}</option>
</select>
</LabeledField>
<LabeledField label={t("value")} htmlFor="coupon-value">
<Input
id="coupon-value"
value={value}
onChange={(e) => setValue(e.target.value)}
type="number"
dir="ltr"
className="text-end"
/>
</LabeledField>
<div className="flex gap-2 sm:col-span-3">
<Button onClick={() => createCoupon.mutate()} disabled={createCoupon.isPending}>
{tCommon("save")}
</Button>
<Button variant="outline" onClick={() => setShowForm(false)}>
{tCommon("cancel")}
</Button>
</div>
</CardContent>
</Card>
)}
{isLoading ? (
<p className="text-muted-foreground">{tCommon("loading")}</p>
) : coupons.length === 0 ? (
<p className="text-muted-foreground">{t("noCoupons")}</p>
) : (
<div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
{coupons.map((c) => (
<Card key={c.id}>
<CardHeader className="pb-2">
<div className="flex items-center justify-between">
<CardTitle className="font-mono text-lg">{c.code}</CardTitle>
<Badge variant={c.isActive ? "default" : "secondary"}>
{c.isActive ? t("active") : t("inactive")}
</Badge>
</div>
</CardHeader>
<CardContent className="text-sm text-muted-foreground">
<p>
{t("type")}: {t(`types.${c.type}`)}
</p>
<p>
{t("value")}: {formatNumber(c.value)}
{c.type === "Percentage" ? "%" : " ت"}
</p>
<p>
{t("usage")}: {formatNumber(c.usedCount)}
{c.usageLimit ? ` / ${formatNumber(c.usageLimit)}` : ""}
</p>
<div className="mt-2 flex justify-end">
<Button
type="button"
size="sm"
variant="ghost"
className="text-red-600 hover:bg-red-50 hover:text-red-700"
onClick={() => setDeleteTarget(c)}
>
<Trash2 className="me-1.5 size-4" />
{tCommon("delete")}
</Button>
</div>
</CardContent>
</Card>
))}
</div>
)}
<ConfirmDialog
open={!!deleteTarget}
onOpenChange={(o) => {
if (!o) setDeleteTarget(null);
}}
title={t("deleteConfirmTitle")}
description={deleteTarget ? t("deleteConfirmDesc", { code: deleteTarget.code }) : undefined}
busy={deleteCoupon.isPending}
onConfirm={() => deleteTarget && deleteCoupon.mutate(deleteTarget.id)}
/>
</div>
);
}