wg-dashboard/app/servers/[id]/edit/page.tsx
Matheus Sampaio Queiroga e6cab72032
Init pages and two routers
Signed-off-by: Matheus Sampaio Queiroga <srherobrine20@gmail.com>
2024-02-03 21:12:51 -03:00

42 lines
1.3 KiB
TypeScript

import { wgServer, Server } from "@db/server";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
function FormEdit(props: Server) {
"use client";
async function updateDb(form: FormData) {
"use server";
const wgInt = await wgServer.findOne({ where: { id: props.id } });
wgInt.name = String(form.get("name")||props.name);
wgInt.IPv4 = String(form.get("IPv4")||props.IPv4);
wgInt.IPv6 = String(form.get("IPv6")||props.IPv6);
await wgInt.validate();
await wgInt.save();
revalidatePath(`/servers/${props.id}/edit`)
}
return (<form action={updateDb}>
<div>
Interface name: <input type="text" name="name" defaultValue={props.name} />
</div>
<div>
IPv4: <input type="text" name="IPv4" defaultValue={props.IPv4} />
</div>
<div>
IPv6: <input type="text" name="IPv6" defaultValue={props.IPv6} />
</div>
<div style={{ display: "flex", justifyContent: "space-evenly" }}>
<input type="submit" value="Update" />
<a href="..">Back to home</a>
</div>
</form>);
}
export default async function EditServer({ params: { id } }) {
const info = await wgServer.findOne({ where: { id: parseInt(id) } });
if (!info) return redirect("/servers");
return (<div>
<h1>Editing {info.name}</h1>
<FormEdit {...(info.toJSON())} id={id} />
</div>);
}