mirror of
https://github.com/trafficlunar/tomodachi-share.git
synced 2026-05-13 13:17:45 +00:00
feat: queue toggle in admin control center
This commit is contained in:
parent
12c0205bf5
commit
bf8e8a094d
6 changed files with 55 additions and 36 deletions
|
|
@ -1,11 +1,10 @@
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { auth } from "@/lib/auth";
|
import { auth } from "@/lib/auth";
|
||||||
|
import { settings } from "@/lib/settings";
|
||||||
let canSubmit = true;
|
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
return NextResponse.json({ success: true, value: canSubmit });
|
return NextResponse.json({ success: true, value: settings.canSubmit });
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function PATCH(request: NextRequest) {
|
export async function PATCH(request: NextRequest) {
|
||||||
|
|
@ -15,9 +14,9 @@ export async function PATCH(request: NextRequest) {
|
||||||
if (Number(session.user?.id) !== Number(process.env.NEXT_PUBLIC_ADMIN_USER_ID)) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
if (Number(session.user?.id) !== Number(process.env.NEXT_PUBLIC_ADMIN_USER_ID)) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||||
|
|
||||||
const body = await request.json();
|
const body = await request.json();
|
||||||
const validatedCanSubmit = z.boolean().safeParse(body);
|
const validated = z.boolean().safeParse(body);
|
||||||
if (!validatedCanSubmit.success) return NextResponse.json({ error: "Failed to validate body" }, { status: 400 });
|
if (!validated.success) return NextResponse.json({ error: "Failed to validate body" }, { status: 400 });
|
||||||
|
|
||||||
canSubmit = validatedCanSubmit.data;
|
settings.canSubmit = validated.data;
|
||||||
return NextResponse.json({ success: true });
|
return NextResponse.json({ success: true });
|
||||||
}
|
}
|
||||||
|
|
|
||||||
22
src/app/api/admin/queue/route.ts
Normal file
22
src/app/api/admin/queue/route.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { auth } from "@/lib/auth";
|
||||||
|
import { settings } from "@/lib/settings";
|
||||||
|
|
||||||
|
export async function GET() {
|
||||||
|
return NextResponse.json({ success: true, value: settings.queueEnabled });
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function PATCH(request: NextRequest) {
|
||||||
|
const session = await auth();
|
||||||
|
if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
||||||
|
|
||||||
|
if (Number(session.user?.id) !== Number(process.env.NEXT_PUBLIC_ADMIN_USER_ID)) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
|
||||||
|
|
||||||
|
const body = await request.json();
|
||||||
|
const validated = z.boolean().safeParse(body);
|
||||||
|
if (!validated.success) return NextResponse.json({ error: "Failed to validate body" }, { status: 400 });
|
||||||
|
|
||||||
|
settings.queueEnabled = validated.data;
|
||||||
|
return NextResponse.json({ success: true });
|
||||||
|
}
|
||||||
|
|
@ -21,6 +21,7 @@ import { ThreeDsTomodachiLifeMii } from "@/lib/three-ds-tomodachi-life-mii";
|
||||||
|
|
||||||
import { SwitchMiiInstructions } from "@/types";
|
import { SwitchMiiInstructions } from "@/types";
|
||||||
import { minifyInstructions } from "@/lib/switch";
|
import { minifyInstructions } from "@/lib/switch";
|
||||||
|
import { settings } from "@/lib/settings";
|
||||||
|
|
||||||
const uploadsDirectory = path.join(process.cwd(), "uploads", "mii");
|
const uploadsDirectory = path.join(process.cwd(), "uploads", "mii");
|
||||||
|
|
||||||
|
|
@ -74,10 +75,7 @@ export async function POST(request: NextRequest) {
|
||||||
const rateLimit = new RateLimit(request, 3);
|
const rateLimit = new RateLimit(request, 3);
|
||||||
const check = await rateLimit.handle();
|
const check = await rateLimit.handle();
|
||||||
if (check) return check;
|
if (check) return check;
|
||||||
|
if (!settings.canSubmit) return rateLimit.sendResponse({ error: "Submissions are temporarily disabled" }, 503);
|
||||||
const response = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/admin/can-submit`);
|
|
||||||
const { value } = await response.json();
|
|
||||||
if (!value) return rateLimit.sendResponse({ error: "Submissions are temporarily disabled" }, 503);
|
|
||||||
|
|
||||||
// Parse tags and QR code as JSON
|
// Parse tags and QR code as JSON
|
||||||
const formData = await request.formData();
|
const formData = await request.formData();
|
||||||
|
|
@ -199,8 +197,8 @@ export async function POST(request: NextRequest) {
|
||||||
name,
|
name,
|
||||||
tags,
|
tags,
|
||||||
description,
|
description,
|
||||||
gender: gender ?? "MALE",
|
gender: gender ?? "MALE",
|
||||||
in_queue: true,
|
in_queue: settings.queueEnabled,
|
||||||
|
|
||||||
// Automatically detect certain information if on 3DS
|
// Automatically detect certain information if on 3DS
|
||||||
...(platform === "THREE_DS"
|
...(platform === "THREE_DS"
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import { auth } from "@/lib/auth";
|
||||||
import { prisma } from "@/lib/prisma";
|
import { prisma } from "@/lib/prisma";
|
||||||
|
|
||||||
import SubmitForm from "@/components/submit-form";
|
import SubmitForm from "@/components/submit-form";
|
||||||
|
import { settings } from "@/lib/settings";
|
||||||
|
|
||||||
export const metadata: Metadata = {
|
export const metadata: Metadata = {
|
||||||
title: "Submit a Mii - TomodachiShare",
|
title: "Submit a Mii - TomodachiShare",
|
||||||
|
|
@ -30,16 +31,8 @@ export default async function SubmitPage() {
|
||||||
});
|
});
|
||||||
if (activePunishment) redirect("/off-the-island");
|
if (activePunishment) redirect("/off-the-island");
|
||||||
|
|
||||||
// Check if submissions are disabled
|
if (!settings.canSubmit)
|
||||||
let value: boolean | null = true;
|
return (
|
||||||
try {
|
|
||||||
const response = await fetch(`${process.env.NEXT_PUBLIC_BASE_URL}/api/admin/can-submit`);
|
|
||||||
value = await response.json();
|
|
||||||
} catch (error) {
|
|
||||||
return <p>An error occurred!</p>;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!value) return (
|
|
||||||
<div className="grow flex items-center justify-center">
|
<div className="grow flex items-center justify-center">
|
||||||
<div className="bg-amber-50 border-2 border-amber-500 rounded-2xl shadow-lg p-8 max-w-xs w-full text-center flex flex-col">
|
<div className="bg-amber-50 border-2 border-amber-500 rounded-2xl shadow-lg p-8 max-w-xs w-full text-center flex flex-col">
|
||||||
<h2 className="text-5xl font-black">Sorry</h2>
|
<h2 className="text-5xl font-black">Sorry</h2>
|
||||||
|
|
@ -50,7 +43,7 @@ export default async function SubmitPage() {
|
||||||
</Link>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|
||||||
return <SubmitForm />;
|
return <SubmitForm />;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,25 +1,17 @@
|
||||||
"use client";
|
"use client";
|
||||||
|
|
||||||
import { useEffect, useState } from "react";
|
import { settings } from "@/lib/settings";
|
||||||
|
import { useState } from "react";
|
||||||
|
|
||||||
export default function ControlCenter() {
|
export default function ControlCenter() {
|
||||||
const [canSubmit, setCanSubmit] = useState(true);
|
const [canSubmit, setCanSubmit] = useState(settings.canSubmit);
|
||||||
|
const [isQueueEnabled, setIsQeueueEnabled] = useState(settings.queueEnabled);
|
||||||
|
|
||||||
const onClickSet = async () => {
|
const onClickSet = async () => {
|
||||||
await fetch("/api/admin/can-submit", { method: "PATCH", body: JSON.stringify(canSubmit) });
|
await fetch("/api/admin/can-submit", { method: "PATCH", body: JSON.stringify(canSubmit) });
|
||||||
|
await fetch("/api/admin/queue", { method: "PATCH", body: JSON.stringify(isQueueEnabled) });
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
const check = async () => {
|
|
||||||
const response = await fetch("/api/admin/can-submit");
|
|
||||||
const { value } = await response.json();
|
|
||||||
|
|
||||||
setCanSubmit(value);
|
|
||||||
};
|
|
||||||
|
|
||||||
check();
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="bg-orange-100 rounded-xl border-2 border-orange-400 p-2 flex flex-col gap-2">
|
<div className="bg-orange-100 rounded-xl border-2 border-orange-400 p-2 flex flex-col gap-2">
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
|
|
@ -33,6 +25,17 @@ export default function ControlCenter() {
|
||||||
/>
|
/>
|
||||||
<label htmlFor="submit">Enable Submissions</label>
|
<label htmlFor="submit">Enable Submissions</label>
|
||||||
</div>
|
</div>
|
||||||
|
<div className="flex items-center gap-2">
|
||||||
|
<input
|
||||||
|
id="queue"
|
||||||
|
type="checkbox"
|
||||||
|
className="checkbox size-6!"
|
||||||
|
placeholder="Enter banner text"
|
||||||
|
checked={isQueueEnabled}
|
||||||
|
onChange={(e) => setIsQeueueEnabled(e.target.checked)}
|
||||||
|
/>
|
||||||
|
<label htmlFor="queue">Enable Queue</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="flex gap-2 self-end">
|
<div className="flex gap-2 self-end">
|
||||||
<button type="submit" className="pill button" onClick={onClickSet}>
|
<button type="submit" className="pill button" onClick={onClickSet}>
|
||||||
|
|
|
||||||
4
src/lib/settings.ts
Normal file
4
src/lib/settings.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
export const settings = {
|
||||||
|
canSubmit: true,
|
||||||
|
queueEnabled: false,
|
||||||
|
};
|
||||||
Loading…
Reference in a new issue