1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
"use client";
import Image from "next/image";
import PackageCamera from "./lib/camera/component/pakageCamera";
import BarcodeScanner from "./lib/camera/component/scannerBarcode";
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 { SaveAsOutlined } from "@mui/icons-material";
import axios from "axios";
import odooApi from "./lib/api/odooApi";
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<boolean>(false); // start false biar nggak nge-flash
const [isDriver, setIsDriver] = useState<boolean>(false);
const [isDispatch, setIsDispatch] = useState<boolean>(false);
const [shippingMethod, setShippingMethod] = useState<ShipMethod>("");
const [shipTouched, setShipTouched] = useState(false);
const {
barcode,
imageSj,
imagePackage,
imageDispatch,
setBarcode,
setImageSj,
setImagePackage,
setImageDispatch,
} = useCameraStore();
const [isLoading, setIsLoading] = useState<boolean>(false);
const router = useRouter();
// Single effect: auth gate + set role
useEffect(() => {
const auth = getAuth();
if (!auth) {
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;
setIsDriver(roleCookie === "driver");
setIsDispatch(roleCookie === "dispatch");
setIsLogin(true);
}, [router]);
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
setIsLoading(true);
if (!barcode) {
alert("Barcode harus diisi.");
setIsLoading(false);
return;
}
// Dispatch: shipping method wajib
if (isDispatch) setShipTouched(true);
if (isDispatch && !shippingMethod) {
alert("Shipping Method wajib dipilih.");
setIsLoading(false);
return;
}
// Validasi foto sesuai role & shipping method
if (isDispatch) {
if (!imageDispatch) {
alert("Foto Dispatch Wajib Diisi");
setIsLoading(false);
return;
}
// SJ opsional untuk self_pickup & ekspedisi
} else {
// Driver: SJ & Penerima wajib
if (!imageSj || !imagePackage) {
alert("Barcode, Foto SJ, dan Foto Penerima harus tersedia.");
setIsLoading(false);
return;
}
}
try {
const newSjImage = imageSj ? imageSj.replace(/^.*?,/, "") : undefined;
const newPackageImage = imagePackage
? imagePackage.replace(/^.*?,/, "")
: undefined;
const newDispatchImage =
imageDispatch && imageDispatch.startsWith("data:")
? imageDispatch.replace(/^.*?,/, "")
: imageDispatch || undefined;
// Kirim hanya yang ada
const submittedSj = !!newSjImage;
const submittedPackage = !!newPackageImage;
const submittedDispatch = !!newDispatchImage && !isDriver;
const data: Record<string, string> = {};
if (submittedSj) data.sj_document = newSjImage!;
if (submittedPackage) data.paket_document = newPackageImage!;
if (submittedDispatch) data.dispatch_document = newDispatchImage!;
if (isDispatch && shippingMethod) data.shipping_method = shippingMethod;
const response = (await odooApi(
"PUT",
`/api/v1/stock-picking/${barcode}/documentation`,
data
)) as { status?: { code?: number } };
if (response?.status?.code === 200) {
alert("Berhasil Submit Data");
setBarcode("");
// Bersihkan HANYA yang dikirim
if (submittedSj) setImageSj("");
if (submittedPackage) setImagePackage("");
if (submittedDispatch) setImageDispatch("");
} else if (response?.status?.code === 404) {
alert("Gagal Submit Data, Picking Code Tidak Ditemukan");
} else {
alert("Gagal Submit Data, Silahkan Coba Lagi");
}
} catch (error) {
if (axios.isAxiosError(error)) {
console.error("Error:", error.response?.data);
} else if (error instanceof Error) {
console.error("Error mengirim data:", error.message);
} else {
console.error("Unknown error:", error);
}
} finally {
setIsLoading(false);
}
};
// === UI helpers ===
// dispatch: SJ hanya utk self_pickup & ekspedisi
const showSjForDispatch =
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);
return (
<div className="bg-white h-screen overflow-auto">
<Header />
{isLogin ? (
<div className="py-4 px-4 sm:px-96 pt-20">
<form onSubmit={handleSubmit}>
<div>
<BarcodeScanner />
</div>
{/* Shipping Method (khusus dispatch) */}
{isDispatch && (
<div className="mt-4">
<FormControl
fullWidth
size="small"
required
error={shipTouched && !shippingMethod}
>
<InputLabel id="shipping-label" shrink>
Shipping Method
</InputLabel>
<Select
labelId="shipping-label"
id="shipping"
label="Shipping Method"
value={shippingMethod}
displayEmpty
onChange={(e) =>
setShippingMethod(e.target.value as ShipMethod)
}
onBlur={() => setShipTouched(true)}
renderValue={(selected) => {
if (!selected)
return (
<span style={{ opacity: 0.6 }}>
Pilih Shipping Method
</span>
);
const map: Record<string, string> = {
self_pickup: "Self Pickup",
indoteknik_delivery: "Indoteknik Delivery",
ekspedisi: "Ekspedisi",
};
return map[selected as string] ?? "";
}}
>
<MenuItem value="">
<em>Pilih Shipping Method</em>
</MenuItem>
<MenuItem value="self_pickup">Self Pickup</MenuItem>
<MenuItem value="indoteknik_delivery">
Indoteknik Delivery
</MenuItem>
<MenuItem value="ekspedisi">Ekspedisi</MenuItem>
</Select>
{shipTouched && !shippingMethod && (
<FormHelperText>
Wajib pilih shipping method.
</FormHelperText>
)}
</FormControl>
</div>
)}
<div className="h-4" />
<div className="flex justify-between">
{isDispatch ? (
<>
{showSjForDispatch && <SjCamera />}
{showDispatchForDispatch && <DispatchCamera />}
</>
) : (
<>
{/* driver / non-dispatch */}
<SjCamera />
<PackageCamera />
</>
)}
</div>
<div className="h-2" />
{/* Preview SJ */}
{showSjPreview && (
<>
<label className="block mt-2 text-sm font-medium text-gray-700 text-center">
Gambar Foto Surat Jalan
</label>
<div className="relative w-full h-[300px] border-2 border-gray-200 p-2 rounded-sm">
<Image
src={imageSj!}
alt="Captured"
fill
unoptimized
className="p-2"
style={{ objectFit: "cover" }}
/>
</div>
</>
)}
{/* Preview Penerima (hanya non-dispatch) */}
{!isDispatch && imagePackage && (
<>
<label className="block mt-2 text-sm font-medium text-gray-700 text-center">
Gambar Foto Penerima
</label>
<div className="relative w-full h-[300px] border-2 border-gray-200 p-2 rounded-sm">
<Image
src={imagePackage}
alt="Captured"
fill
unoptimized
className="p-2"
style={{ objectFit: "cover" }}
/>
</div>
</>
)}
{/* Preview Dispatch */}
{showDispatchPreview && (
<>
<label className="block mt-2 text-sm font-medium text-gray-700 text-center">
Gambar Foto Dispatch
</label>
<div className="relative w-full h-[300px] border-2 border-gray-200 p-2 rounded-sm">
<Image
src={imageDispatch!}
alt="Captured"
fill
unoptimized
className="p-2"
style={{ objectFit: "cover" }}
/>
</div>
</>
)}
<div className="h-4" />
<Button
className="w-[50%] sm:w-[25%]"
variant="contained"
color="error"
startIcon={<SaveAsOutlined />}
type="submit"
disabled={isLoading}
>
Simpan
</Button>
</form>
</div>
) : (
<div className="py-4 px-4 sm:px-96 pt-20">
<div className="text-center">
<p className="text-2xl font-bold">Loading...</p>
</div>
</div>
)}
</div>
);
}
|