352 lines
11 KiB
TypeScript
352 lines
11 KiB
TypeScript
import { useState } from 'react';
|
|
import { Link, useNavigate, useParams } from 'react-router-dom';
|
|
import { useQuery, useQueryClient } from '@tanstack/react-query';
|
|
import { Download, ShieldCheck } from 'lucide-react';
|
|
import { api, apiBlob } from '../api/client';
|
|
import type { ClaimStatus, GymClaim } from '../api/types';
|
|
import {
|
|
BackButton,
|
|
Badge,
|
|
Button,
|
|
Card,
|
|
ConfirmDialog,
|
|
EmptyState,
|
|
ErrorText,
|
|
Field,
|
|
ListRow,
|
|
Loading,
|
|
Muted,
|
|
PageHeader,
|
|
SectionHeader,
|
|
Textarea,
|
|
} from '../components/ui';
|
|
import type { BadgeColor } from '../components/ui';
|
|
import { Avatar } from '../components/Avatar';
|
|
import { formatAvailability } from '../lib/availability';
|
|
import { useTopbar } from '../topbar';
|
|
|
|
const STATUS_META: Record<ClaimStatus, { label: string; color: BadgeColor }> = {
|
|
PENDING: { label: 'Oczekuje', color: 'yellow' },
|
|
APPROVED: { label: 'Zatwierdzony', color: 'green' },
|
|
REJECTED: { label: 'Odrzucony', color: 'red' },
|
|
};
|
|
|
|
function claimTitle(claim: GymClaim) {
|
|
return claim.gym?.name ?? claim.gymName ?? '—';
|
|
}
|
|
|
|
function claimTypeLabel(claim: GymClaim) {
|
|
return claim.type === 'verification' ? 'Weryfikacja' : 'Nowa siłownia';
|
|
}
|
|
|
|
function applicantName(claim: GymClaim) {
|
|
const { firstName, lastName, email } = claim.user;
|
|
return [firstName, lastName].filter(Boolean).join(' ') || email;
|
|
}
|
|
|
|
/** Pobranie pliku przez `apiBlob` (Bearer + rotacja 401) → tymczasowy <a download>. */
|
|
async function downloadFile(path: string, filename: string) {
|
|
const blob = await apiBlob(path);
|
|
const url = URL.createObjectURL(blob);
|
|
const a = document.createElement('a');
|
|
a.href = url;
|
|
a.download = filename;
|
|
document.body.appendChild(a);
|
|
a.click();
|
|
a.remove();
|
|
URL.revokeObjectURL(url);
|
|
}
|
|
|
|
function InfoItem({ label, children }: { label: string; children: React.ReactNode }) {
|
|
return (
|
|
<div>
|
|
<div className="text-[11px] font-bold uppercase tracking-[0.1em] text-dim">{label}</div>
|
|
<div className="mt-0.5">{children}</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default function VerificationsList() {
|
|
useTopbar({ title: 'Weryfikacje' });
|
|
const navigate = useNavigate();
|
|
const { data, isLoading, error } = useQuery({
|
|
queryKey: ['gyms-claims'],
|
|
queryFn: () => api<{ claims: GymClaim[] }>('/admin/gyms/claims'),
|
|
});
|
|
|
|
const claims = data?.claims ?? [];
|
|
|
|
return (
|
|
<>
|
|
{isLoading && <Loading />}
|
|
{error && <ErrorText>Błąd: {(error as Error).message}</ErrorText>}
|
|
|
|
<Card>
|
|
<div className="divide-y divide-line">
|
|
{claims.map((c) => (
|
|
<div
|
|
key={c.id}
|
|
className="cursor-pointer"
|
|
onClick={() => navigate(`/verifications/${c.id}`)}
|
|
>
|
|
<ListRow
|
|
title={
|
|
<span className="flex items-center gap-2.5">
|
|
<Avatar name={applicantName(c)} size={28} />
|
|
<Link
|
|
to={`/verifications/${c.id}`}
|
|
className="hover:text-accent-text"
|
|
onClick={(e) => e.stopPropagation()}
|
|
>
|
|
{claimTitle(c)}
|
|
</Link>
|
|
</span>
|
|
}
|
|
meta={`${applicantName(c)} · ${new Date(c.createdAt).toLocaleDateString('pl-PL')}`}
|
|
actions={
|
|
<>
|
|
<Badge color={c.type === 'verification' ? 'blue' : 'gray'}>
|
|
{claimTypeLabel(c)}
|
|
</Badge>
|
|
{c.documentCount ? <Badge color="gray">{c.documentCount} dok.</Badge> : null}
|
|
<Badge color={STATUS_META[c.status].color}>{STATUS_META[c.status].label}</Badge>
|
|
</>
|
|
}
|
|
/>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</Card>
|
|
|
|
{!isLoading && !error && claims.length === 0 && (
|
|
<Card>
|
|
<EmptyState
|
|
icon={ShieldCheck}
|
|
title="Brak wniosków"
|
|
description="Brak wniosków o siłownie."
|
|
/>
|
|
</Card>
|
|
)}
|
|
</>
|
|
);
|
|
}
|
|
|
|
export function VerificationDetail() {
|
|
useTopbar({ title: 'Weryfikacje', back: '/verifications' });
|
|
const { id } = useParams();
|
|
const navigate = useNavigate();
|
|
const queryClient = useQueryClient();
|
|
const { data, isLoading, error } = useQuery({
|
|
queryKey: ['gyms-claims', id],
|
|
queryFn: () => api<{ claim: GymClaim }>(`/admin/gyms/claims/${id}`),
|
|
});
|
|
|
|
const [confirmOpen, setConfirmOpen] = useState(false);
|
|
const [busy, setBusy] = useState(false);
|
|
const [reason, setReason] = useState('');
|
|
const [actionError, setActionError] = useState('');
|
|
|
|
const claim = data?.claim;
|
|
|
|
const invalidate = () =>
|
|
Promise.all([
|
|
queryClient.invalidateQueries({ queryKey: ['gyms-claims'] }),
|
|
queryClient.invalidateQueries({ queryKey: ['gyms'] }),
|
|
]);
|
|
|
|
const approve = async () => {
|
|
if (!claim) return;
|
|
setBusy(true);
|
|
setActionError('');
|
|
try {
|
|
await api(`/admin/gyms/claims/${claim.id}/approve`, { method: 'POST' });
|
|
setConfirmOpen(false);
|
|
await invalidate();
|
|
navigate('/verifications');
|
|
} catch (e) {
|
|
setActionError(e instanceof Error ? e.message : 'Błąd');
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
const reject = async () => {
|
|
if (!claim) return;
|
|
setBusy(true);
|
|
setActionError('');
|
|
try {
|
|
await api(`/admin/gyms/claims/${claim.id}/reject`, {
|
|
method: 'POST',
|
|
body: { reason: reason || undefined },
|
|
});
|
|
await invalidate();
|
|
navigate('/verifications');
|
|
} catch (e) {
|
|
setActionError(e instanceof Error ? e.message : 'Błąd');
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
};
|
|
|
|
const download = async (docId: string, name: string) => {
|
|
if (!claim) return;
|
|
try {
|
|
await downloadFile(`/admin/gyms/claims/${claim.id}/documents/${docId}`, name);
|
|
} catch (e) {
|
|
alert(e instanceof Error ? e.message : 'Błąd pobierania');
|
|
}
|
|
};
|
|
|
|
if (isLoading) return <Loading />;
|
|
if (error) return <ErrorText>Błąd: {(error as Error).message}</ErrorText>;
|
|
if (!claim) {
|
|
return (
|
|
<>
|
|
<BackButton to="/verifications" className="mb-4" />
|
|
<ErrorText>Nie znaleziono wniosku.</ErrorText>
|
|
</>
|
|
);
|
|
}
|
|
|
|
const isPending = claim.status === 'PENDING';
|
|
|
|
return (
|
|
<>
|
|
<PageHeader
|
|
title={claimTitle(claim)}
|
|
badge={
|
|
<>
|
|
<Badge color={claim.type === 'verification' ? 'blue' : 'gray'}>
|
|
{claimTypeLabel(claim)}
|
|
</Badge>
|
|
<Badge color={STATUS_META[claim.status].color}>{STATUS_META[claim.status].label}</Badge>
|
|
</>
|
|
}
|
|
actions={<BackButton to="/verifications" />}
|
|
/>
|
|
|
|
<Card>
|
|
<SectionHeader title="Dane wniosku" />
|
|
<div className="grid grid-cols-1 gap-x-4 gap-y-3 sm:grid-cols-2">
|
|
{claim.type === 'verification' ? (
|
|
<>
|
|
<InfoItem label="Siłownia">
|
|
{claim.gym?.name ?? '—'}
|
|
{claim.gym && (
|
|
<span className="ml-2 inline-flex gap-1.5">
|
|
<Badge color={claim.gym.kind === 'VIRTUAL' ? 'blue' : 'gray'}>
|
|
{claim.gym.kind === 'VIRTUAL' ? 'Wirtualna' : 'Realna'}
|
|
</Badge>
|
|
{claim.gym.isPrivate && <Badge color="gray">Prywatna</Badge>}
|
|
</span>
|
|
)}
|
|
</InfoItem>
|
|
<InfoItem label="Adres (nadpisuje lokalizację)">{claim.address || '—'}</InfoItem>
|
|
</>
|
|
) : (
|
|
<>
|
|
<InfoItem label="Nazwa siłowni">{claim.gymName || '—'}</InfoItem>
|
|
<InfoItem label="Lokalizacja">{claim.gymLocation || '—'}</InfoItem>
|
|
<InfoItem label="Dostępność">
|
|
{formatAvailability(claim.gymAvailability) || '—'}
|
|
</InfoItem>
|
|
<InfoItem label="Pojemność">{claim.gymCapacity ?? '—'}</InfoItem>
|
|
</>
|
|
)}
|
|
<InfoItem label="Opis">{claim.description || '—'}</InfoItem>
|
|
<InfoItem label="Nazwa firmy">{claim.companyName || '—'}</InfoItem>
|
|
<InfoItem label="NIP">{claim.nip || '—'}</InfoItem>
|
|
<InfoItem label="Telefon">{claim.phone || '—'}</InfoItem>
|
|
<InfoItem label="E-mail">{claim.email || '—'}</InfoItem>
|
|
</div>
|
|
</Card>
|
|
|
|
<Card>
|
|
<SectionHeader title="Wnioskodawca" />
|
|
<div className="grid grid-cols-1 gap-x-4 gap-y-3 sm:grid-cols-2">
|
|
<InfoItem label="Imię i nazwisko">
|
|
<span className="flex items-center gap-2.5">
|
|
<Avatar name={applicantName(claim)} size={28} />
|
|
{applicantName(claim)}
|
|
</span>
|
|
</InfoItem>
|
|
<InfoItem label="E-mail">{claim.user.email}</InfoItem>
|
|
{claim.user.phone != null && <InfoItem label="Telefon">{claim.user.phone}</InfoItem>}
|
|
</div>
|
|
</Card>
|
|
|
|
<Card>
|
|
<SectionHeader title="Dokumenty" />
|
|
{claim.documents.length === 0 ? (
|
|
<Muted className="text-[0.875rem]">Brak załączonych dokumentów.</Muted>
|
|
) : (
|
|
<div className="divide-y divide-line">
|
|
{claim.documents.map((d) => (
|
|
<ListRow
|
|
key={d.id}
|
|
title={d.originalName}
|
|
actions={
|
|
<Button
|
|
size="sm"
|
|
variant="secondary"
|
|
onClick={() => void download(d.id, d.originalName)}
|
|
>
|
|
<Download className="h-4 w-4" />
|
|
Pobierz
|
|
</Button>
|
|
}
|
|
/>
|
|
))}
|
|
</div>
|
|
)}
|
|
</Card>
|
|
|
|
{claim.status === 'REJECTED' && claim.rejectReason && (
|
|
<Card>
|
|
<SectionHeader title="Powód odrzucenia" />
|
|
<p className="text-[0.95rem]">{claim.rejectReason}</p>
|
|
</Card>
|
|
)}
|
|
|
|
{isPending && (
|
|
<Card>
|
|
<SectionHeader title="Rozstrzygnięcie" />
|
|
<div className="flex flex-wrap items-center gap-3">
|
|
<Button onClick={() => setConfirmOpen(true)} disabled={busy}>
|
|
<ShieldCheck className="h-4 w-4" />
|
|
Zatwierdź
|
|
</Button>
|
|
<Button variant="danger" onClick={reject} disabled={busy}>
|
|
{busy ? 'Odrzucanie…' : 'Odrzuć'}
|
|
</Button>
|
|
</div>
|
|
<div className="mt-4">
|
|
<Field label="Powód odrzucenia (opcjonalny)">
|
|
<Textarea
|
|
value={reason}
|
|
onChange={(e) => setReason(e.target.value)}
|
|
placeholder="np. Brak potwierdzenia adresu prowadzenia siłowni"
|
|
/>
|
|
</Field>
|
|
</div>
|
|
{actionError && <ErrorText>{actionError}</ErrorText>}
|
|
</Card>
|
|
)}
|
|
|
|
<ConfirmDialog
|
|
open={confirmOpen}
|
|
onClose={() => setConfirmOpen(false)}
|
|
onConfirm={() => void approve()}
|
|
title="Zatwierdzić wniosek?"
|
|
description={
|
|
claim.type === 'verification'
|
|
? 'Siłownia stanie się realną (REAL) i publiczną, a wnioskodawca otrzyma rolę właściciela.'
|
|
: 'Zostanie utworzona nowa realna siłownia, a wnioskodawca otrzyma rolę właściciela.'
|
|
}
|
|
confirmLabel="Zatwierdź"
|
|
busy={busy}
|
|
/>
|
|
</>
|
|
);
|
|
}
|