feat: request on api
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import { FormEvent, useState } from "react";
|
||||
import { siteConfig } from "../lib/server-config";
|
||||
|
||||
type FormStatus = {
|
||||
tone: "success" | "error";
|
||||
@@ -19,29 +20,27 @@ export default function MinecraftWhitelistForm() {
|
||||
|
||||
const formData = new FormData(form);
|
||||
const payload = {
|
||||
username: String(formData.get("username") ?? ""),
|
||||
reason: String(formData.get("reason") ?? ""),
|
||||
game_username: String(formData.get("username") ?? ""),
|
||||
message: String(formData.get("reason") ?? ""),
|
||||
server_slug: "minecraft",
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch("/api/minecraft/whitelist/request", {
|
||||
const response = await fetch(`${siteConfig.apiUrl}/api/access-requests`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
const result = (await response.json()) as {
|
||||
ok?: boolean;
|
||||
message?: string;
|
||||
};
|
||||
|
||||
if (!response.ok || !result.ok) {
|
||||
if (!response.ok) {
|
||||
const result = await response.json().catch(() => ({})) as { message?: string };
|
||||
throw new Error(result.message ?? "Demande refusée.");
|
||||
}
|
||||
|
||||
form.reset();
|
||||
setStatus({
|
||||
tone: "success",
|
||||
message: result.message ?? "Demande envoyée.",
|
||||
message: "Demande envoyée.",
|
||||
});
|
||||
} catch (error) {
|
||||
setStatus({
|
||||
@@ -78,8 +77,7 @@ export default function MinecraftWhitelistForm() {
|
||||
<textarea
|
||||
id="minecraft-reason"
|
||||
name="reason"
|
||||
minLength={10}
|
||||
maxLength={800}
|
||||
maxLength={255}
|
||||
rows={5}
|
||||
placeholder="Je voudrais rejoindre le serveur..."
|
||||
required
|
||||
|
||||
Reference in New Issue
Block a user