Alter Maintenance-Screen hinzugefügt
This commit is contained in:
@@ -5,54 +5,39 @@ import { Notam } from "@repo/db";
|
||||
import { NotamOptionalDefaults, NotamOptionalDefaultsSchema } from "@repo/db/zod";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { addMessage, disableMessage } from "../action";
|
||||
import { useState } from "react";
|
||||
import { DateInput } from "_components/ui/DateInput";
|
||||
import "react-datepicker/dist/react-datepicker.css"; // <-- Add this line at the top if using react-datepicker
|
||||
import { Button } from "_components/ui/Button";
|
||||
import { PaginatedTableRef } from "_components/PaginatedTable";
|
||||
import { RefObject } from "react";
|
||||
|
||||
export const MessageForm = ({ message }: { message?: Notam }) => {
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
|
||||
const getDefaultShowUntilDate = () => {
|
||||
const date = new Date();
|
||||
return date;
|
||||
};
|
||||
|
||||
export const MessageForm = ({ tableRef }: { tableRef: RefObject<PaginatedTableRef | null> }) => {
|
||||
const disableMessageClient = async () => {
|
||||
await disableMessage();
|
||||
window.location.reload();
|
||||
tableRef?.current?.refresh();
|
||||
};
|
||||
|
||||
const form = useForm<NotamOptionalDefaults>({
|
||||
resolver: zodResolver(NotamOptionalDefaultsSchema),
|
||||
defaultValues: {
|
||||
message: message?.message,
|
||||
color: message?.color,
|
||||
active: true,
|
||||
wartungsmodus: message?.wartungsmodus,
|
||||
disableHPG: message?.disableHPG,
|
||||
showUntil: getDefaultShowUntilDate(),
|
||||
},
|
||||
});
|
||||
|
||||
return (
|
||||
<form
|
||||
onSubmit={form.handleSubmit(async (values) => {
|
||||
setIsSubmitting(true);
|
||||
try {
|
||||
await addMessage(values);
|
||||
window.location.reload();
|
||||
tableRef?.current?.refresh();
|
||||
form.reset();
|
||||
} catch (error) {
|
||||
setIsSubmitting(false);
|
||||
console.error("Failed to add message", error);
|
||||
}
|
||||
})}
|
||||
className="grid grid-cols-6 gap-3"
|
||||
>
|
||||
<label className="floating-label col-span-6">
|
||||
<span>Globale Service Nachricht</span>
|
||||
<span>Notam</span>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Globale Service Nachricht"
|
||||
placeholder="Globales Notam"
|
||||
className="input input-md w-full mb-2"
|
||||
{...form.register("message")}
|
||||
/>
|
||||
@@ -94,7 +79,7 @@ export const MessageForm = ({ message }: { message?: Notam }) => {
|
||||
<input
|
||||
type="checkbox"
|
||||
className="checkbox checkbox-primary"
|
||||
{...form.register("wartungsmodus")}
|
||||
{...form.register("maintenanceEnabled")}
|
||||
/>
|
||||
Wartungsmodus einschalten
|
||||
</label>
|
||||
@@ -107,26 +92,24 @@ export const MessageForm = ({ message }: { message?: Notam }) => {
|
||||
HPG Alarmierung deaktivieren
|
||||
</label>
|
||||
</div>
|
||||
<div className="flex flex-col gap-2 ml-2">
|
||||
<label className="label">Nachricht & Effekte bis (optional)</label>
|
||||
<DateInput
|
||||
control={form.control}
|
||||
name="showUntil"
|
||||
showTimeInput
|
||||
timeCaption="Uhrzeit"
|
||||
showTimeCaption
|
||||
className="input input-md"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-col justify-end">
|
||||
<div className="flex justify-center gap-2">
|
||||
<button type="submit" className="btn btn-soft" onClick={disableMessageClient}>
|
||||
Aktuelle Nachricht deaktivieren
|
||||
</button>
|
||||
<button type="submit" className="btn btn-primary" disabled={isSubmitting}>
|
||||
<Button
|
||||
type="button"
|
||||
onSubmit={() => false}
|
||||
className="btn btn-soft"
|
||||
onClick={disableMessageClient}
|
||||
>
|
||||
Config zurücksetzen
|
||||
</Button>
|
||||
<Button
|
||||
type="submit"
|
||||
className="btn btn-primary"
|
||||
isLoading={form.formState.isSubmitting}
|
||||
>
|
||||
Speichern
|
||||
</button>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
22
apps/hub/app/(app)/admin/config/action.tsx
Normal file
22
apps/hub/app/(app)/admin/config/action.tsx
Normal file
@@ -0,0 +1,22 @@
|
||||
"use server";
|
||||
import { prisma, Prisma } from "@repo/db";
|
||||
|
||||
export const addMessage = async (notam: Prisma.NotamCreateInput) => {
|
||||
try {
|
||||
await prisma.notam.create({
|
||||
data: notam,
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error("Failed to add message");
|
||||
}
|
||||
};
|
||||
|
||||
export const disableMessage = async () => {
|
||||
try {
|
||||
await prisma.notam.create({
|
||||
data: {},
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error("Failed to disable message");
|
||||
}
|
||||
};
|
||||
@@ -1,27 +1,30 @@
|
||||
"use client";
|
||||
|
||||
import { Check, MessageSquareWarning } from "lucide-react";
|
||||
import { MessageForm } from "./_components/messageForm";
|
||||
import { PaginatedTable } from "_components/PaginatedTable";
|
||||
import { Check, MessageSquareWarning, Settings } from "lucide-react";
|
||||
import { MessageForm } from "./_components/MessageForm";
|
||||
import { PaginatedTable, PaginatedTableRef } from "_components/PaginatedTable";
|
||||
import { ColumnDef } from "@tanstack/react-table";
|
||||
import { Notam } from "@repo/db";
|
||||
import { useRef } from "react";
|
||||
|
||||
export default function MessagePage() {
|
||||
const tableRef = useRef<PaginatedTableRef | null>(null);
|
||||
return (
|
||||
<>
|
||||
<div className="grid grid-cols-6 gap-4">
|
||||
<div className="col-span-full">
|
||||
<p className="text-2xl font-semibold text-left flex items-center gap-2">
|
||||
<MessageSquareWarning className="w-5 h-5" /> Service Nachrichten
|
||||
<Settings className="w-5 h-5" /> Config
|
||||
</p>
|
||||
</div>
|
||||
<div className="card bg-base-200 shadow-xl mb-4 col-span-6">
|
||||
<div className="card-body">
|
||||
<MessageForm />
|
||||
<MessageForm tableRef={tableRef} />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<PaginatedTable
|
||||
ref={tableRef}
|
||||
prismaModel="notam"
|
||||
initialOrderBy={[{ id: "createdAt", desc: true }]}
|
||||
columns={
|
||||
@@ -42,7 +45,7 @@ export default function MessagePage() {
|
||||
accessorKey: "wartungsmodus",
|
||||
header: "Wartungsmodus",
|
||||
cell: ({ row }) => {
|
||||
const wartungsmodus = row.getValue("wartungsmodus");
|
||||
const wartungsmodus = row.original.maintenanceEnabled;
|
||||
return wartungsmodus ? <Check /> : "";
|
||||
},
|
||||
},
|
||||
@@ -50,28 +53,11 @@ export default function MessagePage() {
|
||||
accessorKey: "disableHPG",
|
||||
header: "HPG deaktiviert",
|
||||
cell: ({ row }) => {
|
||||
const disableHPG = row.getValue("disableHPG");
|
||||
const disableHPG = row.original.disableHPG;
|
||||
return disableHPG ? <Check /> : "";
|
||||
},
|
||||
},
|
||||
{
|
||||
accessorKey: "showUntil",
|
||||
header: "Zeitlimit",
|
||||
cell: ({ row, cell }) => {
|
||||
const showUntil = new Date(cell.getValue() as string);
|
||||
const createdAt = new Date(row.getValue("createdAt") as string);
|
||||
if (showUntil > createdAt) {
|
||||
return showUntil.toLocaleDateString("de-DE", {
|
||||
year: "numeric",
|
||||
month: "2-digit",
|
||||
day: "2-digit",
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
});
|
||||
}
|
||||
return "";
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
accessorKey: "createdAt",
|
||||
header: "Erstellt am",
|
||||
@@ -1,39 +0,0 @@
|
||||
"use server";
|
||||
import { prisma, Prisma } from "@repo/db";
|
||||
|
||||
export const addMessage = async (message: Prisma.NotamCreateInput) => {
|
||||
try {
|
||||
await prisma.notam.updateMany({
|
||||
where: { active: true },
|
||||
data: { active: false },
|
||||
});
|
||||
|
||||
const showUntil = new Date(message.showUntil);
|
||||
const showUntilActive = showUntil > new Date();
|
||||
|
||||
await prisma.notam.create({
|
||||
data: {
|
||||
message: message.message,
|
||||
color: message.color,
|
||||
active: true,
|
||||
wartungsmodus: message.wartungsmodus,
|
||||
disableHPG: message.disableHPG,
|
||||
showUntilActive,
|
||||
showUntil,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error("Failed to add message");
|
||||
}
|
||||
};
|
||||
|
||||
export const disableMessage = async () => {
|
||||
try {
|
||||
await prisma.notam.updateMany({
|
||||
where: { active: true },
|
||||
data: { active: false },
|
||||
});
|
||||
} catch (error) {
|
||||
throw new Error("Failed to disable message");
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user