co2 galvo owner test
This commit is contained in:
parent
e0ef742d64
commit
cd526dced4
1 changed files with 51 additions and 24 deletions
|
|
@ -5,12 +5,26 @@ import { useParams } from "next/navigation";
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
import Markdown from "react-markdown";
|
import Markdown from "react-markdown";
|
||||||
|
|
||||||
|
function getMaToken(): string | null {
|
||||||
|
if (typeof window === "undefined") return null;
|
||||||
|
// cookie first
|
||||||
|
const c = document.cookie
|
||||||
|
.split("; ")
|
||||||
|
.find((x) => x.startsWith("ma_at="))
|
||||||
|
?.split("=")[1];
|
||||||
|
if (c) return decodeURIComponent(c);
|
||||||
|
// localStorage fallback (if you ever store it there)
|
||||||
|
try {
|
||||||
|
const ls = window.localStorage.getItem("ma_at");
|
||||||
|
if (ls) return ls;
|
||||||
|
} catch {}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
export default function CO2GalvoSettingDetailPage() {
|
export default function CO2GalvoSettingDetailPage() {
|
||||||
const { id } = useParams();
|
const { id } = useParams<{ id: string }>();
|
||||||
const [setting, setSetting] = useState<any>(null);
|
const [setting, setSetting] = useState<any>(null);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
|
|
||||||
// claim UI state
|
|
||||||
const [claimBusy, setClaimBusy] = useState(false);
|
const [claimBusy, setClaimBusy] = useState(false);
|
||||||
const [claimMsg, setClaimMsg] = useState<string | null>(null);
|
const [claimMsg, setClaimMsg] = useState<string | null>(null);
|
||||||
const [claimErr, setClaimErr] = useState<string | null>(null);
|
const [claimErr, setClaimErr] = useState<string | null>(null);
|
||||||
|
|
@ -18,17 +32,13 @@ export default function CO2GalvoSettingDetailPage() {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!id) return;
|
if (!id) return;
|
||||||
|
|
||||||
const url =
|
const fields = [
|
||||||
`${process.env.NEXT_PUBLIC_API_BASE_URL}/items/settings_co2gal/${id}` +
|
|
||||||
`?fields=` +
|
|
||||||
[
|
|
||||||
"submission_id",
|
"submission_id",
|
||||||
"setting_title",
|
"setting_title",
|
||||||
"uploader",
|
"uploader",
|
||||||
// ── Owner (expand username) ───────────────────────────
|
// need username explicitly for M2O
|
||||||
"owner.id",
|
"owner.id",
|
||||||
"owner.username",
|
"owner.username",
|
||||||
// ─────────────────────────────────────────────────────
|
|
||||||
"setting_notes",
|
"setting_notes",
|
||||||
"photo.filename_disk",
|
"photo.filename_disk",
|
||||||
"photo.title",
|
"photo.title",
|
||||||
|
|
@ -47,7 +57,7 @@ export default function CO2GalvoSettingDetailPage() {
|
||||||
"lens_apt.name",
|
"lens_apt.name",
|
||||||
"lens_exp.name",
|
"lens_exp.name",
|
||||||
"focus",
|
"focus",
|
||||||
// laser software can be string or relation
|
// laser_soft may be string OR relation
|
||||||
"laser_soft",
|
"laser_soft",
|
||||||
"laser_soft.name",
|
"laser_soft.name",
|
||||||
"repeat_all",
|
"repeat_all",
|
||||||
|
|
@ -56,26 +66,47 @@ export default function CO2GalvoSettingDetailPage() {
|
||||||
"raster_settings",
|
"raster_settings",
|
||||||
].join(",");
|
].join(",");
|
||||||
|
|
||||||
fetch(url, { cache: "no-store" })
|
const url =
|
||||||
.then((res) => {
|
`${process.env.NEXT_PUBLIC_API_BASE_URL}/items/settings_co2gal/` +
|
||||||
if (!res.ok) throw new Error("Failed to load");
|
encodeURIComponent(String(id)) +
|
||||||
|
`?fields=${encodeURIComponent(fields)}`;
|
||||||
|
|
||||||
|
const token = getMaToken();
|
||||||
|
setLoading(true);
|
||||||
|
|
||||||
|
fetch(url, {
|
||||||
|
method: "GET",
|
||||||
|
headers: token ? { Authorization: `Bearer ${token}` } : {},
|
||||||
|
cache: "no-store",
|
||||||
|
// do NOT rely on cross-site cookies; we send the bearer explicitly
|
||||||
|
credentials: "omit",
|
||||||
|
})
|
||||||
|
.then(async (res) => {
|
||||||
|
if (!res.ok) {
|
||||||
|
const j = await res.json().catch(() => ({}));
|
||||||
|
throw new Error(j?.errors?.[0]?.message || `HTTP ${res.status}`);
|
||||||
|
}
|
||||||
return res.json();
|
return res.json();
|
||||||
})
|
})
|
||||||
.then((data) => setSetting(data.data))
|
.then((json) => setSetting(json?.data ?? null))
|
||||||
.catch(() => setSetting(null))
|
.catch((e) => {
|
||||||
|
console.error("co2-galvo fetch failed:", e);
|
||||||
|
setSetting(null);
|
||||||
|
})
|
||||||
.finally(() => setLoading(false));
|
.finally(() => setLoading(false));
|
||||||
}, [id]);
|
}, [id]);
|
||||||
|
|
||||||
if (loading) return <p className="p-6">Loading setting...</p>;
|
if (loading) return <p className="p-6">Loading setting...</p>;
|
||||||
if (!setting) return <p className="p-6">Setting not found.</p>;
|
if (!setting) return <p className="p-6">Setting not found.</p>;
|
||||||
|
|
||||||
// Owner display: prefer username if relation is expanded; otherwise show raw value/id
|
// ----- presentation helpers -----
|
||||||
const ownerDisplay: string =
|
const ownerDisplay: string =
|
||||||
typeof setting?.owner === "object"
|
typeof setting?.owner === "object"
|
||||||
? (setting.owner?.username ?? setting.owner?.id ?? "—")
|
? (setting.owner?.username ?? setting.owner?.id ?? "—")
|
||||||
: (typeof setting?.owner === "string" ? setting.owner : "—");
|
: typeof setting?.owner === "string"
|
||||||
|
? setting.owner
|
||||||
|
: "—";
|
||||||
|
|
||||||
// laser_soft may be string or relation
|
|
||||||
const softwareLabel: string =
|
const softwareLabel: string =
|
||||||
typeof setting?.laser_soft === "object"
|
typeof setting?.laser_soft === "object"
|
||||||
? (setting.laser_soft?.name ?? "—")
|
? (setting.laser_soft?.name ?? "—")
|
||||||
|
|
@ -160,12 +191,8 @@ export default function CO2GalvoSettingDetailPage() {
|
||||||
<h1 className="text-3xl font-bold mb-1">{setting.setting_title}</h1>
|
<h1 className="text-3xl font-bold mb-1">{setting.setting_title}</h1>
|
||||||
|
|
||||||
<div className="space-y-1 text-sm text-muted-foreground mb-3">
|
<div className="space-y-1 text-sm text-muted-foreground mb-3">
|
||||||
<p>
|
<p><strong>Owner:</strong> <span>{ownerDisplay}</span></p>
|
||||||
<strong>Owner:</strong> <span>{ownerDisplay}</span>
|
<p><strong>Uploader:</strong> {setting.uploader || "—"}</p>
|
||||||
</p>
|
|
||||||
<p>
|
|
||||||
<strong>Uploader:</strong> {setting.uploader || "—"}
|
|
||||||
</p>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
{ownerDisplay === "—" && (
|
{ownerDisplay === "—" && (
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue