From 011c01a741f23734e4154342e9a560925687f152 Mon Sep 17 00:00:00 2001 From: Miqdad Date: Tue, 30 Sep 2025 22:42:46 +0700 Subject: No auto login --- app/page.tsx | 128 +++++++++++++++++++++++++---------------------------------- 1 file changed, 54 insertions(+), 74 deletions(-) (limited to 'app/page.tsx') diff --git a/app/page.tsx b/app/page.tsx index add141f..af5ada3 100644 --- a/app/page.tsx +++ b/app/page.tsx @@ -6,7 +6,14 @@ import SjCamera from "./lib/camera/component/sjCamera"; import DispatchCamera from "./lib/camera/component/dispatchCamera"; import useCameraStore from "./lib/camera/hooks/useCameraStore"; import Header from "./lib/camera/component/hedear"; -import { Button, FormControl, InputLabel, MenuItem, Select, FormHelperText } from "@mui/material"; +import { + Button, + FormControl, + InputLabel, + MenuItem, + Select, + FormHelperText, +} from "@mui/material"; import { SaveAsOutlined } from "@mui/icons-material"; import axios from "axios"; import odooApi from "./lib/api/odooApi"; @@ -14,18 +21,18 @@ import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { getAuth } from "./lib/api/auth"; import { getCookie } from "cookies-next"; +import { clearOdooSession } from "./lib/api/clearOdooSession"; type Role = "driver" | "dispatch"; type ShipMethod = "" | "self_pickup" | "indoteknik_delivery" | "ekspedisi"; export default function Home() { - const [isLogin, setIsLogin] = useState(true); + const [isLogin, setIsLogin] = useState(false); // start false biar nggak nge-flash const [isDriver, setIsDriver] = useState(false); const [isDispatch, setIsDispatch] = useState(false); const [shippingMethod, setShippingMethod] = useState(""); const [shipTouched, setShipTouched] = useState(false); - const { barcode, imageSj, @@ -40,18 +47,20 @@ export default function Home() { const [isLoading, setIsLoading] = useState(false); const router = useRouter(); + // Single effect: auth gate + set role useEffect(() => { const auth = getAuth(); if (!auth) { - router.push("/login"); + void clearOdooSession(process.env.NEXT_PUBLIC_ODOO_API_HOST ?? ""); + router.replace("/login"); return; } - const roleCookie = (getCookie("web_role") as string | undefined)?.toLowerCase() as Role | undefined; - const role: Role | null = roleCookie === "driver" || roleCookie === "dispatch" ? roleCookie : null; - + const roleCookie = ( + getCookie("web_role") as string | undefined + )?.toLowerCase() as Role | undefined; + setIsDriver(roleCookie === "driver"); + setIsDispatch(roleCookie === "dispatch"); setIsLogin(true); - setIsDriver(role === "driver"); - setIsDispatch(role === "dispatch"); }, [router]); const handleSubmit = async (event: React.FormEvent) => { @@ -72,7 +81,6 @@ export default function Home() { return; } - // Validasi foto sesuai role & shipping method if (isDispatch) { if (!imageDispatch) { @@ -80,7 +88,7 @@ export default function Home() { setIsLoading(false); return; } - // SJ opsional untuk self_pickup & ekspedisi → tidak divalidasi + // SJ opsional untuk self_pickup & ekspedisi } else { // Driver: SJ & Penerima wajib if (!imageSj || !imagePackage) { @@ -92,7 +100,9 @@ export default function Home() { try { const newSjImage = imageSj ? imageSj.replace(/^.*?,/, "") : undefined; - const newPackageImage = imagePackage ? imagePackage.replace(/^.*?,/, "") : undefined; + const newPackageImage = imagePackage + ? imagePackage.replace(/^.*?,/, "") + : undefined; const newDispatchImage = imageDispatch && imageDispatch.startsWith("data:") ? imageDispatch.replace(/^.*?,/, "") @@ -107,7 +117,6 @@ export default function Home() { if (submittedSj) data.sj_document = newSjImage!; if (submittedPackage) data.paket_document = newPackageImage!; if (submittedDispatch) data.dispatch_document = newDispatchImage!; - // (opsional) kirim shippingMethod jika backend mau simpan if (isDispatch && shippingMethod) data.shipping_method = shippingMethod; const response = (await odooApi( @@ -141,23 +150,23 @@ export default function Home() { } }; + // === UI helpers === // dispatch: SJ hanya utk self_pickup & ekspedisi const showSjForDispatch = - isDispatch && (shippingMethod === "self_pickup" || shippingMethod === "ekspedisi"); - + isDispatch && + (shippingMethod === "self_pickup" || shippingMethod === "ekspedisi"); // dispatch: kamera dispatch tampil utk semua method (asal sudah dipilih) const showDispatchForDispatch = isDispatch && shippingMethod !== ""; - // preview SJ: sembunyikan kalau dispatch belum pilih method const showSjPreview = !!imageSj && (!isDispatch || showSjForDispatch); - // preview Dispatch: sembunyikan kalau dispatch belum pilih method - const showDispatchPreview = !!imageDispatch && (!isDispatch || showDispatchForDispatch); + const showDispatchPreview = + !!imageDispatch && (!isDispatch || showDispatchForDispatch); return (
- {isLogin && ( + {isLogin ? (
@@ -167,19 +176,32 @@ export default function Home() { {/* Shipping Method (khusus dispatch) */} {isDispatch && (
- - {/* shrink = kunci agar label tidak tumpuk dengan placeholder */} - Shipping Method + + + Shipping Method + {shipTouched && !shippingMethod && ( - Wajib pilih shipping method. + + Wajib pilih shipping method. + )}
)} -
@@ -210,19 +235,16 @@ export default function Home() { <> {showSjForDispatch && } {showDispatchForDispatch && } - {/* dispatch TIDAK perlu kamera penerima */} ) : ( <> {/* driver / non-dispatch */} - {/* driver tidak menampilkan kamera dispatch */} )}
-
{/* Preview SJ */} @@ -271,48 +293,7 @@ export default function Home() {
Captured -
- - )} - - -
- - {!isDispatch && imagePackage && ( - <> - -
- Captured -
- - )} - -
- - {!isDriver && imageDispatch && ( - <> - -
- Captured
- )} - {!isLogin && ( + ) : (

Loading...

-- cgit v1.2.3