Initial commit: NestJS NextJS pnpm monorepo
This commit is contained in:
commit
610dcf202b
175 files changed
+25172
No files matched your search
@@ -0,0 +1,106 @@
|
||||
"use client";
|
||||
|
||||
import { emptyArticleDto, Article, ArticleCreate, Category, Manufacturer, ArticleUpsert } from "../../types/catalog"
|
||||
import { FormProvider, useForm } from "react-hook-form"
|
||||
import { ArticleBaseForm } from "./articleBase.form";
|
||||
import { ArticleVariantForm } from "./articleVariants.form";
|
||||
import { articleCreate, articleUpdate } from "../../actions/article.actions";
|
||||
import { useEffect, useState } from "react";
|
||||
import Modal from "../ui/modal";
|
||||
import { ManufactuererForm } from "../manufacturer/manufacturer.form";
|
||||
|
||||
|
||||
interface Params {
|
||||
categories: Category[]
|
||||
manufacturer: Manufacturer[]
|
||||
initData?: ArticleUpsert
|
||||
debug?: boolean
|
||||
}
|
||||
|
||||
|
||||
export const ArticleMainForm = ({ categories, manufacturer, initData, debug }: Params) => {
|
||||
|
||||
const methods = useForm<ArticleUpsert>({ defaultValues: emptyArticleDto });
|
||||
const [errMsg, setErrorMsg] = useState<string[]>([])
|
||||
const [error, setError] = useState(false)
|
||||
const [creatManufacturer, setCreatManufacturer] = useState(-1);
|
||||
const [hersteller,setHersteller] = useState<Manufacturer[]>([])
|
||||
|
||||
useEffect(() => {
|
||||
setHersteller(manufacturer);
|
||||
if (initData && initData._id) {
|
||||
|
||||
methods.reset(initData);
|
||||
}
|
||||
}, [initData])
|
||||
|
||||
const handlerCreatManufacturer = (data: Manufacturer) => {
|
||||
console.log('Create')
|
||||
setHersteller((cur)=>([...cur,data]));
|
||||
|
||||
|
||||
methods.setValue(
|
||||
`variants.${creatManufacturer}.manufacturerId`,
|
||||
data._id,
|
||||
{ shouldDirty: true, shouldValidate: true }
|
||||
);
|
||||
setCreatManufacturer(-1)
|
||||
}
|
||||
|
||||
const onSubmit = async (data: ArticleUpsert) => {
|
||||
if (initData?._id) {
|
||||
|
||||
const result = await articleUpdate(initData._id, data)
|
||||
if (!result.success) {
|
||||
setError(true)
|
||||
setErrorMsg(() => result.errors)
|
||||
console.log(result)
|
||||
} else {
|
||||
setError(false)
|
||||
|
||||
}
|
||||
} else {
|
||||
console.log('create')
|
||||
|
||||
const { name, categoryId, variants, attributes } = data;
|
||||
if (categoryId) {
|
||||
const create = {
|
||||
name,
|
||||
categoryId,
|
||||
variants,
|
||||
attributes
|
||||
}
|
||||
articleCreate(create);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="me-30 flex flex-col p-4 pt-10 ps-10" >
|
||||
<FormProvider {...methods}>
|
||||
{initData?._id && <div>{initData._id}</div>}
|
||||
<form onSubmit={methods.handleSubmit(onSubmit)}>
|
||||
<ArticleBaseForm categories={categories}></ArticleBaseForm>
|
||||
<ArticleVariantForm manufacturer={hersteller} onOpenManufacturer={(index) => setCreatManufacturer(index)}></ArticleVariantForm>
|
||||
|
||||
<div>{methods.watch("name")}</div>
|
||||
<pre>{JSON.stringify(methods.watch(), null, 2)}</pre>
|
||||
<button type="submit">Absenden</button>
|
||||
</form>
|
||||
|
||||
</FormProvider>
|
||||
<Modal open={creatManufacturer >= 0} onClose={() => setCreatManufacturer(-1)}>
|
||||
<ManufactuererForm onChangeData={(d) => handlerCreatManufacturer(d)}></ManufactuererForm>
|
||||
<button onClick={() => setCreatManufacturer(-1)}>Abbruch</button>
|
||||
</Modal>
|
||||
{error && <div className="border-1 border-red-400 bg-red-200">
|
||||
Error
|
||||
{errMsg.map((msg, id) => (<div key={id}>{msg}</div>))}
|
||||
</div>}
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user