Files
repo-home/apps/frontend/components/article/articleList.tsx
T

87 lines
2.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use client';
import { useRouter } from 'next/navigation';
import { useState } from 'react';
import { Article } from '../../types/catalog';
import { articleDelete } from '../../actions/article.actions';
import { sites } from '../../_config/sites';
import Link from 'next/link';
export const ArticleList = ({ articles }: { articles: Article[] }) => {
const router = useRouter();
const [error, setError] = useState('');
const [pendingId, setPendingId] = useState<string | null>(null);
const archive = async (id: string) => {
setPendingId(id);
setError('');
const result = await articleDelete(id);
if (!result.success) setError(result.errors.join(' · '));
else router.refresh();
setPendingId(null);
};
return (
<div className="overflow-auto">
{error && (
<p role="alert" className="text-red-700">
{error}
</p>
)}
<table className="w-full text-left">
<thead>
<tr>
<th>Nummer</th>
<th>Artikel</th>
<th>Hersteller</th>
<th>Einheit</th>
<th>Bestandsführung</th>
<th>Aktionen</th>
</tr>
</thead>
<tbody>
{articles.map((article) => (
<tr key={article._id} className="border-b">
<td className="p-2">{article.articleNumber}</td>
<td className="p-2">
{article.name}
{article.group && (
<span className="block text-sm text-gray-500">
{article.group.name}
</span>
)}
</td>
<td className="p-2">{article.manufacturer?.name ?? '–'}</td>
<td className="p-2">{article.baseUnit}</td>
<td className="p-2">
{
{
quantity: 'Menge',
batch: 'Charge',
individual: 'Einzelstücke',
}[article.trackingMode]
}
</td>
<td className="p-2">
<Link
className="text-sky-700 mr-3"
href={`${sites.article.update.url}/${article._id}`}
>
Bearbeiten
</Link>
<button
disabled={pendingId !== null}
onClick={() => archive(article._id)}
>
{pendingId === article._id ? 'Archiviert …' : 'Archivieren'}
</button>
</td>
</tr>
))}
</tbody>
</table>
{articles.length === 0 && (
<p className="py-4">Noch keine Artikel vorhanden.</p>
)}
</div>
);
};