Files
pn-new-crm/src/components/interests/interest-outcome-dialog.tsx
Matt e7e498dedd fix(T3): copy + entry points + recommender alias
Batch of small fixes from the post-audit plan:

F11 — "Mark as won" dialog copy
  Was: "This will move the interest to Completed and stamp the outcome."
  Completed was retired in the 7-stage refactor; copy now reads
  "marks Won; stage stays where it is" with a parallel Lost variant.

F13 — Bulk-add berths wizard had no UI entry point
  Page existed at /[portSlug]/admin/berths/bulk-add but nothing linked
  to it. Added a "Bulk add" button on the Berths list toolbar, gated
  on `berths.import`. Also fixed the API route's permission key
  (was `berths.create`, a phantom — switched to `berths.import` to
  match seed-permissions).

F14 — Audit Log nav entry
  Sidebar Admin section now lists "Audit Log" → /admin/audit, gated
  by the adminRequired group rule.

F18 — Recommender `limit` param ignored
  POST /interests/[id]/recommend-berths now accepts `limit` as an
  alias for `topN`. Audit sent `{limit:3}` and silently got 8 rows
  back; both names now resolve.

Tests still green.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 22:59:38 +02:00

168 lines
5.2 KiB
TypeScript

'use client';
import { useState } from 'react';
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { Loader2, Trophy, XCircle } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import {
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from '@/components/ui/select';
import { apiFetch } from '@/lib/api/client';
import { type InterestOutcome } from '@/lib/validators/interests';
const OUTCOME_LABELS: Record<InterestOutcome, string> = {
won: 'Won',
lost_other_marina: 'Lost - went to another marina',
lost_unqualified: 'Lost - unqualified',
lost_no_response: 'Lost - no response',
lost_other: 'Lost - other',
cancelled: 'Cancelled',
};
const LOST_OUTCOMES: InterestOutcome[] = [
'lost_other_marina',
'lost_unqualified',
'lost_no_response',
'lost_other',
'cancelled',
];
interface Props {
interestId: string;
open: boolean;
onOpenChange: (open: boolean) => void;
/** Determines which outcomes are offered. 'won' opens with just the Won option preselected. */
mode: 'won' | 'lost';
}
export function InterestOutcomeDialog({ interestId, open, onOpenChange, mode }: Props) {
const queryClient = useQueryClient();
const choices: InterestOutcome[] = mode === 'won' ? ['won'] : LOST_OUTCOMES;
const [outcome, setOutcome] = useState<InterestOutcome>(choices[0]!);
const [reason, setReason] = useState('');
const mutation = useMutation({
mutationFn: () =>
apiFetch(`/api/v1/interests/${interestId}/outcome`, {
method: 'POST',
body: { outcome, reason: reason || undefined },
}),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['interests', interestId] });
queryClient.invalidateQueries({ queryKey: ['interests'] });
onOpenChange(false);
setReason('');
},
});
function handleOpenChange(next: boolean) {
if (!next) {
setReason('');
setOutcome(choices[0]!);
}
onOpenChange(next);
}
return (
<Dialog open={open} onOpenChange={handleOpenChange}>
<DialogContent className="sm:max-w-md">
<DialogHeader>
<DialogTitle className="flex items-center gap-2">
{mode === 'won' ? (
<Trophy className="h-4 w-4 text-emerald-600" aria-hidden />
) : (
<XCircle className="h-4 w-4 text-rose-600" aria-hidden />
)}
{mode === 'won' ? 'Mark interest as won' : 'Close interest as lost'}
</DialogTitle>
</DialogHeader>
<div className="space-y-4 py-2">
{mode === 'lost' ? (
<div className="space-y-1">
<Label>Reason</Label>
<Select value={outcome} onValueChange={(v) => setOutcome(v as InterestOutcome)}>
<SelectTrigger>
<SelectValue />
</SelectTrigger>
<SelectContent>
{LOST_OUTCOMES.map((o) => (
<SelectItem key={o} value={o}>
{OUTCOME_LABELS[o]}
</SelectItem>
))}
</SelectContent>
</Select>
</div>
) : null}
<div className="space-y-1">
<Label htmlFor="outcome-reason">Notes (optional)</Label>
<Textarea
id="outcome-reason"
value={reason}
onChange={(e) => setReason(e.target.value)}
placeholder={
mode === 'won'
? 'Anything notable about the win? (visible in timeline + reports)'
: 'What happened? (visible in timeline + reports)'
}
rows={3}
/>
</div>
<p className="text-xs text-muted-foreground">
{mode === 'won' ? (
<>
This will mark the interest as <strong>Won</strong>. The pipeline stage stays where
it is; the outcome flag is set. You can clear the outcome later to reopen.
</>
) : (
<>
This will close the interest with a <strong>Lost</strong> outcome. The pipeline
stage stays where it is. You can clear the outcome later to reopen.
</>
)}
</p>
</div>
<DialogFooter>
<Button
variant="outline"
onClick={() => handleOpenChange(false)}
disabled={mutation.isPending}
>
Cancel
</Button>
<Button
onClick={() => mutation.mutate()}
disabled={mutation.isPending}
className={
mode === 'won'
? 'bg-emerald-600 hover:bg-emerald-700'
: 'bg-rose-600 hover:bg-rose-700'
}
>
{mutation.isPending && <Loader2 className="mr-2 h-4 w-4 animate-spin" aria-hidden />}
{mode === 'won' ? 'Mark as won' : 'Close as lost'}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}