C4: Add SettingsSequences.tsx — CRUD UI for sequences (no edit of next_number)
This commit is contained in:
@@ -0,0 +1,138 @@
|
|||||||
|
import React, { useState, useCallback } from 'react';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { apiGet, apiPost, apiPatch, apiDelete } from '@/api/client';
|
||||||
|
|
||||||
|
interface Sequence {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
prefix: string;
|
||||||
|
next_number: number;
|
||||||
|
padding: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function SettingsSequencesPage() {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const [sequences, setSequences] = useState<Sequence[]>([]);
|
||||||
|
const [loading, setLoading] = useState(true);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
const [showForm, setShowForm] = useState(false);
|
||||||
|
const [editing, setEditing] = useState<Sequence | null>(null);
|
||||||
|
const [formData, setFormData] = useState({ name: '', prefix: '', padding: 4 });
|
||||||
|
|
||||||
|
const fetchSequences = useCallback(async () => {
|
||||||
|
setLoading(true);
|
||||||
|
setError(null);
|
||||||
|
try {
|
||||||
|
const data = await apiGet<{ items: Sequence[]; total: number }>('/sequences');
|
||||||
|
setSequences(data.items);
|
||||||
|
} catch (err: any) {
|
||||||
|
setError(err.message || t('common.error'));
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
}, [t]);
|
||||||
|
|
||||||
|
React.useEffect(() => { fetchSequences(); }, [fetchSequences]);
|
||||||
|
|
||||||
|
const handleSave = async (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
try {
|
||||||
|
if (editing) {
|
||||||
|
await apiPatch(`/sequences/${editing.id}`, formData);
|
||||||
|
} else {
|
||||||
|
await apiPost('/sequences', formData);
|
||||||
|
}
|
||||||
|
setShowForm(false);
|
||||||
|
setEditing(null);
|
||||||
|
setFormData({ name: '', prefix: '', padding: 4 });
|
||||||
|
await fetchSequences();
|
||||||
|
} catch (err: any) {
|
||||||
|
setError(err.message || t('common.error'));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDelete = async (id: string) => {
|
||||||
|
if (!window.confirm(t('sequences.confirmDelete'))) return;
|
||||||
|
try {
|
||||||
|
await apiDelete(`/sequences/${id}`);
|
||||||
|
await fetchSequences();
|
||||||
|
} catch (err: any) {
|
||||||
|
setError(err.message || t('common.error'));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleEdit = (seq: Sequence) => {
|
||||||
|
setEditing(seq);
|
||||||
|
setFormData({ name: seq.name, prefix: seq.prefix, padding: seq.padding });
|
||||||
|
setShowForm(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (loading) return <div className="text-secondary-500">{t('common.loading')}</div>;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-6" data-testid="settings-sequences">
|
||||||
|
<div className="flex items-center justify-between">
|
||||||
|
<h1 className="text-2xl font-bold text-secondary-900">{t('sequences.title')}</h1>
|
||||||
|
<button
|
||||||
|
onClick={() => { setEditing(null); setFormData({ name: '', prefix: '', padding: 4 }); setShowForm(true); }}
|
||||||
|
className="px-4 py-2 text-sm font-medium text-white bg-primary-600 rounded-lg hover:bg-primary-700"
|
||||||
|
>
|
||||||
|
{t('sequences.add')}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
{error && <div className="p-3 text-sm text-red-700 bg-red-50 rounded-lg">{error}</div>}
|
||||||
|
|
||||||
|
{showForm && (
|
||||||
|
<form onSubmit={handleSave} className="p-4 border border-secondary-200 rounded-lg space-y-3">
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-secondary-700">{t('sequences.name')}</label>
|
||||||
|
<input type="text" value={formData.name} onChange={(e) => setFormData({ ...formData, name: e.target.value })} required className="mt-1 block w-full rounded-md border border-secondary-300 px-3 py-1.5 text-sm" />
|
||||||
|
</div>
|
||||||
|
<div className="grid grid-cols-2 gap-3">
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-secondary-700">{t('sequences.prefix')}</label>
|
||||||
|
<input type="text" value={formData.prefix} onChange={(e) => setFormData({ ...formData, prefix: e.target.value })} placeholder="RE-" className="mt-1 block w-full rounded-md border border-secondary-300 px-3 py-1.5 text-sm" />
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<label className="block text-sm font-medium text-secondary-700">{t('sequences.padding')}</label>
|
||||||
|
<input type="number" min={1} max={10} value={formData.padding} onChange={(e) => setFormData({ ...formData, padding: parseInt(e.target.value) || 4 })} required className="mt-1 block w-full rounded-md border border-secondary-300 px-3 py-1.5 text-sm" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="flex justify-end gap-2">
|
||||||
|
<button type="button" onClick={() => { setShowForm(false); setEditing(null); }} className="px-3 py-1.5 text-sm font-medium text-secondary-700 bg-secondary-100 rounded-lg hover:bg-secondary-200">{t('common.cancel')}</button>
|
||||||
|
<button type="submit" className="px-3 py-1.5 text-sm font-medium text-white bg-primary-600 rounded-lg hover:bg-primary-700">{t('common.save')}</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
)}
|
||||||
|
|
||||||
|
<div className="overflow-hidden border border-secondary-200 rounded-lg">
|
||||||
|
<table className="min-w-full divide-y divide-secondary-200">
|
||||||
|
<thead className="bg-secondary-50">
|
||||||
|
<tr>
|
||||||
|
<th className="px-4 py-2 text-left text-xs font-medium text-secondary-500 uppercase">{t('sequences.name')}</th>
|
||||||
|
<th className="px-4 py-2 text-left text-xs font-medium text-secondary-500 uppercase">{t('sequences.prefix')}</th>
|
||||||
|
<th className="px-4 py-2 text-left text-xs font-medium text-secondary-500 uppercase">{t('sequences.nextNumber')}</th>
|
||||||
|
<th className="px-4 py-2 text-left text-xs font-medium text-secondary-500 uppercase">{t('sequences.padding')}</th>
|
||||||
|
<th className="px-4 py-2"></th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody className="divide-y divide-secondary-200">
|
||||||
|
{sequences.map((seq) => (
|
||||||
|
<tr key={seq.id}>
|
||||||
|
<td className="px-4 py-2 text-sm text-secondary-900">{seq.name}</td>
|
||||||
|
<td className="px-4 py-2 text-sm text-secondary-900">{seq.prefix}</td>
|
||||||
|
<td className="px-4 py-2 text-sm text-secondary-900">{seq.next_number}</td>
|
||||||
|
<td className="px-4 py-2 text-sm text-secondary-900">{seq.padding}</td>
|
||||||
|
<td className="px-4 py-2 text-sm text-right">
|
||||||
|
<button onClick={() => handleEdit(seq)} className="text-secondary-600 hover:underline mr-3">{t('common.edit')}</button>
|
||||||
|
<button onClick={() => handleDelete(seq.id)} className="text-red-600 hover:underline">{t('common.delete')}</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user