Artikel DB struktur geändert von CODEX
This commit is contained in:
1 parent
0c0a678eaa
commit
9c18234efe
95 files changed
+3872
-2316
No files matched your search
@@ -1,85 +1,65 @@
|
||||
"use client";
|
||||
'use client';
|
||||
|
||||
import { ArticleCreate, ArticleUpsert, Manufacturer } from "../../types/catalog"
|
||||
import { useFieldArray, useFormContext } from "react-hook-form"
|
||||
import { InputTextR } from "../ui/inputTextR";
|
||||
import ComboBoxRHF from "../ui/ComboBoxR";
|
||||
import { useState } from "react";
|
||||
import Modal from "../ui/modal";
|
||||
import { ManufactuererForm } from "../manufacturer/manufacturer.form";
|
||||
import SelectAndSearch2 from "../ui/SelectAndSearch2";
|
||||
|
||||
|
||||
interface Params {
|
||||
manufacturer: Manufacturer[]
|
||||
onOpenManufacturer: (index: number) => void
|
||||
}
|
||||
|
||||
export const ArticleVariantForm = ({ manufacturer, onOpenManufacturer }: Params) => {
|
||||
|
||||
const { control, register, getValues, setValue } = useFormContext<ArticleUpsert>();
|
||||
const { fields, append, remove } = useFieldArray({ control, name: "variants", });
|
||||
const [creatManufacturer, setCreatManufacturer] = useState(false);
|
||||
|
||||
const handleRemove = (index: number) => {
|
||||
|
||||
const id = getValues(`variants.${index}._id`);
|
||||
if (!id) {
|
||||
remove(index);
|
||||
return;
|
||||
}
|
||||
const x = getValues(`variants.${index}.deleted`) ? true : false;
|
||||
|
||||
setValue(`variants.${index}.deleted`, !x, { shouldDirty: true })
|
||||
}
|
||||
|
||||
const hersteller = manufacturer.map((i) => ({ id: i._id, value: i.name }))
|
||||
|
||||
return (
|
||||
<div className="mt-4 border-1 rounded border-gray-200 p-2">
|
||||
|
||||
<div>
|
||||
<button type="button" onClick={() => append({ manufacturerId: "", packagingUnit: "1" })} className="bg-sky-400 rounded-md p-1 mt-2 mb-2 ps-2 pe-2">
|
||||
+ Neue Variante
|
||||
</button>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
<div className="flex flex-col gap-2 mb-2">
|
||||
<div className="grid grid-cols-12 gap-4 ">
|
||||
<div className="col-span-4" >
|
||||
Hersteller
|
||||
</div>
|
||||
<div className="col-span-3" >
|
||||
Packungseinheit
|
||||
</div>
|
||||
<div className="col-span-3" >
|
||||
Aktion
|
||||
</div>
|
||||
</div>
|
||||
<div className="border-1"></div>
|
||||
{fields.map((field, index) => (
|
||||
<div key={field.id} className="grid grid-cols-12 gap-4 ">
|
||||
<div className="col-span-4" >
|
||||
<ComboBoxRHF name={`variants.${index}.manufacturerId`} control={control} options={hersteller} placeholder="wählen..." />
|
||||
|
||||
<button onClick={() => onOpenManufacturer(index)}>+</button>
|
||||
</div>
|
||||
<div className="col-span-3" >
|
||||
<InputTextR {...register(`variants.${index}.packagingUnit`)} />
|
||||
</div>
|
||||
<div className="col-span-3" >
|
||||
<button type="button" onClick={() => handleRemove(index)} className={`bg-gray-200 rounded-md ps-2 pe-2 `}>
|
||||
löschen
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
import { ArticleCreate } from '../../types/catalog';
|
||||
import { useFieldArray, useFormContext } from 'react-hook-form';
|
||||
import { InputTextR } from '../ui/inputTextR';
|
||||
|
||||
export const ArticlePackagingForm = () => {
|
||||
const {
|
||||
control,
|
||||
register,
|
||||
watch,
|
||||
formState: { errors },
|
||||
} = useFormContext<ArticleCreate>();
|
||||
const { fields, append, remove } = useFieldArray({
|
||||
control,
|
||||
name: 'packaging',
|
||||
});
|
||||
return (
|
||||
<fieldset className="border rounded p-3 mt-4">
|
||||
<legend>Verpackungen (optional)</legend>
|
||||
<p className="text-sm mb-2">
|
||||
Der Bestand wird in {watch('baseUnit') || 'der Basiseinheit'} geführt.
|
||||
Hier hinterlegst du den Inhalt einer Verpackung.
|
||||
</p>
|
||||
{fields.map((field, index) => (
|
||||
<div key={field.id} className="grid gap-2 md:grid-cols-4 mb-3">
|
||||
<InputTextR
|
||||
label="Bezeichnung"
|
||||
{...register(`packaging.${index}.name`, { required: true })}
|
||||
/>
|
||||
<InputTextR
|
||||
label="Inhalt in Basiseinheiten"
|
||||
type="number"
|
||||
step="any"
|
||||
min="0.000001"
|
||||
{...register(`packaging.${index}.quantityInBaseUnits`, {
|
||||
valueAsNumber: true,
|
||||
required: true,
|
||||
min: Number.MIN_VALUE,
|
||||
})}
|
||||
/>
|
||||
<InputTextR
|
||||
label="Barcode"
|
||||
{...register(`packaging.${index}.barcode`)}
|
||||
/>
|
||||
<button type="button" onClick={() => remove(index)}>
|
||||
Verpackung entfernen
|
||||
</button>
|
||||
</div>
|
||||
|
||||
|
||||
)
|
||||
}
|
||||
))}
|
||||
{errors.packaging && (
|
||||
<p role="alert" className="text-red-700">
|
||||
Jede Verpackung benötigt eine Bezeichnung und einen positiven Inhalt.
|
||||
</p>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
className="border rounded px-3 py-1"
|
||||
onClick={() => append({ name: '', quantityInBaseUnits: 1 })}
|
||||
>
|
||||
Verpackung hinzufügen
|
||||
</button>
|
||||
</fieldset>
|
||||
);
|
||||
};
|
||||
Reference in new issue
Block a user