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,26 @@
|
||||
"use client";
|
||||
|
||||
import { ArticleCreate, ArticleUpsert, Category } from "../../types/catalog"
|
||||
import { useFormContext } from "react-hook-form"
|
||||
import { InputTextR } from "../ui/inputTextR";
|
||||
import ComboBoxR from "../ui/ComboBoxR";
|
||||
|
||||
|
||||
interface Params {
|
||||
categories: Category[]
|
||||
}
|
||||
|
||||
export const ArticleBaseForm = ({ categories }: Params) => {
|
||||
|
||||
const { register, control } = useFormContext<ArticleUpsert>();
|
||||
|
||||
const values = categories.map((i) => ({ id: i._id, value: i.name }))
|
||||
return (
|
||||
<>
|
||||
<InputTextR label="Artikename" {...register("name", { required: "Pflichtfeld" })} />
|
||||
<ComboBoxR name="categoryId" control={control} options={values} placeholder="wählen..." />
|
||||
</>
|
||||
|
||||
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
"use client";
|
||||
|
||||
import { useRouter } from 'next/navigation'
|
||||
import { Article } from "../../types/catalog"
|
||||
|
||||
import { articleDelete } from "../../actions/article.actions";
|
||||
import { sites } from "../../_config/sites";
|
||||
import Link from "next/link";
|
||||
import { Eye, OctagonX } from 'lucide-react';
|
||||
|
||||
|
||||
interface Params {
|
||||
articles: Article[]
|
||||
}
|
||||
|
||||
export const ArticleList = ({ articles }: Params) => {
|
||||
|
||||
|
||||
const router = useRouter()
|
||||
|
||||
const handlerOnDelete = (id: string) => {
|
||||
articleDelete(id)
|
||||
router.refresh();
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='p-4 '>
|
||||
{articles.map((a) => (
|
||||
<div key={a._id} className="flex gap-4 border-b-1 p-4 border-gray-200">
|
||||
<div className='flex gap-4 wa'>
|
||||
<div className='w-32'>
|
||||
{a.name}
|
||||
</div>
|
||||
<div className='w-8'>
|
||||
<Link href={`${sites.article.update.url}/${a._id}`}>
|
||||
<Eye className=''></Eye>
|
||||
</Link>
|
||||
|
||||
|
||||
</div>
|
||||
<div className='w-8 '>
|
||||
<button className='flex gap-2' onClick={() => handlerOnDelete(a._id)}>
|
||||
<OctagonX className='' enableBackground={'green'} color='red'></OctagonX>
|
||||
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>))}
|
||||
|
||||
</div>
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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>
|
||||
|
||||
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
"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>
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user