Files
InaTrading-Portal/src/app/admin/places/[placeId]/edit/page.tsx
2026-04-24 05:19:05 +07:00

82 lines
2.9 KiB
TypeScript

"use client";
import { useParams } from "next/navigation";
import { useState } from "react";
import { PlaceForm, PlaceFormState, defaultPlaceForm } from "../../PlaceForm";
function getCachedPlace(placeId: string): PlaceFormState | null {
if (typeof window === "undefined" || !placeId) return null;
const cached = sessionStorage.getItem("editPlaceCache");
if (!cached) return null;
try {
const raw = JSON.parse(cached) as Record<string, unknown>;
if (raw.id !== placeId) return null;
sessionStorage.removeItem("editPlaceCache");
return {
name: typeof raw.name === "string" ? raw.name : "",
description: typeof raw.description === "string" ? raw.description : "",
type: typeof raw.type === "string" ? raw.type : "",
contact: typeof raw.contact === "string" ? raw.contact : "",
address: typeof raw.address === "string" ? raw.address : "",
country: typeof raw.country === "string" ? raw.country : "Indonesia",
province: typeof raw.province === "string" ? raw.province : "",
provinceId: "",
city: typeof raw.city === "string" ? raw.city : "",
cityId: "",
latitude: raw.latitude != null ? String(raw.latitude) : "",
longitude: raw.longitude != null ? String(raw.longitude) : "",
status: typeof raw.status === "string" ? raw.status : "APPROVED",
image1: typeof raw.image1 === "string" ? raw.image1 : "",
image2: typeof raw.image2 === "string" ? raw.image2 : "",
image3: typeof raw.image3 === "string" ? raw.image3 : "",
image4: typeof raw.image4 === "string" ? raw.image4 : "",
image5: typeof raw.image5 === "string" ? raw.image5 : "",
};
} catch {
return null;
}
}
export default function AdminPlacesEditPage() {
const params = useParams<{ placeId: string }>();
const [initialData] = useState<PlaceFormState | null>(() =>
getCachedPlace(params.placeId)
);
const loading = false;
const loadError = initialData
? ""
: "Data tidak tersedia. Kembali ke daftar lokasi dan klik Edit lagi.";
if (loading) {
return (
<div className="m-6 flex items-center justify-center h-64">
<span className="material-symbols-outlined text-4xl text-slate-300 animate-spin">progress_activity</span>
</div>
);
}
if (loadError) {
return (
<div className="m-6 flex flex-col items-center justify-center h-64 gap-4">
<span className="material-symbols-outlined text-4xl text-error">error</span>
<p className="text-sm font-semibold text-error">{loadError}</p>
</div>
);
}
return (
<PlaceForm
initialData={initialData ?? defaultPlaceForm}
pageTitle="Edit Place"
pageSubtitle="Perbarui informasi lokasi"
submitLabel="Simpan Perubahan"
submittingLabel="Menyimpan..."
successMessage="Lokasi berhasil diperbarui! Mengalihkan..."
apiMethod="PUT"
apiUrl={`/api/admin/places/${params.placeId}`}
/>
);
}