diff --git a/apps/admin-web/src/app/api/admin/locations/route.ts b/apps/admin-web/src/app/api/admin/locations/route.ts new file mode 100644 index 0000000..3dfd20d --- /dev/null +++ b/apps/admin-web/src/app/api/admin/locations/route.ts @@ -0,0 +1,30 @@ +import { cookies } from "next/headers"; +import { NextRequest, NextResponse } from "next/server"; + +export async function POST(request: NextRequest) { + const cookieStore = await cookies(); + const authCookieName = process.env.ADMIN_AUTH_COOKIE || "admin_token"; + const token = cookieStore.get(authCookieName)?.value || ""; + const apiBase = process.env.NEXT_PUBLIC_API_URL || ""; + + if (!apiBase || !token) { + return NextResponse.json( + { success: false, error: { code: "ADMIN_AUTH_REQUIRED", message: "Admin authentication is required." } }, + { status: 401 } + ); + } + + const payload = await request.json(); + const response = await fetch(`${apiBase}/admin/locations`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + }, + body: JSON.stringify(payload), + cache: "no-store", + }); + + const data = await response.json(); + return NextResponse.json(data, { status: response.status }); +} diff --git a/apps/admin-web/src/components/admin/LocationCreationForm.tsx b/apps/admin-web/src/components/admin/LocationCreationForm.tsx index 01d7a51..6744e0c 100644 --- a/apps/admin-web/src/components/admin/LocationCreationForm.tsx +++ b/apps/admin-web/src/components/admin/LocationCreationForm.tsx @@ -1,5 +1,6 @@ "use client"; +import { useRouter } from "next/navigation"; import { useEffect, useMemo, useState } from "react"; import { useForm } from "react-hook-form"; import { z } from "zod"; @@ -42,10 +43,13 @@ function RecenterOnCoordinate({ latitude, longitude }: { latitude: number; longi } export default function LocationCreationForm() { + const router = useRouter(); const [query, setQuery] = useState(""); const [geocodeLoading, setGeocodeLoading] = useState(false); const [geocodeResults, setGeocodeResults] = useState([]); const [submitPayload, setSubmitPayload] = useState(""); + const [submitError, setSubmitError] = useState(""); + const [submitSuccess, setSubmitSuccess] = useState(""); const { register, @@ -100,6 +104,8 @@ export default function LocationCreationForm() { }; const onSubmit = async (values: FormValues) => { + setSubmitError(""); + setSubmitSuccess(""); const payload = { ...values, location: { @@ -107,7 +113,33 @@ export default function LocationCreationForm() { coordinates: [values.longitude, values.latitude], }, }; - setSubmitPayload(JSON.stringify(payload, null, 2)); + + try { + const response = await fetch("/api/admin/locations", { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify(payload), + }); + + const result = (await response.json()) as { + success?: boolean; + error?: { + message?: string; + }; + }; + + if (!response.ok || !result.success) { + throw new Error(result.error?.message || "Unable to save location."); + } + + setSubmitPayload(JSON.stringify(payload, null, 2)); + setSubmitSuccess("Location created successfully."); + router.refresh(); + } catch (error) { + setSubmitError(error instanceof Error ? error.message : "Unable to save location."); + } }; return ( @@ -234,10 +266,12 @@ export default function LocationCreationForm() { + {submitError ?

{submitError}

: null} + {submitSuccess ?

{submitSuccess}

: null} {submitPayload &&
{submitPayload}
}