feat(api): implement new API routes for bulk category updates, inventory consumption, and product management with authentication
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useTransition } from 'react';
|
||||
import { consumeInventoryItem } from './actions';
|
||||
import { useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
type Props = {
|
||||
id: number;
|
||||
@@ -10,8 +10,9 @@ type Props = {
|
||||
|
||||
export default function InventoryConsumeForm({ id, unit }: Props) {
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const [isPending, setIsPending] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const router = useRouter();
|
||||
|
||||
if (!isOpen) {
|
||||
return (
|
||||
@@ -44,14 +45,27 @@ export default function InventoryConsumeForm({ id, unit }: Props) {
|
||||
const { quantity, unit: parsedUnit } = parseQuantityInput(raw, unit);
|
||||
formData.set('amountUsed', String(quantity));
|
||||
formData.set('unit', parsedUnit);
|
||||
startTransition(async () => {
|
||||
try {
|
||||
await consumeInventoryItem(formData);
|
||||
setIsOpen(false);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Okänt fel');
|
||||
const comment = String(formData.get('comment') || '').trim();
|
||||
const payload: Record<string, unknown> = { amountUsed: quantity };
|
||||
if (comment) payload.comment = comment;
|
||||
setIsPending(true);
|
||||
try {
|
||||
const res = await fetch(`/api/admin/inventory-item/${id}/consume`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
throw new Error(data?.error || 'Kunde inte förbruka');
|
||||
}
|
||||
});
|
||||
setIsOpen(false);
|
||||
router.refresh();
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Okänt fel');
|
||||
} finally {
|
||||
setIsPending(false);
|
||||
}
|
||||
}}
|
||||
style={{
|
||||
display: 'grid',
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useTransition } from 'react';
|
||||
import { updateInventoryItem } from './actions';
|
||||
import { useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import type { InventoryItem } from '../../features/inventory/types';
|
||||
import { UNIT_OPTIONS } from '../../lib/units';
|
||||
|
||||
@@ -46,8 +46,9 @@ const LOCATION_OPTIONS = [
|
||||
|
||||
export default function InventoryEditForm({ item }: Props) {
|
||||
const [isEditing, setIsEditing] = useState(false);
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const [isPending, setIsPending] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const router = useRouter();
|
||||
|
||||
if (!isEditing) {
|
||||
return (
|
||||
@@ -79,16 +80,35 @@ export default function InventoryEditForm({ item }: Props) {
|
||||
const raw = formData.get('quantity') as string;
|
||||
const unit = formData.get('unit') as string;
|
||||
const { quantity, unit: parsedUnit } = parseQuantityInput(raw, unit);
|
||||
formData.set('quantity', String(quantity));
|
||||
formData.set('unit', parsedUnit);
|
||||
startTransition(async () => {
|
||||
try {
|
||||
await updateInventoryItem(formData);
|
||||
setIsEditing(false);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Okänt fel');
|
||||
|
||||
const payload: Record<string, unknown> = { opened: formData.get('opened') === 'on' };
|
||||
if (raw) payload.quantity = quantity;
|
||||
if (parsedUnit) payload.unit = parsedUnit;
|
||||
payload.location = String(formData.get('location') || '').trim();
|
||||
payload.brand = String(formData.get('brand') || '').trim();
|
||||
payload.suitableFor = String(formData.get('suitableFor') || '').trim();
|
||||
payload.comment = String(formData.get('comment') || '').trim();
|
||||
const bestBeforeDate = String(formData.get('bestBeforeDate') || '').trim();
|
||||
payload.bestBeforeDate = bestBeforeDate || null;
|
||||
|
||||
setIsPending(true);
|
||||
try {
|
||||
const res = await fetch(`/api/admin/inventory-item/${item.id}`, {
|
||||
method: 'PATCH',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
throw new Error(data?.error || 'Kunde inte uppdatera');
|
||||
}
|
||||
});
|
||||
setIsEditing(false);
|
||||
router.refresh();
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Okänt fel');
|
||||
} finally {
|
||||
setIsPending(false);
|
||||
}
|
||||
}}
|
||||
style={{
|
||||
display: 'grid',
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { useState } from 'react';
|
||||
import { createInventoryItem } from './actions';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import type { Product } from '../../features/inventory/types';
|
||||
import { UNIT_OPTIONS } from '../../lib/units';
|
||||
|
||||
@@ -13,6 +13,7 @@ export default function InventoryForm({ products }: Props) {
|
||||
const [isPending, setIsPending] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const router = useRouter();
|
||||
|
||||
const LOCATION_OPTIONS = [
|
||||
{ value: '', label: 'Välj plats' },
|
||||
@@ -82,8 +83,32 @@ export default function InventoryForm({ products }: Props) {
|
||||
formData.set('quantity', String(quantity));
|
||||
formData.set('unit', parsedUnit);
|
||||
try {
|
||||
await createInventoryItem(formData);
|
||||
const payload: Record<string, unknown> = {
|
||||
productId: Number(formData.get('productId')),
|
||||
quantity,
|
||||
unit: parsedUnit,
|
||||
};
|
||||
const location = String(formData.get('location') || '').trim();
|
||||
if (location) payload.location = location;
|
||||
payload.opened = formData.get('opened') === 'on';
|
||||
const brand = String(formData.get('brand') || '').trim();
|
||||
if (brand) payload.brand = brand;
|
||||
const suitableFor = String(formData.get('suitableFor') || '').trim();
|
||||
if (suitableFor) payload.suitableFor = suitableFor;
|
||||
const bestBeforeDate = String(formData.get('bestBeforeDate') || '').trim();
|
||||
if (bestBeforeDate) payload.bestBeforeDate = bestBeforeDate;
|
||||
|
||||
const res = await fetch('/api/admin/inventory-item', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
throw new Error(data?.error || 'Kunde inte spara');
|
||||
}
|
||||
form.reset();
|
||||
router.refresh();
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Okänt fel');
|
||||
} finally {
|
||||
|
||||
@@ -1,29 +1,40 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useTransition } from 'react';
|
||||
import { createProduct } from './actions';
|
||||
import { useState } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
export default function ProductForm() {
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const [isPending, setIsPending] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const router = useRouter();
|
||||
|
||||
return (
|
||||
<form
|
||||
onSubmit={(e) => {
|
||||
onSubmit={async (e) => {
|
||||
e.preventDefault();
|
||||
setError(null);
|
||||
|
||||
const form = e.currentTarget;
|
||||
const formData = new FormData(form);
|
||||
const name = String((new FormData(form)).get('name') || '').trim();
|
||||
|
||||
startTransition(async () => {
|
||||
try {
|
||||
await createProduct(formData);
|
||||
form.reset();
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Okänt fel');
|
||||
setIsPending(true);
|
||||
try {
|
||||
const res = await fetch('/api/admin/create-product', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ name }),
|
||||
});
|
||||
if (!res.ok) {
|
||||
const data = await res.json().catch(() => ({}));
|
||||
throw new Error(data?.error || 'Kunde inte skapa produkt');
|
||||
}
|
||||
});
|
||||
form.reset();
|
||||
router.refresh();
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'Okänt fel');
|
||||
} finally {
|
||||
setIsPending(false);
|
||||
}
|
||||
}}
|
||||
style={{
|
||||
display: 'grid',
|
||||
|
||||
Reference in New Issue
Block a user