This commit is contained in:
2025-11-08 13:42:43 +01:00
commit 7567d3eb05
125 changed files with 16866 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
"use server";
const apiUrl = process.env.NEXT_SERVER_API_URL;
export async function getTableData() {
try {
const res = await fetch(
`${apiUrl}/vorlageFlaechendruck/getAllFlaechendruckVorlagen`
);
if (!res.ok) {
console.error("Response status:", res.status);
}
const data: {
id: number;
product_type: string;
height: number;
width: number;
printer: string;
coordinates: {
x: number;
y: number;
rotation: number;
}[];
}[] = await res.json();
return data;
} catch (error) {
console.error("Error fetching vorlagen:", error);
throw new Error("An error occurred while fetching vorlagen.");
}
}
export async function deleteVorlageFlaechendruck(id: number) {
const res = await fetch(`${apiUrl}/vorlageFlaechendruck/delete/${id}`, {
method: "DELETE",
});
if (!res.ok) {
throw new Error("Failed to delete item");
}
return;
}

View File

@@ -0,0 +1,83 @@
"use server";
const apiUrl = process.env.NEXT_SERVER_API_URL;
export async function createVorlage(formData: FormData) {
const printer = formData.get("printer") as string;
const product_type = formData.get("product_type") as string;
const height = Number(formData.get("height"));
const width = Number(formData.get("width"));
const coordinatesJson = formData.get("coordinates") as string;
const coordinates = JSON.parse(coordinatesJson);
const payload = {
printer,
product_type,
height,
width,
coordinates,
};
try {
const response = await fetch(
`${apiUrl}/vorlageFlaechendruck/createWithCoordinates`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
cache: "no-store",
}
);
if (!response.ok) {
console.error("Response status:", response.status);
throw new Error("Failed to submit form");
}
} catch (error) {
console.error("Error submitting form:", error);
throw new Error("An error occurred while submitting the form.");
}
}
export async function alterVorlage(formData: FormData) {
const id = Number(formData.get("id"));
const printer = formData.get("printer") as string;
const product_type = formData.get("product_type") as string;
const height = Number(formData.get("height"));
const width = Number(formData.get("width"));
const coordinatesJson = formData.get("coordinates") as string;
const coordinates = JSON.parse(coordinatesJson);
const payload = {
id,
printer,
product_type,
height,
width,
coordinates,
};
try {
const response = await fetch(
`${apiUrl}/vorlageFlaechendruck/alterVorlage`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(payload),
cache: "no-store",
}
);
if (!response.ok) {
console.error("Response status:", response.status);
throw new Error("Failed to submit form");
}
} catch (error) {
console.error("Error submitting form:", error);
throw new Error("An error occurred while submitting the form.");
}
}

View File

