From 64db7e7f8e4eb78e31d13bc348a150c7813c2354 Mon Sep 17 00:00:00 2001 From: marszayn Date: Mon, 10 Mar 2025 14:05:44 +0700 Subject: [PATCH] refactor: Perbarui logika pengelolaan file terunggah --- app/Http/Controllers/HukumController.php | 10 +- .../components/modals/add-penaatan-modal.tsx | 10 +- .../js/components/modals/add-sanksi-hukum.tsx | 10 +- .../modals/edit-perusahaan-modal.tsx | 837 ++++++++++++++++++ 4 files changed, 857 insertions(+), 10 deletions(-) create mode 100644 resources/js/components/modals/edit-perusahaan-modal.tsx diff --git a/app/Http/Controllers/HukumController.php b/app/Http/Controllers/HukumController.php index 2103a25..88a14c9 100644 --- a/app/Http/Controllers/HukumController.php +++ b/app/Http/Controllers/HukumController.php @@ -70,7 +70,10 @@ class HukumController extends Controller $data = $request->validated(); - if ($request->hasFile('SanksiFile')) { + // Preserve existing SanksiFile if no new file is uploaded + if (!$request->hasFile('SanksiFile')) { + unset($data['SanksiFile']); + } else { // Delete old file if exists if ($hukum->SanksiFile && Storage::disk('public')->exists($hukum->SanksiFile)) { Storage::disk('public')->delete($hukum->SanksiFile); @@ -81,7 +84,10 @@ class HukumController extends Controller $data['SanksiFile'] = $path; } - if ($request->hasFile('PenaatanFile')) { + // Preserve existing PenaatanFile if no new file is uploaded + if (!$request->hasFile('PenaatanFile')) { + unset($data['PenaatanFile']); + } else { // Delete old file if exists if ($hukum->PenaatanFile && Storage::disk('public')->exists($hukum->PenaatanFile)) { Storage::disk('public')->delete($hukum->PenaatanFile); diff --git a/resources/js/components/modals/add-penaatan-modal.tsx b/resources/js/components/modals/add-penaatan-modal.tsx index b4d1106..41a259d 100644 --- a/resources/js/components/modals/add-penaatan-modal.tsx +++ b/resources/js/components/modals/add-penaatan-modal.tsx @@ -10,7 +10,7 @@ import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import Select from "react-select"; import { useState, useEffect } from "react"; -import { useForm } from "@inertiajs/react"; +import { router, useForm } from "@inertiajs/react"; import { useToast } from "@/hooks/use-toast"; import { HukumType } from "@/types/perusahaan"; @@ -92,6 +92,7 @@ export function AddPenaatanModal({ data: formData, forceFormData: true, onSuccess: () => { + router.visit(window.location.href, { replace: true }); toast({ title: "Berhasil", description: "Data Penaatan berhasil diperbarui", @@ -166,11 +167,12 @@ export function AddPenaatanModal({ /> {editingData?.PenaatanFile && (
- + {/* File saat ini:{" "} {editingData.PenaatanFile} - + */}
)} diff --git a/resources/js/components/modals/add-sanksi-hukum.tsx b/resources/js/components/modals/add-sanksi-hukum.tsx index db2d504..7d4ec82 100644 --- a/resources/js/components/modals/add-sanksi-hukum.tsx +++ b/resources/js/components/modals/add-sanksi-hukum.tsx @@ -10,7 +10,7 @@ import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import Select from "react-select"; import { useState, useEffect } from "react"; -import { useForm } from "@inertiajs/react"; +import { router, useForm } from "@inertiajs/react"; import { useToast } from "@/hooks/use-toast"; import { HukumType } from "@/types/perusahaan"; @@ -131,6 +131,7 @@ export function AddHukumModal({ data: formData, forceFormData: true, onSuccess: () => { + router.visit(window.location.href, { replace: true }); toast({ title: "Berhasil", description: editingData @@ -222,10 +223,11 @@ export function AddHukumModal({ /> {editingData?.SanksiFile && (
- + {/* File saat ini: {editingData.SanksiFile} - + */}
)} diff --git a/resources/js/components/modals/edit-perusahaan-modal.tsx b/resources/js/components/modals/edit-perusahaan-modal.tsx new file mode 100644 index 0000000..ada9ae2 --- /dev/null +++ b/resources/js/components/modals/edit-perusahaan-modal.tsx @@ -0,0 +1,837 @@ +import { Button } from "@/components/ui/button"; +import { + Dialog, + DialogContent, + DialogDescription, + DialogFooter, + DialogHeader, + DialogTitle, +} from "@/components/ui/dialog"; +import { Input } from "@/components/ui/input"; +import { Label } from "@/components/ui/label"; +import Select from "react-select"; +import { useState, useEffect } from "react"; +import { Checkbox } from "@/components/ui/checkbox"; +import { + FileText, + Hotel, + LocateFixed, + MapPinned, + UsersRound, + VerifiedIcon, +} from "lucide-react"; +import { Separator } from "../ui/separator"; +import { Textarea } from "../ui/textarea"; + +import { + JenisDokIL, + JenisKegiatan, + Verifikator, + Kabupaten, + Kecamatan, + Kelurahan, + Perusahaan, +} from "@/types/perusahaan"; +import { useForm } from "@inertiajs/react"; +import { useToast } from "@/hooks/use-toast"; + +interface EditPerusahaanModalProps { + open: boolean; + onClose: () => void; + onSuccess: () => void; + jenisKegiatan: JenisKegiatan[]; + jenisDokIL: JenisDokIL[]; + verifikator: Verifikator[]; + kabupaten: Kabupaten[]; + kecamatan: Kecamatan[]; + kelurahan: Kelurahan[]; + perusahaan: Perusahaan | null; +} + +export function EditPerusahaanModal({ + open, + onClose, + onSuccess, + jenisKegiatan, + jenisDokIL, + verifikator, + kabupaten, + kecamatan, + kelurahan, + perusahaan, +}: EditPerusahaanModalProps) { + const { toast } = useToast(); + const [loading, setLoading] = useState(false); + + const { + data, + setData, + post, + reset, + errors: formErrors, + } = useForm({ + NomorInduk: "", + PerusahaanId: "", + JenisKegiatanId: "", + NamaPerusahaan: "", + Alamat: "", + KelurahanId: "", + KodePos: "", + Telepon: "", + Fax: "", + Email: "", + Lintang: "", + Bujur: "", + CPNama: "", + CPTelepon: "", + JenisDokILId: "", + VerifikatorId: "", + IsPublish: true, + ILDokumen: null as File | null, + ILNomor: "", + ILTanggal: "", + ReportLocked: true, + }); + + const [selectedKabupaten, setSelectedKabupaten] = useState<{ + value: number; + label: string; + } | null>(null); + + const [selectedKecamatan, setSelectedKecamatan] = useState<{ + value: number; + label: string; + } | null>(null); + + const [selectedKelurahan, setSelectedKelurahan] = useState<{ + value: number; + label: string; + } | null>(null); + + useEffect(() => { + if (perusahaan) { + setData({ + NomorInduk: perusahaan.NomorInduk || "", + PerusahaanId: perusahaan.PerusahaanId.toString(), + JenisKegiatanId: perusahaan.JenisKegiatanId?.toString() || "", + NamaPerusahaan: perusahaan.NamaPerusahaan || "", + Alamat: perusahaan.Alamat || "", + KelurahanId: perusahaan.KelurahanId?.toString() || "", + KodePos: perusahaan.KodePos || "", + Telepon: perusahaan.Telepon || "", + Fax: perusahaan.Fax || "", + Email: perusahaan.Email || "", + Lintang: perusahaan.Lintang || "", + Bujur: perusahaan.Bujur || "", + CPNama: perusahaan.CPNama || "", + CPTelepon: perusahaan.CPTelepon || "", + JenisDokILId: perusahaan.JenisDokILId?.toString() || "", + VerifikatorId: perusahaan.VerifikatorId?.toString() || "", + IsPublish: + perusahaan.IsPublish !== undefined + ? perusahaan.IsPublish + : true, + ILDokumen: null, // Files need to be uploaded again if needed + ILNomor: perusahaan.ILNomor || "", + ILTanggal: perusahaan.ILTanggal || "", + ReportLocked: perusahaan.ReportLocked || true, + }); + + // Set location dropdown selections + if (perusahaan.kelurahan && perusahaan.KelurahanId) { + const currentKelurahan = kelurahan.find( + (k) => k.KelurahanId === perusahaan.KelurahanId + ); + + if (currentKelurahan && currentKelurahan.KecamatanId) { + const currentKecamatan = kecamatan.find( + (k) => k.KecamatanId === currentKelurahan.KecamatanId + ); + + if (currentKecamatan && currentKecamatan.KabupatenId) { + // Set kabupaten + const kabupatenData = kabupaten.find( + (k) => + k.KabupatenId === currentKecamatan.KabupatenId + ); + if (kabupatenData) { + setSelectedKabupaten({ + value: kabupatenData.KabupatenId, + label: kabupatenData.NamaKabupaten, + }); + } + + // Set kecamatan + setSelectedKecamatan({ + value: currentKecamatan.KecamatanId, + label: currentKecamatan.NamaKecamatan, + }); + + // Set kelurahan + setSelectedKelurahan({ + value: currentKelurahan.KelurahanId, + label: currentKelurahan.NamaKelurahan, + }); + } + } + } + } + }, [perusahaan, kecamatan, kelurahan, kabupaten, setData]); + + const jenisKegiatanOptions = jenisKegiatan.map((jk) => ({ + value: jk.JenisKegiatanId, + label: jk.NamaJenisKegiatan, + })); + + const jenisDokILOptions = jenisDokIL.map((jdi) => ({ + value: jdi.JenisDokILId, + label: jdi.NamaJenisDokIL, + })); + + const verifikatorOptions = verifikator.map((v) => ({ + value: v.VerifikatorId, + label: v.NamaUnitKerja, + })); + + const kabupatenOptions = kabupaten.map((k) => ({ + value: k.KabupatenId, + label: k.NamaKabupaten, + })); + + const kecamatanOptions = kecamatan + .filter( + (kec) => + selectedKabupaten && kec.KabupatenId === selectedKabupaten.value + ) + .map((kec) => ({ + value: kec.KecamatanId, + label: kec.NamaKecamatan, + })); + + const kelurahanOptions = kelurahan + .filter( + (kel) => + selectedKecamatan && kel.KecamatanId === selectedKecamatan.value + ) + .map((kel) => ({ + value: kel.KelurahanId, + label: kel.NamaKelurahan, + })); + + const handleSubmit = (e: React.FormEvent) => { + e.preventDefault(); + + if (!perusahaan) { + toast({ + title: "Error", + description: "Data perusahaan tidak ditemukan", + variant: "destructive", + }); + return; + } + + setLoading(true); + + const formData = new FormData(); + + // Add all form fields to FormData + Object.entries(data).forEach(([key, value]) => { + if (value !== null && key !== "ILDokumen") { + formData.append(key, value.toString()); + } + }); + + // Add file only if a new one was selected + if (data.ILDokumen) { + formData.append("ILDokumen", data.ILDokumen); + } + + post(`/admin/perusahaan/${perusahaan.PerusahaanId}`, { + data: formData, + forceFormData: true, + onSuccess: () => { + toast({ + title: "Berhasil", + description: "Data perusahaan berhasil diperbarui", + variant: "default", + }); + reset(); + onSuccess(); + onClose(); + }, + onError: (error) => { + console.error("Error:", error); + toast({ + title: "Gagal", + description: "Terjadi kesalahan saat mengupdate perusahaan", + variant: "destructive", + }); + }, + onFinish: () => { + setLoading(false); + }, + }); + }; + + return ( + + + + Edit Data Perusahaan + + Perbarui informasi {perusahaan?.NamaPerusahaan || ""}{" "} + dalam sistem. + + +
+
+ {/* Sisi Kiri */} +
+
+
+ +

+ Data Perusahaan +

+
+ +
+ + + setData({ + ...data, + NomorInduk: e.target.value, + }) + } + /> + {formErrors.NomorInduk && ( +

+ {formErrors.NomorInduk} +

+ )} +
+ +
+ + + setData({ + ...data, + NamaPerusahaan: e.target.value, + }) + } + /> + {formErrors.NamaPerusahaan && ( +

+ {formErrors.NamaPerusahaan} +

+ )} +
+
+ + + option.value.toString() === + data.VerifikatorId + )} + onChange={(option) => + setData({ + ...data, + VerifikatorId: + option?.value?.toString() || + "", + }) + } + /> + {formErrors.VerifikatorId && ( +

+ {formErrors.VerifikatorId} +

+ )} +
+ + + + {/* Kontak Person */} +
+
+ +

+ Kontak Person +

+
+ +
+ + + setData({ + ...data, + CPNama: e.target.value, + }) + } + /> +
+ +
+ + + setData({ + ...data, + CPTelepon: e.target.value, + }) + } + /> +
+
+ + + {/* Dokumen Izin */} +
+
+ +

+ Dokumen Izin +

+
+ +
+ + + setData({ + ...data, + ILNomor: e.target.value, + }) + } + /> +
+ +
+ + + setData({ + ...data, + ILTanggal: e.target.value, + }) + } + /> +
+ +
+ + + setData({ + ...data, + ILDokumen: e.target.files + ? e.target.files[0] + : null, + }) + } + /> + {perusahaan?.ILDokumen && ( + //
+ // Dokumen saat ini:{" "} + // {perusahaan.ILDokumen} + // + // Lihat dokumen + // + //
+ + )} +
+
+
+
+ + {/* Sisi Kanan */} +
+
+
+ +

+ Alamat Perusahaan +

+
+
+ + { + setSelectedKecamatan(value); + setSelectedKelurahan(null); + setData({ + ...data, + KelurahanId: "", + }); + }} + placeholder="Pilih Kecamatan" + isSearchable + isDisabled={!selectedKabupaten} + required + /> +
+ +
+ +