109 lines
3.4 KiB
TypeScript
109 lines
3.4 KiB
TypeScript
"use client";
|
|
|
|
import { emptyArticleDto, Article, ArticleCreate, Category, Manufacturer, ArticleVariant } from "../../types/catalog"
|
|
import { FormProvider, useForm } from "react-hook-form"
|
|
import { articleCreate, articleUpdate } from "../../actions/article.actions";
|
|
import { useEffect, useState } from "react";
|
|
import Modal from "../ui/modal";
|
|
import { ManufactuererForm } from "../manufacturer/manufacturer.form";
|
|
import { Location, LocationTreeNode } from "../../types/inventory";
|
|
import { emptyStock, Stock, StockCreate } from "../../types/inventory/stockContent.types";
|
|
import { stockCreate, stockUpdate } from "../../actions/stock.actions";
|
|
import { InputTextR } from "../ui/inputTextR";
|
|
import { LocationView } from "../location/location.view";
|
|
import { LocationTreeView } from "../location/location.treeView";
|
|
|
|
|
|
interface Params {
|
|
categories: Category[]
|
|
article: Article[],
|
|
orte: LocationTreeNode[]
|
|
initData?: Stock
|
|
debug?: boolean
|
|
}
|
|
|
|
|
|
export const StockForm = ({ article, categories, initData, orte, debug }: Params) => {
|
|
|
|
const { register, reset, formState: { errors }, handleSubmit } = useForm<StockCreate>({ defaultValues: emptyStock });
|
|
const [errMsg, setErrorMsg] = useState<string[]>([])
|
|
const [selectetCategory, setSelectedCategory] = useState<Category>()
|
|
const [selectetArticle, setSelectedArticle] = useState<Article>()
|
|
const [selectetArticleVariant, setSelectedArticleVariant] = useState<ArticleVariant>()
|
|
const [selectetOrt, setSelectedOrt] = useState<Location>()
|
|
|
|
const [error, setError] = useState(false)
|
|
|
|
useEffect(() => {
|
|
|
|
if (initData && initData.id) {
|
|
|
|
reset(initData);
|
|
}
|
|
}, [initData])
|
|
|
|
|
|
|
|
|
|
const onSubmit = async (data: StockCreate) => {
|
|
if (initData?.id) {
|
|
|
|
const result = await stockUpdate(initData.id, data)
|
|
if (!result.success) {
|
|
setError(true)
|
|
setErrorMsg(() => result.errors)
|
|
console.log(result)
|
|
} else {
|
|
setError(false)
|
|
|
|
}
|
|
} else {
|
|
console.log('create')
|
|
|
|
const { articleVariantId, locationId, quantity, expiryDate, batchNumber } = data;
|
|
if (true) {
|
|
const create = {
|
|
articleVariantId,
|
|
locationId,
|
|
quantity,
|
|
expiryDate,
|
|
batchNumber
|
|
}
|
|
stockCreate(create);
|
|
}
|
|
}
|
|
|
|
};
|
|
|
|
const handleLocationSelect = (ort: LocationTreeNode) => {
|
|
setSelectedOrt(ort);
|
|
}
|
|
|
|
|
|
return (
|
|
<div className="me-30 flex flex-col p-4 pt-10 ps-10" >
|
|
|
|
{initData?.id && <div>{initData.id}</div>}
|
|
<form onSubmit={handleSubmit(onSubmit)}>
|
|
|
|
<InputTextR
|
|
label="Menge"
|
|
{...register("quantity", { required: "Pflichtfeld" })}
|
|
error={errors.quantity?.message}
|
|
/>
|
|
|
|
<button type="submit">Absenden</button>
|
|
</form>
|
|
<LocationTreeView data={orte} onSelect={(item) => handleLocationSelect(item)}></LocationTreeView>
|
|
|
|
{error && <div className="border-1 border-red-400 bg-red-200">
|
|
Error
|
|
{errMsg.map((msg, id) => (<div key={id}>{msg}</div>))}
|
|
</div>}
|
|
{selectetOrt && <div>{selectetOrt.name}</div>}
|
|
|
|
</div>
|
|
|
|
|
|
)
|
|
} |