@@ -0,0 +1,276 @@
"use Client";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { z } from "zod"
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { Separator } from "@/components/ui/separator";
import { Plus } from "lucide-react";
import { useFieldArray } from "react-hook-form";
import { createVorlage } from "./action";
import { useState } from "react";
export default function AddDialog({ onNewEntry }: { onNewEntry: () => void }) {
const [open, setOpen] = useState(false)
const formSchema = z.object({
Drucker: z.string().min(1, {
message: "Druckername muss mindestens 2 Zeichen lang sein.",
}),
Artikeltyp: z.string().min(1, {
message: "Artikeltyp muss mindestens 2 Zeichen lang sein.",
}),
Höhe: z.preprocess(
(value) => (typeof value === "string" ? parseFloat(value) : value),
z.number({
invalid_type_error: "Höhe muss eine gültige Zahl sein. Bspw. 1.5 .",
}).min(1, {
message: "Höhe muss mindestens 1 sein.",
})
),
Breite: z.preprocess(
(value) => (typeof value === "string" ? parseFloat(value) : value),
z.number({
invalid_type_error: "Breite muss eine gültige Zahl sein. Bspw. 1.5 .",
}).min(1, {
message: "Breite muss mindestens 1 sein.",
})
),
Koordinaten: z.array(
z.object({
x: z.preprocess(
(value) => (typeof value === "string" ? parseFloat(value) : value),
z.number().min(0, {
message: "X-Koordinate muss mindestens 0 sein.",
})
),
y: z.preprocess(
(value) => (typeof value === "string" ? parseFloat(value) : value),
z.number().min(0, {
message: "Y-Koordinate muss mindestens 0 sein.",
})
),
rotation: z.preprocess(
(value) => (typeof value === "string" ? parseFloat(value) : value),
z.number().min(0, {
message: "Rotation muss mindestens 0 sein.",
})
),
})
).min(1, {
message: "Es muss mindestens eine Koordinate angegeben werden.",
}),
});
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
Drucker: "",
Artikeltyp: "",
Höhe: 0,
Breite: 0,
Koordinaten: [{ x: 0, y: 0, rotation: 0 }],
},
});
const { fields, append, remove } = useFieldArray({
control: form.control,
name: "Koordinaten",
});
async function onSubmit(values: z.infer<typeof formSchema>) {
const formData = new FormData();
formData.append("printer", values.Drucker);
formData.append("product_type", values.Artikeltyp);
formData.append("height", values.Höhe.toString());
formData.append("width", values.Breite.toString());
formData.append("coordinates", JSON.stringify(values.Koordinaten));
try {
await createVorlage(formData);
setOpen(false);
form.reset();
onNewEntry();
}
catch (error) {
console.error("Error submitting form:", error);
throw new Error("An error occurred while submitting the form.");
}
}
return (
<div>
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button variant="outline" className="mt-2 mb-2 border-green-700 text-green-700">
Vorlage hinzufügen
</Button>
</DialogTrigger>
<DialogContent className="max-w-screen-lg max-h-screen overflow-y-auto">
<DialogHeader>
<DialogTitle className="mb-8">Vorlage hinzufügen</DialogTitle>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
<FormField
control={form.control}
name="Drucker"
render={({ field }) => (
<FormItem>
<FormLabel>Drucker Name</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<DialogDescription>
Der Name des Druckers, auf dem die Vorlage gedruckt wird.
</DialogDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="Artikeltyp"
render={({ field }) => (
<FormItem>
<FormLabel>Artikel Typ</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<DialogDescription>
Der Name des Artikels, der auf der Vorlage gedruckt wird.
</DialogDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="Höhe"
render={({ field }) => (
<FormItem>
<FormLabel>Höhe</FormLabel>
<FormControl>
<Input type="number" step="any" {...field} />
</FormControl>
<FormDescription>
Die Höhe der Vorlage in Millimeter.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="Breite"
render={({ field }) => (
<FormItem>
<FormLabel>Breite</FormLabel>
<FormControl>
<Input type="number" step="any" {...field} />
</FormControl>
<FormDescription>
Die Breite der Vorlage in Millimeter.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<div className="max-h-64 overflow-y-auto">
<FormLabel className="mb-4">Koordinaten:</FormLabel>
<Separator />
{fields.map((field, index) => (
<div key={field.id} className="space-y-4">
<FormField
control={form.control}
name={`Koordinaten.${index}.x`}
render={({ field }) => (
<FormItem className="mt-4">
<FormLabel>X-Koordinate</FormLabel>
<FormControl>
<Input type="number" step="any" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name={`Koordinaten.${index}.y`}
render={({ field }) => (
<FormItem>
<FormLabel>Y-Koordinate</FormLabel>
<FormControl>
<Input type="number" step="any" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name={`Koordinaten.${index}.rotation`}
render={({ field }) => (
<FormItem>
<FormLabel>Rotation</FormLabel>
<FormControl>
<Input type="number" step="any" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button
type="button"
variant="destructive"
onClick={() => remove(index)}
>
Entfernen
</Button>
<Separator className="mt-4" />
</div>
))}
<Button
type="button"
variant="outline"
onClick={() => append({ x: 0, y: 0, rotation: 0 })}
className="mt-4"
>
<Plus />
</Button>
</div>
<Button type="submit">
Hinzufügen
</Button>
</form>
</Form>
</DialogHeader>
</DialogContent>
</Dialog>
</div>
);
}

View File

@@ -0,0 +1,294 @@
"use Client";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog";
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { z } from "zod"
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { Separator } from "@/components/ui/separator";
import { Plus } from "lucide-react";
import { useFieldArray } from "react-hook-form";
import { alterVorlage } from "./action";
import { useState } from "react";
type TableData = {
id: number;
product_type: string;
height: number;
width: number;
printer: string;
coordinates: { x: number; y: number; rotation: number }[];
};
type AlterDialogProps = {
onNewEntry: () => void;
data: TableData;
};
export default function AlterDialog({ onNewEntry, data }: AlterDialogProps) {
const [open, setOpen] = useState(false)
const formSchema = z.object({
Drucker: z.string().min(1, {
message: "Druckername muss mindestens 2 Zeichen lang sein.",
}),
Artikeltyp: z.string().min(1, {
message: "Artikeltyp muss mindestens 2 Zeichen lang sein.",
}),
Höhe: z.preprocess(
(value) => (typeof value === "string" ? parseFloat(value) : value),
z.number({
invalid_type_error: "Höhe muss eine gültige Zahl sein. Bspw. 1.5 .",
}).min(1, {
message: "Höhe muss mindestens 1 sein.",
})
),
Breite: z.preprocess(
(value) => (typeof value === "string" ? parseFloat(value) : value),
z.number({
invalid_type_error: "Breite muss eine gültige Zahl sein. Bspw. 1.5 .",
}).min(1, {
message: "Breite muss mindestens 1 sein.",
})
),
Koordinaten: z.array(
z.object({
x: z.preprocess(
(value) => (typeof value === "string" ? parseFloat(value) : value),
z.number().min(0, {
message: "X-Koordinate muss mindestens 0 sein.",
})
),
y: z.preprocess(
(value) => (typeof value === "string" ? parseFloat(value) : value),
z.number().min(0, {
message: "Y-Koordinate muss mindestens 0 sein.",
})
),
rotation: z.preprocess(
(value) => (typeof value === "string" ? parseFloat(value) : value),
z.number().min(0, {
message: "Rotation muss mindestens 0 sein.",
})
),
})
).min(1, {
message: "Es muss mindestens eine Koordinate angegeben werden.",
}),
});
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
Drucker: data.printer,
Artikeltyp: data.product_type,
Höhe: data.height,
Breite: data.width,
Koordinaten: data.coordinates.map((coord) => ({
x: coord.x,
y: coord.y,
rotation: coord.rotation,
})),
},
});
const { fields, append, remove } = useFieldArray({
control: form.control,
name: "Koordinaten",
});
async function onSubmit(values: z.infer<typeof formSchema>) {
const formData = new FormData();
formData.append("id", data.id.toString());
formData.append("printer", values.Drucker);
formData.append("product_type", values.Artikeltyp);
formData.append("height", values.Höhe.toString());
formData.append("width", values.Breite.toString());
formData.append("coordinates", JSON.stringify(values.Koordinaten));
try {
await alterVorlage(formData);
setOpen(false);
form.reset();
onNewEntry();
}
catch (error) {
console.error("Error submitting form:", error);
throw new Error("An error occurred while submitting the form.");
}
}
return (
<div>
<Dialog open={open} onOpenChange={setOpen}>
<DialogTrigger asChild>
<Button variant="ghost" className="font-normal pl-2">
Vorlage bearbeiten
</Button>
</DialogTrigger>
<DialogContent className="max-w-screen-lg max-h-screen overflow-y-auto">
<DialogHeader>
<DialogTitle className="mb-8">Vorlage bearbeiten</DialogTitle>
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
<FormField
control={form.control}
name="Drucker"
render={({ field }) => (
<FormItem>
<FormLabel>Drucker Name</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<DialogDescription>
Der Name des Druckers, auf dem die Vorlage gedruckt wird.
</DialogDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="Artikeltyp"
render={({ field }) => (
<FormItem>
<FormLabel>Artikel Typ</FormLabel>
<FormControl>
<Input {...field} />
</FormControl>
<DialogDescription>
Der Name des Artikels, der auf der Vorlage gedruckt wird.
</DialogDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="Höhe"
render={({ field }) => (
<FormItem>
<FormLabel>Höhe</FormLabel>
<FormControl>
<Input type="number" step="any" {...field} />
</FormControl>
<FormDescription>
Die Höhe der Vorlage in Millimeter.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name="Breite"
render={({ field }) => (
<FormItem>
<FormLabel>Breite</FormLabel>
<FormControl>
<Input type="number" step="any" {...field} />
</FormControl>
<FormDescription>
Die Breite der Vorlage in Millimeter.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>
<div className="max-h-64 overflow-y-auto">
<FormLabel className="mb-4">Koordinaten:</FormLabel>
<Separator />
{fields.map((field, index) => (
<div key={field.id} className="space-y-4">
<FormField
control={form.control}
name={`Koordinaten.${index}.x`}
render={({ field }) => (
<FormItem className="mt-4">
<FormLabel>X-Koordinate</FormLabel>
<FormControl>
<Input type="number" step="any" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name={`Koordinaten.${index}.y`}
render={({ field }) => (
<FormItem>
<FormLabel>Y-Koordinate</FormLabel>
<FormControl>
<Input type="number" step="any" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<FormField
control={form.control}
name={`Koordinaten.${index}.rotation`}
render={({ field }) => (
<FormItem>
<FormLabel>Rotation</FormLabel>
<FormControl>
<Input type="number" step="any" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button
type="button"
variant="destructive"
onClick={() => remove(index)}
>
Entfernen
</Button>
<Separator className="mt-4" />
</div>
))}
<Button
type="button"
variant="outline"
onClick={() => append({ x: 0, y: 0, rotation: 0 })}
className="mt-4"
>
<Plus />
</Button>
</div>
<Button type="submit">
Änderungen speichern
</Button>
</form>
</Form>
</DialogHeader>
</DialogContent>
</Dialog>
</div>
);
}

View File

@@ -0,0 +1,199 @@
"use client";
import { useEffect, useState } from "react";
import { Ellipsis } from "lucide-react"
import { getTableData, deleteVorlageFlaechendruck } from "./action";
import {
Table,
TableBody,
TableCaption,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table"
import { Button } from "@/components/ui/button"
import {
Dialog,
DialogContent,
DialogDescription,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "@/components/ui/dialog"
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
AlertDialogTrigger,
} from "@/components/ui/alert-dialog"
import AddDialog from "./components/addDialog";
import AlterDialog from "./components/alterDialog";
type TableData = {
id: number;
product_type: string;
height: number;
width: number;
printer: string;
coordinates: { x: number; y: number; rotation: number }[];
};
export default function Page() {
const [data, setData] = useState<TableData[]>([]);
useEffect(() => {
async function fetchData() {
try {
const result = await getTableData();
setData(result);
} catch (error) {
console.error("Fehler beim Laden der Daten:", error);
}
}
fetchData();
}, []);
const handleNewEntry = async () => {
try {
const result = await getTableData(); // Daten erneut abrufen
setData(result); // Tabelle aktualisieren
} catch (error) {
console.error("Fehler beim Aktualisieren der Daten:", error);
}
};
const deleteEntry = async (id: number) => {
try {
await deleteVorlageFlaechendruck(id);
await handleNewEntry();
}
catch (error) {
console.error("Fehler beim Löschen der Daten:", error);
}
};
return (
<div className="min-h-screen bg-gray-50 p-6">
<div className="max-w-5xl mx-auto">
{/* Table Section */}
<Table className="bg-white shadow-md border border-gray-300">
<TableCaption>Vorlagen Flächendruck</TableCaption>
<TableHeader>
<TableRow>
<TableHead className="w-[100px]">Drucker</TableHead>
<TableHead>Artikel Typ</TableHead>
<TableHead>Höhe</TableHead>
<TableHead>Breite</TableHead>
<TableHead>Koordinaten</TableHead>
<TableHead className="text-right">
<AddDialog onNewEntry={handleNewEntry} />
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{data.map((item) => (
<TableRow key={item.id}>
<TableCell>{item.printer}</TableCell>
<TableCell>{item.product_type}</TableCell>
<TableCell>{item.height}</TableCell>
<TableCell >{item.width}</TableCell>
<TableCell>
<Dialog>
<DialogTrigger asChild>
<Button variant="outline">Anzeigen</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Koordinaten</DialogTitle>
<DialogDescription>
Hier sind die Koordinaten für die ausgewählte Vorlage. X und Y Werte in Millimetern.
</DialogDescription>
</DialogHeader>
<Table>
<TableCaption>Koordinaten Tabelle</TableCaption>
<TableHeader>
<TableRow>
<TableHead>X</TableHead>
<TableHead>Y</TableHead>
<TableHead className="text-right">Rotation</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{item.coordinates.map((coordi) => (
<TableRow key={crypto.randomUUID()}>
<TableCell>{coordi.x}</TableCell>
<TableCell>{coordi.y}</TableCell>
<TableCell className="text-right">{coordi.rotation}°</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</DialogContent>
</Dialog>
</TableCell>
<TableCell className="text-right">
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="ghost" size="icon">
<Ellipsis />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end">
<AlterDialog onNewEntry={handleNewEntry} data={item} />
<DropdownMenuSeparator />
<DropdownMenuItem variant="destructive">
<AlertDialog>
<AlertDialogTrigger
onClick={(event) => {
event.stopPropagation(); // Verhindert das Schließen des Dropdown-Menüs
}}
>
Eintrag Löschen
</AlertDialogTrigger>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Sind Sie sich sicher?</AlertDialogTitle>
<AlertDialogDescription>
Diese Aktion ist permanent und kann nicht rückgängig gemacht werden.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Abbrechen</AlertDialogCancel>
<AlertDialogAction
onClick={() => {
deleteEntry(item.id); // Eintrag löschen
}}
>
Löschen
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
</div>
);
}