Implement admin ports and system settings management
- Port CRUD: list, create, update with branding, currency, timezone - System settings: upsert key-value pairs per port with known settings UI (AI feature flags, invoice discount, pipeline weights, berth rules) - Settings manager with toggle switches, number inputs, and JSON editors - Replace both stub pages with real implementations Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
209
src/components/admin/ports/port-form.tsx
Normal file
209
src/components/admin/ports/port-form.tsx
Normal file
@@ -0,0 +1,209 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Switch } from '@/components/ui/switch';
|
||||
import { Sheet, SheetContent, SheetHeader, SheetTitle, SheetFooter } from '@/components/ui/sheet';
|
||||
import { apiFetch } from '@/lib/api/client';
|
||||
|
||||
interface PortFormProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
port?: {
|
||||
id: string;
|
||||
name: string;
|
||||
slug: string;
|
||||
logoUrl: string | null;
|
||||
primaryColor: string | null;
|
||||
defaultCurrency: string;
|
||||
timezone: string;
|
||||
isActive: boolean;
|
||||
} | null;
|
||||
onSuccess: () => void;
|
||||
}
|
||||
|
||||
export function PortForm({ open, onOpenChange, port, onSuccess }: PortFormProps) {
|
||||
const [name, setName] = useState('');
|
||||
const [slug, setSlug] = useState('');
|
||||
const [primaryColor, setPrimaryColor] = useState('#0F4C81');
|
||||
const [defaultCurrency, setDefaultCurrency] = useState('USD');
|
||||
const [timezone, setTimezone] = useState('America/Anguilla');
|
||||
const [isActive, setIsActive] = useState(true);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const isEdit = !!port;
|
||||
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
if (port) {
|
||||
setName(port.name);
|
||||
setSlug(port.slug);
|
||||
setPrimaryColor(port.primaryColor ?? '#0F4C81');
|
||||
setDefaultCurrency(port.defaultCurrency);
|
||||
setTimezone(port.timezone);
|
||||
setIsActive(port.isActive);
|
||||
} else {
|
||||
setName('');
|
||||
setSlug('');
|
||||
setPrimaryColor('#0F4C81');
|
||||
setDefaultCurrency('USD');
|
||||
setTimezone('America/Anguilla');
|
||||
setIsActive(true);
|
||||
}
|
||||
setError(null);
|
||||
}
|
||||
}, [open, port]);
|
||||
|
||||
function handleNameChange(value: string) {
|
||||
setName(value);
|
||||
if (!isEdit) {
|
||||
setSlug(
|
||||
value
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]+/g, '-')
|
||||
.replace(/^-|-$/g, ''),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSubmit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setError(null);
|
||||
setLoading(true);
|
||||
|
||||
try {
|
||||
if (isEdit) {
|
||||
await apiFetch(`/api/v1/admin/ports/${port.id}`, {
|
||||
method: 'PATCH',
|
||||
body: { name, slug, primaryColor, defaultCurrency, timezone, isActive },
|
||||
});
|
||||
} else {
|
||||
await apiFetch('/api/v1/admin/ports', {
|
||||
method: 'POST',
|
||||
body: { name, slug, primaryColor, defaultCurrency, timezone },
|
||||
});
|
||||
}
|
||||
onSuccess();
|
||||
onOpenChange(false);
|
||||
} catch (err: unknown) {
|
||||
const message = err instanceof Error ? err.message : 'Something went wrong';
|
||||
setError(message);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<Sheet open={open} onOpenChange={onOpenChange}>
|
||||
<SheetContent className="overflow-y-auto">
|
||||
<SheetHeader>
|
||||
<SheetTitle>{isEdit ? 'Edit Port' : 'New Port'}</SheetTitle>
|
||||
</SheetHeader>
|
||||
|
||||
<form onSubmit={handleSubmit} className="mt-6 space-y-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="port-name">Name</Label>
|
||||
<Input
|
||||
id="port-name"
|
||||
value={name}
|
||||
onChange={(e) => handleNameChange(e.target.value)}
|
||||
placeholder="Port Nimara"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="port-slug">Slug</Label>
|
||||
<Input
|
||||
id="port-slug"
|
||||
value={slug}
|
||||
onChange={(e) => setSlug(e.target.value)}
|
||||
placeholder="port-nimara"
|
||||
pattern="^[a-z0-9-]+$"
|
||||
required
|
||||
/>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Used in URLs. Lowercase letters, numbers, and hyphens only.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="port-color">Brand Color</Label>
|
||||
<div className="flex items-center gap-2">
|
||||
<input
|
||||
type="color"
|
||||
id="port-color"
|
||||
value={primaryColor}
|
||||
onChange={(e) => setPrimaryColor(e.target.value)}
|
||||
className="h-9 w-9 rounded border cursor-pointer"
|
||||
/>
|
||||
<Input
|
||||
value={primaryColor}
|
||||
onChange={(e) => setPrimaryColor(e.target.value)}
|
||||
placeholder="#0F4C81"
|
||||
className="w-28 font-mono text-sm"
|
||||
maxLength={7}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-2 gap-4">
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="port-currency">Currency</Label>
|
||||
<Input
|
||||
id="port-currency"
|
||||
value={defaultCurrency}
|
||||
onChange={(e) => setDefaultCurrency(e.target.value.toUpperCase())}
|
||||
placeholder="USD"
|
||||
maxLength={3}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="port-timezone">Timezone</Label>
|
||||
<Input
|
||||
id="port-timezone"
|
||||
value={timezone}
|
||||
onChange={(e) => setTimezone(e.target.value)}
|
||||
placeholder="America/Anguilla"
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{isEdit && (
|
||||
<div className="flex items-center justify-between rounded-lg border p-3">
|
||||
<div>
|
||||
<Label htmlFor="port-active">Port Active</Label>
|
||||
<p className="text-xs text-muted-foreground">
|
||||
Inactive ports are hidden from users
|
||||
</p>
|
||||
</div>
|
||||
<Switch id="port-active" checked={isActive} onCheckedChange={setIsActive} />
|
||||
</div>
|
||||
)}
|
||||
|
||||
{error && <p className="text-sm text-destructive">{error}</p>}
|
||||
|
||||
<SheetFooter>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => onOpenChange(false)}
|
||||
disabled={loading}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button type="submit" disabled={loading || !name.trim() || !slug.trim()}>
|
||||
{loading ? 'Saving...' : isEdit ? 'Save Changes' : 'Create Port'}
|
||||
</Button>
|
||||
</SheetFooter>
|
||||
</form>
|
||||
</SheetContent>
|
||||
</Sheet>
|
||||
);
|
||||
}
|
||||
147
src/components/admin/ports/port-list.tsx
Normal file
147
src/components/admin/ports/port-list.tsx
Normal file
@@ -0,0 +1,147 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { type ColumnDef } from '@tanstack/react-table';
|
||||
import { Pencil, Plus } from 'lucide-react';
|
||||
|
||||
import { DataTable } from '@/components/shared/data-table';
|
||||
import { PageHeader } from '@/components/shared/page-header';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Badge } from '@/components/ui/badge';
|
||||
import { apiFetch } from '@/lib/api/client';
|
||||
import { PortForm } from './port-form';
|
||||
|
||||
interface PortRow {
|
||||
id: string;
|
||||
name: string;
|
||||
slug: string;
|
||||
logoUrl: string | null;
|
||||
primaryColor: string | null;
|
||||
defaultCurrency: string;
|
||||
timezone: string;
|
||||
isActive: boolean;
|
||||
settings: Record<string, unknown>;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export function PortList() {
|
||||
const [ports, setPorts] = useState<PortRow[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [formOpen, setFormOpen] = useState(false);
|
||||
const [editingPort, setEditingPort] = useState<PortRow | null>(null);
|
||||
|
||||
const fetchPorts = useCallback(async () => {
|
||||
setLoading(true);
|
||||
try {
|
||||
const res = await apiFetch<{ data: PortRow[] }>('/api/v1/admin/ports');
|
||||
setPorts(res.data);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
void fetchPorts();
|
||||
}, [fetchPorts]);
|
||||
|
||||
function handleNewPort() {
|
||||
setEditingPort(null);
|
||||
setFormOpen(true);
|
||||
}
|
||||
|
||||
function handleEditPort(port: PortRow) {
|
||||
setEditingPort(port);
|
||||
setFormOpen(true);
|
||||
}
|
||||
|
||||
const columns: ColumnDef<PortRow, unknown>[] = [
|
||||
{
|
||||
accessorKey: 'name',
|
||||
header: 'Name',
|
||||
cell: ({ row }) => (
|
||||
<div className="flex items-center gap-2">
|
||||
{row.original.primaryColor && (
|
||||
<span
|
||||
className="inline-block h-3 w-3 rounded-full"
|
||||
style={{ backgroundColor: row.original.primaryColor }}
|
||||
/>
|
||||
)}
|
||||
<span className="font-medium">{row.original.name}</span>
|
||||
</div>
|
||||
),
|
||||
},
|
||||
{
|
||||
accessorKey: 'slug',
|
||||
header: 'Slug',
|
||||
cell: ({ row }) => (
|
||||
<code className="text-xs bg-muted px-1.5 py-0.5 rounded">{row.original.slug}</code>
|
||||
),
|
||||
},
|
||||
{
|
||||
accessorKey: 'defaultCurrency',
|
||||
header: 'Currency',
|
||||
},
|
||||
{
|
||||
accessorKey: 'timezone',
|
||||
header: 'Timezone',
|
||||
},
|
||||
{
|
||||
accessorKey: 'isActive',
|
||||
header: 'Status',
|
||||
cell: ({ row }) =>
|
||||
row.original.isActive ? (
|
||||
<Badge variant="default" className="bg-green-600">
|
||||
Active
|
||||
</Badge>
|
||||
) : (
|
||||
<Badge variant="destructive">Inactive</Badge>
|
||||
),
|
||||
},
|
||||
{
|
||||
id: 'actions',
|
||||
header: '',
|
||||
cell: ({ row }) => (
|
||||
<Button variant="ghost" size="sm" onClick={() => handleEditPort(row.original)}>
|
||||
<Pencil className="h-4 w-4" />
|
||||
<span className="sr-only">Edit</span>
|
||||
</Button>
|
||||
),
|
||||
enableSorting: false,
|
||||
size: 60,
|
||||
},
|
||||
];
|
||||
|
||||
return (
|
||||
<div>
|
||||
<PageHeader
|
||||
title="Port Management"
|
||||
description="Manage marina ports and their configuration"
|
||||
actions={
|
||||
<Button onClick={handleNewPort}>
|
||||
<Plus className="mr-1.5 h-4 w-4" />
|
||||
New Port
|
||||
</Button>
|
||||
}
|
||||
/>
|
||||
|
||||
<DataTable
|
||||
columns={columns}
|
||||
data={ports}
|
||||
isLoading={loading}
|
||||
getRowId={(row) => row.id}
|
||||
emptyState={
|
||||
<div className="text-center py-8">
|
||||
<p className="text-muted-foreground">No ports configured.</p>
|
||||
</div>
|
||||
}
|
||||
/>
|
||||
|
||||
<PortForm
|
||||
open={formOpen}
|
||||
onOpenChange={setFormOpen}
|
||||
port={editingPort}
|
||||
onSuccess={fetchPorts}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user