290 lines
14 KiB
TypeScript
290 lines
14 KiB
TypeScript
import React, { useEffect, useState } from 'react';
|
|
import { apiClient } from '../../services/api';
|
|
import { Button } from '../../components/ui/Button';
|
|
import { StatusPill } from '../../components/ui/StatusPill';
|
|
import { Plus, Building2, SlidersHorizontal } from 'lucide-react';
|
|
|
|
/**
|
|
* Platform Tenants Management View.
|
|
*
|
|
* Fulfills FR-TENANTLIFECYCLE-001 (UC-018 / SCREEN-TENANT-001).
|
|
* Styled per DESIGN.md: AgriTrade Mercantile (Tactile Corporate Minimalism).
|
|
*/
|
|
export interface PlatformTenant {
|
|
tenant_id: string;
|
|
business_name: string;
|
|
legal_identifier: string;
|
|
contact_email: string;
|
|
primary_mandi: string;
|
|
status: 'ACTIVE' | 'SUSPENDED' | 'CLOSED';
|
|
created_at: string;
|
|
}
|
|
|
|
export const PlatformTenantsView: React.FC = () => {
|
|
const [tenants, setTenants] = useState<PlatformTenant[]>([]);
|
|
const [loading, setLoading] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
// Provisioning Modal State
|
|
const [showModal, setShowModal] = useState(false);
|
|
const [formData, setFormData] = useState({
|
|
business_name: '',
|
|
legal_identifier: '',
|
|
primary_mandi: '',
|
|
contact_email: '',
|
|
initial_admin_email: '',
|
|
});
|
|
|
|
// Status Change Dialog State
|
|
const [selectedTenant, setSelectedTenant] = useState<PlatformTenant | null>(null);
|
|
const [newStatus, setNewStatus] = useState<string>('SUSPENDED');
|
|
const [statusReason, setStatusReason] = useState('');
|
|
|
|
useEffect(() => {
|
|
fetchTenants();
|
|
}, []);
|
|
|
|
const fetchTenants = async () => {
|
|
setLoading(true);
|
|
setError(null);
|
|
const resp = await apiClient<{ tenants: PlatformTenant[] }>('/api/v1/platform/tenants');
|
|
setLoading(false);
|
|
if (resp.status === 200 && resp.data?.tenants) {
|
|
setTenants(resp.data.tenants);
|
|
} else {
|
|
setError(resp.error || 'Failed to fetch platform tenants.');
|
|
}
|
|
};
|
|
|
|
const handleCreate = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setLoading(true);
|
|
const resp = await apiClient('/api/v1/platform/tenants', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(formData),
|
|
});
|
|
setLoading(false);
|
|
if (resp.status === 201) {
|
|
setShowModal(false);
|
|
setFormData({ business_name: '', legal_identifier: '', primary_mandi: '', contact_email: '', initial_admin_email: '' });
|
|
fetchTenants();
|
|
} else {
|
|
setError(resp.error || 'Failed to provision tenant.');
|
|
}
|
|
};
|
|
|
|
const handleUpdateStatus = async () => {
|
|
if (!selectedTenant) return;
|
|
setLoading(true);
|
|
const resp = await apiClient(`/api/v1/platform/tenants/${selectedTenant.tenant_id}/status`, {
|
|
method: 'PATCH',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ status: newStatus, reason: statusReason }),
|
|
});
|
|
setLoading(false);
|
|
if (resp.status === 200) {
|
|
setSelectedTenant(null);
|
|
setStatusReason('');
|
|
fetchTenants();
|
|
} else {
|
|
setError(resp.error || 'Failed to update tenant status.');
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="space-y-6" data-testid="platform-tenants-view">
|
|
<div className="flex items-center justify-between">
|
|
<div>
|
|
<h2 className="text-xl font-bold tracking-tight text-text-primary">Platform Trading Enterprises</h2>
|
|
<p className="mt-0.5 text-xs text-text-muted font-mono">
|
|
Provision and govern multi-tenant agricultural trading enterprises (FR-TENANTLIFECYCLE-001)
|
|
</p>
|
|
</div>
|
|
<Button
|
|
variant="primary"
|
|
size="sm"
|
|
onClick={() => setShowModal(true)}
|
|
className="flex items-center space-x-1.5 font-mono"
|
|
>
|
|
<Plus className="w-3.5 h-3.5 mr-1 text-accent-ochre" />
|
|
<span>Provision Enterprise</span>
|
|
</Button>
|
|
</div>
|
|
|
|
{error && (
|
|
<div className="bg-status-watch-bg border-l-[3px] border-accent-ochre p-3 rounded text-status-watch-text text-xs font-mono">
|
|
{error}
|
|
</div>
|
|
)}
|
|
|
|
{/* Directory Table adhering to DESIGN.md tabular rules */}
|
|
<div className="bg-card-surface border border-card-border rounded shadow-none overflow-hidden">
|
|
<table className="min-w-full divide-y divide-card-border">
|
|
<thead className="bg-surface-low">
|
|
<tr>
|
|
<th className="py-3 px-4 text-left text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono">Trading Enterprise</th>
|
|
<th className="py-3 px-4 text-left text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono">Legal GSTIN</th>
|
|
<th className="py-3 px-4 text-left text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono">Primary Mandi</th>
|
|
<th className="py-3 px-4 text-left text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono">Contact Email</th>
|
|
<th className="py-3 px-4 text-left text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono">Lifecycle Status</th>
|
|
<th className="py-3 px-4 text-right text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody className="divide-y divide-card-border bg-white text-xs">
|
|
{tenants.map((t) => (
|
|
<tr key={t.tenant_id} className="hover:bg-surface-bright transition-colors">
|
|
<td className="py-3.5 px-4 font-semibold text-text-primary">{t.business_name}</td>
|
|
<td className="py-3.5 px-4 font-mono text-text-muted">{t.legal_identifier}</td>
|
|
<td className="py-3.5 px-4 font-mono text-text-primary">{t.primary_mandi}</td>
|
|
<td className="py-3.5 px-4 text-text-muted">{t.contact_email}</td>
|
|
<td className="py-3.5 px-4">
|
|
<StatusPill
|
|
status={t.status === 'ACTIVE' ? 'normal' : t.status === 'SUSPENDED' ? 'watch' : 'neutral'}
|
|
label={t.status}
|
|
/>
|
|
</td>
|
|
<td className="py-3.5 px-4 text-right font-mono">
|
|
{t.status !== 'CLOSED' && (
|
|
<button
|
|
onClick={() => { setSelectedTenant(t); setNewStatus(t.status === 'ACTIVE' ? 'SUSPENDED' : 'ACTIVE'); }}
|
|
className="text-xs text-text-primary hover:text-accent-ochre font-medium underline underline-offset-2 transition"
|
|
>
|
|
Update State
|
|
</button>
|
|
)}
|
|
</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
{/* Provision Modal */}
|
|
{showModal && (
|
|
<div className="fixed z-50 inset-0 overflow-y-auto">
|
|
<div className="flex items-center justify-center min-h-screen px-4 text-center sm:p-0">
|
|
<div className="fixed inset-0 bg-sidebar-surface/40 transition-opacity" onClick={() => setShowModal(false)} />
|
|
<div className="inline-block bg-card-surface rounded border border-card-border p-6 text-left overflow-hidden shadow-tactile transform transition-all sm:max-w-md sm:w-full z-10">
|
|
<h3 className="text-base font-bold text-text-primary tracking-tight">Provision New Trading Enterprise</h3>
|
|
<p className="text-xs text-text-muted mt-0.5 font-mono">Registers enterprise entity within row-level multi-tenant database boundary</p>
|
|
|
|
<form onSubmit={handleCreate} className="mt-4 space-y-3.5">
|
|
<div>
|
|
<label className="block text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono mb-1">Business Legal Name</label>
|
|
<input
|
|
type="text"
|
|
required
|
|
value={formData.business_name}
|
|
onChange={(e) => setFormData({ ...formData, business_name: e.target.value })}
|
|
className="w-full bg-white border border-card-border rounded px-3 py-2 text-xs focus:outline-none focus:border-sidebar-surface focus:ring-2 focus:ring-secondary-amber/20"
|
|
placeholder="e.g. Rameshwaram Grain Mercantile Pvt Ltd"
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="block text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono mb-1">Legal Identifier / GSTIN</label>
|
|
<input
|
|
type="text"
|
|
required
|
|
value={formData.legal_identifier}
|
|
onChange={(e) => setFormData({ ...formData, legal_identifier: e.target.value })}
|
|
className="w-full bg-white border border-card-border rounded px-3 py-2 text-xs font-mono focus:outline-none focus:border-sidebar-surface focus:ring-2 focus:ring-secondary-amber/20"
|
|
placeholder="23AAAAA0000A1Z5"
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="block text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono mb-1">Primary Mandi APMC Hub</label>
|
|
<input
|
|
type="text"
|
|
required
|
|
value={formData.primary_mandi}
|
|
onChange={(e) => setFormData({ ...formData, primary_mandi: e.target.value })}
|
|
className="w-full bg-white border border-card-border rounded px-3 py-2 text-xs font-mono focus:outline-none focus:border-sidebar-surface focus:ring-2 focus:ring-secondary-amber/20"
|
|
placeholder="APMC-INDORE"
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="block text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono mb-1">Corporate Contact Email</label>
|
|
<input
|
|
type="email"
|
|
required
|
|
value={formData.contact_email}
|
|
onChange={(e) => setFormData({ ...formData, contact_email: e.target.value })}
|
|
className="w-full bg-white border border-card-border rounded px-3 py-2 text-xs focus:outline-none focus:border-sidebar-surface focus:ring-2 focus:ring-secondary-amber/20"
|
|
placeholder="ops@rameshwaramgrain.com"
|
|
/>
|
|
</div>
|
|
<div>
|
|
<label className="block text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono mb-1">Initial Tenant Administrator Email</label>
|
|
<input
|
|
type="email"
|
|
value={formData.initial_admin_email}
|
|
onChange={(e) => setFormData({ ...formData, initial_admin_email: e.target.value })}
|
|
className="w-full bg-white border border-card-border rounded px-3 py-2 text-xs focus:outline-none focus:border-sidebar-surface focus:ring-2 focus:ring-secondary-amber/20"
|
|
placeholder="admin@rameshwaramgrain.com"
|
|
/>
|
|
</div>
|
|
<div className="pt-3 border-t border-card-border flex justify-end space-x-2">
|
|
<Button type="button" variant="secondary" size="sm" onClick={() => setShowModal(false)}>
|
|
Cancel
|
|
</Button>
|
|
<Button type="submit" variant="primary" size="sm" disabled={loading}>
|
|
Provision Enterprise
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
{/* Status Change Dialog */}
|
|
{selectedTenant && (
|
|
<div className="fixed z-50 inset-0 overflow-y-auto">
|
|
<div className="flex items-center justify-center min-h-screen px-4 text-center sm:p-0">
|
|
<div className="fixed inset-0 bg-sidebar-surface/40 transition-opacity" onClick={() => setSelectedTenant(null)} />
|
|
<div className="inline-block bg-card-surface rounded border border-card-border p-6 text-left overflow-hidden shadow-tactile transform transition-all sm:max-w-md sm:w-full z-10">
|
|
<h3 className="text-base font-bold text-text-primary tracking-tight">Update Lifecycle Status</h3>
|
|
<p className="mt-0.5 text-xs text-text-muted font-mono">
|
|
Modifying state for <strong>{selectedTenant.business_name}</strong>
|
|
</p>
|
|
<div className="mt-4 space-y-3.5">
|
|
<div>
|
|
<label className="block text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono mb-1">Target Lifecycle State</label>
|
|
<select
|
|
value={newStatus}
|
|
onChange={(e) => setNewStatus(e.target.value)}
|
|
className="w-full bg-white border border-card-border rounded px-3 py-2 text-xs font-mono focus:outline-none focus:border-sidebar-surface focus:ring-2 focus:ring-secondary-amber/20"
|
|
>
|
|
<option value="ACTIVE">ACTIVE</option>
|
|
<option value="SUSPENDED">SUSPENDED</option>
|
|
<option value="CLOSED">CLOSED (Terminal State)</option>
|
|
</select>
|
|
</div>
|
|
<div>
|
|
<label className="block text-[11px] font-semibold text-text-muted uppercase tracking-wider font-mono mb-1">Statutory Justification / Audit Note</label>
|
|
<textarea
|
|
rows={2}
|
|
value={statusReason}
|
|
onChange={(e) => setStatusReason(e.target.value)}
|
|
className="w-full bg-white border border-card-border rounded px-3 py-2 text-xs focus:outline-none focus:border-sidebar-surface focus:ring-2 focus:ring-secondary-amber/20"
|
|
placeholder="Enter compliance or audit reasoning..."
|
|
/>
|
|
</div>
|
|
<div className="pt-3 border-t border-card-border flex justify-end space-x-2">
|
|
<Button type="button" variant="secondary" size="sm" onClick={() => setSelectedTenant(null)}>
|
|
Cancel
|
|
</Button>
|
|
<Button type="button" variant="accent" size="sm" onClick={handleUpdateStatus} disabled={loading}>
|
|
Save Transition
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
};
|