2024-12-03 17:49:25 +00:00
|
|
|
"use client";
|
2024-12-17 14:27:47 +00:00
|
|
|
import React, { useEffect, useRef, useState } from "react";
|
2024-12-03 17:49:25 +00:00
|
|
|
import { useForm, Controller } from "react-hook-form";
|
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { Label } from "@/components/ui/label";
|
|
|
|
|
import { Card } from "@/components/ui/card";
|
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
|
import * as z from "zod";
|
|
|
|
|
import Swal from "sweetalert2";
|
|
|
|
|
import withReactContent from "sweetalert2-react-content";
|
2024-12-17 14:27:47 +00:00
|
|
|
import { useParams, useRouter } from "next/navigation";
|
2024-12-03 17:49:25 +00:00
|
|
|
import {
|
|
|
|
|
Select,
|
|
|
|
|
SelectContent,
|
|
|
|
|
SelectItem,
|
|
|
|
|
SelectTrigger,
|
|
|
|
|
SelectValue,
|
|
|
|
|
} from "@/components/ui/select";
|
|
|
|
|
import { Checkbox } from "@/components/ui/checkbox";
|
|
|
|
|
import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group";
|
|
|
|
|
import JoditEditor from "jodit-react";
|
2024-12-17 14:27:47 +00:00
|
|
|
import { createTask, getTask } from "@/service/task";
|
2024-12-03 17:49:25 +00:00
|
|
|
|
|
|
|
|
const taskSchema = z.object({
|
|
|
|
|
title: z.string().min(1, { message: "Judul diperlukan" }),
|
|
|
|
|
naration: z.string().min(2, {
|
|
|
|
|
message: "Narasi Penugasan harus lebih dari 2 karakter.",
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
2024-12-17 14:27:47 +00:00
|
|
|
export type taskDetail = {
|
|
|
|
|
id: number;
|
|
|
|
|
title: string;
|
|
|
|
|
fileTypeOutput: string;
|
|
|
|
|
assignedToTopLevel: string;
|
|
|
|
|
assignmentType: {
|
|
|
|
|
id: number;
|
|
|
|
|
name: string;
|
|
|
|
|
};
|
|
|
|
|
assignmentMainType: {
|
|
|
|
|
id: number;
|
|
|
|
|
name: string;
|
|
|
|
|
};
|
|
|
|
|
taskType: string;
|
|
|
|
|
broadcastType: string;
|
|
|
|
|
narration: string;
|
|
|
|
|
is_active: string;
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-03 17:49:25 +00:00
|
|
|
export default function FormTask() {
|
|
|
|
|
const MySwal = withReactContent(Swal);
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
const editor = useRef(null);
|
|
|
|
|
type TaskSchema = z.infer<typeof taskSchema>;
|
2024-12-17 14:27:47 +00:00
|
|
|
const { id } = useParams() as { id: string };
|
|
|
|
|
console.log(id);
|
2024-12-03 17:49:25 +00:00
|
|
|
|
|
|
|
|
// State for various form fields
|
2024-12-05 17:02:06 +00:00
|
|
|
const [taskOutput, setTaskOutput] = useState({
|
2024-12-03 17:49:25 +00:00
|
|
|
all: false,
|
|
|
|
|
video: false,
|
|
|
|
|
audio: false,
|
|
|
|
|
image: false,
|
|
|
|
|
text: false,
|
|
|
|
|
});
|
|
|
|
|
|
2024-12-09 13:17:22 +00:00
|
|
|
// const [assignmentType, setAssignmentType] = useState("mediahub");
|
|
|
|
|
// const [assignmentCategory, setAssignmentCategory] = useState("publication");
|
2024-12-17 14:27:47 +00:00
|
|
|
const [mainType, setMainType] = useState<string>("1");
|
2024-12-16 14:05:10 +00:00
|
|
|
const [taskType, setTaskType] = useState<string>("atensi-khusus");
|
2024-12-17 14:27:47 +00:00
|
|
|
const [broadcastType, setBroadcastType] = useState<string>(""); // untuk Tipe Penugasan
|
2024-12-05 17:02:06 +00:00
|
|
|
const [type, setType] = useState<string>("1");
|
2024-12-03 17:49:25 +00:00
|
|
|
const [selectedTarget, setSelectedTarget] = useState("all");
|
2024-12-17 14:27:47 +00:00
|
|
|
const [detail, setDetail] = useState<taskDetail>();
|
|
|
|
|
const [refresh] = useState(false);
|
2024-12-05 17:02:06 +00:00
|
|
|
|
|
|
|
|
const [platformTypeVisible, setPlatformTypeVisible] = useState(false);
|
2024-12-03 17:49:25 +00:00
|
|
|
const [unitSelection, setUnitSelection] = useState({
|
|
|
|
|
allUnit: false,
|
|
|
|
|
mabes: false,
|
|
|
|
|
polda: false,
|
|
|
|
|
polres: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
control,
|
|
|
|
|
handleSubmit,
|
|
|
|
|
formState: { errors },
|
|
|
|
|
} = useForm<TaskSchema>({
|
|
|
|
|
resolver: zodResolver(taskSchema),
|
|
|
|
|
});
|
|
|
|
|
|
2024-12-17 14:27:47 +00:00
|
|
|
// const handleRadioChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
// const selectedValue = Number(event.target.value);
|
|
|
|
|
// setMainType(selectedValue);
|
2024-12-05 17:02:06 +00:00
|
|
|
|
2024-12-17 14:27:47 +00:00
|
|
|
// setPlatformTypeVisible(selectedValue === 2);
|
|
|
|
|
// };
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
async function initState() {
|
|
|
|
|
if (id) {
|
|
|
|
|
const response = await getTask(id);
|
|
|
|
|
const details = response.data?.data;
|
|
|
|
|
|
|
|
|
|
setDetail(details);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
initState();
|
|
|
|
|
}, [id, refresh]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (detail?.broadcastType) {
|
|
|
|
|
setBroadcastType(detail.broadcastType); // Mengatur nilai broadcastType dari API
|
|
|
|
|
}
|
|
|
|
|
}, [detail?.broadcastType]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (detail?.fileTypeOutput) {
|
|
|
|
|
const outputSet = new Set(detail.fileTypeOutput.split(",").map(Number)); // Membagi string ke dalam array dan mengonversi ke nomor
|
|
|
|
|
setTaskOutput({
|
|
|
|
|
all: outputSet.has(0),
|
|
|
|
|
video: outputSet.has(2),
|
|
|
|
|
audio: outputSet.has(4),
|
|
|
|
|
image: outputSet.has(1),
|
|
|
|
|
text: outputSet.has(3),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [detail?.fileTypeOutput]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (detail?.assignedToTopLevel) {
|
|
|
|
|
const outputSet = new Set(
|
|
|
|
|
detail.assignedToTopLevel.split(",").map(Number)
|
|
|
|
|
);
|
|
|
|
|
setUnitSelection({
|
|
|
|
|
allUnit: outputSet.has(0),
|
|
|
|
|
mabes: outputSet.has(1),
|
|
|
|
|
polda: outputSet.has(2),
|
|
|
|
|
polres: outputSet.has(3),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [detail?.fileTypeOutput]);
|
2024-12-05 17:02:06 +00:00
|
|
|
|
2024-12-03 17:49:25 +00:00
|
|
|
const save = async (data: TaskSchema) => {
|
2024-12-09 13:17:22 +00:00
|
|
|
const fileTypeMapping = {
|
|
|
|
|
all: "1",
|
|
|
|
|
video: "2",
|
|
|
|
|
audio: "3",
|
|
|
|
|
image: "4",
|
|
|
|
|
text: "5",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const selectedOutputs = Object.keys(taskOutput)
|
|
|
|
|
.filter((key) => taskOutput[key as keyof typeof taskOutput]) // Ambil hanya yang `true`
|
|
|
|
|
.map((key) => fileTypeMapping[key as keyof typeof fileTypeMapping]) // Konversi ke nilai string
|
|
|
|
|
.join(",");
|
|
|
|
|
|
2024-12-03 17:49:25 +00:00
|
|
|
const requestData = {
|
|
|
|
|
...data,
|
2024-12-09 13:17:22 +00:00
|
|
|
// assignmentType,
|
|
|
|
|
// assignmentCategory,
|
2024-12-03 17:49:25 +00:00
|
|
|
target: selectedTarget,
|
|
|
|
|
unitSelection,
|
2024-12-05 17:02:06 +00:00
|
|
|
assignedToRole: "3",
|
2024-12-16 14:05:10 +00:00
|
|
|
taskType: taskType,
|
|
|
|
|
broadcastType: broadcastType,
|
2024-12-09 13:17:22 +00:00
|
|
|
assignmentMainTypeId: mainType,
|
2024-12-05 17:02:06 +00:00
|
|
|
assignmentPurpose: "1",
|
|
|
|
|
assignmentTypeId: type,
|
2024-12-09 13:17:22 +00:00
|
|
|
fileTypeOutput: selectedOutputs,
|
2024-12-05 17:02:06 +00:00
|
|
|
id: null,
|
|
|
|
|
narration: data.naration,
|
|
|
|
|
platformType: "",
|
|
|
|
|
title: data.title,
|
2024-12-03 17:49:25 +00:00
|
|
|
};
|
|
|
|
|
|
2024-12-05 17:02:06 +00:00
|
|
|
const response = await createTask(requestData);
|
|
|
|
|
|
2024-12-03 17:49:25 +00:00
|
|
|
console.log("Form Data Submitted:", requestData);
|
2024-12-05 17:02:06 +00:00
|
|
|
console.log("response", response);
|
2024-12-03 17:49:25 +00:00
|
|
|
|
|
|
|
|
MySwal.fire({
|
|
|
|
|
title: "Sukses",
|
|
|
|
|
text: "Data berhasil disimpan.",
|
|
|
|
|
icon: "success",
|
|
|
|
|
confirmButtonColor: "#3085d6",
|
|
|
|
|
confirmButtonText: "OK",
|
|
|
|
|
}).then(() => {
|
2024-12-16 14:05:10 +00:00
|
|
|
router.push("/en/contributor/task");
|
2024-12-03 17:49:25 +00:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSubmit = (data: TaskSchema) => {
|
|
|
|
|
MySwal.fire({
|
|
|
|
|
title: "Simpan Data",
|
|
|
|
|
text: "Apakah Anda yakin ingin menyimpan data ini?",
|
|
|
|
|
icon: "warning",
|
|
|
|
|
showCancelButton: true,
|
|
|
|
|
cancelButtonColor: "#d33",
|
|
|
|
|
confirmButtonColor: "#3085d6",
|
|
|
|
|
confirmButtonText: "Simpan",
|
|
|
|
|
}).then((result) => {
|
|
|
|
|
if (result.isConfirmed) {
|
|
|
|
|
save(data);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Card>
|
|
|
|
|
<div className="px-6 py-6">
|
|
|
|
|
<p className="text-lg font-semibold mb-3">Form Penugasan</p>
|
2024-12-17 14:27:47 +00:00
|
|
|
{detail !== undefined ? (
|
|
|
|
|
<form onSubmit={handleSubmit(onSubmit)}>
|
|
|
|
|
<div className="gap-5 mb-5">
|
|
|
|
|
{/* Input Title */}
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
<Label>Judul</Label>
|
|
|
|
|
<Controller
|
|
|
|
|
control={control}
|
|
|
|
|
name="title"
|
|
|
|
|
render={({ field }) => (
|
|
|
|
|
<Input
|
|
|
|
|
size="md"
|
|
|
|
|
type="text"
|
|
|
|
|
value={detail?.title}
|
|
|
|
|
onChange={field.onChange}
|
|
|
|
|
placeholder="Enter Title"
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
{errors.title?.message && (
|
|
|
|
|
<p className="text-red-400 text-sm">{errors.title.message}</p>
|
2024-12-03 17:49:25 +00:00
|
|
|
)}
|
2024-12-17 14:27:47 +00:00
|
|
|
</div>
|
|
|
|
|
<div className="flex flex-row items-center">
|
|
|
|
|
<div className="mt-5">
|
|
|
|
|
<Label>Tujuan Pemilihan Tugas</Label>
|
|
|
|
|
<Select onValueChange={setSelectedTarget}>
|
|
|
|
|
<SelectTrigger size="md">
|
|
|
|
|
<SelectValue placeholder="Pilih" />
|
|
|
|
|
</SelectTrigger>
|
|
|
|
|
<SelectContent>
|
|
|
|
|
<SelectItem value="all">Semua Pengguna</SelectItem>
|
|
|
|
|
<SelectItem value="contributor">Kontributor</SelectItem>
|
|
|
|
|
<SelectItem value="approver">Approver</SelectItem>
|
|
|
|
|
</SelectContent>
|
|
|
|
|
</Select>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex flex-wrap gap-3 mt-5 pt-5 ml-3">
|
|
|
|
|
{Object.keys(unitSelection).map((key) => (
|
|
|
|
|
<div className="flex items-center gap-2" key={key}>
|
|
|
|
|
<Checkbox
|
|
|
|
|
id={key}
|
|
|
|
|
checked={
|
|
|
|
|
unitSelection[key as keyof typeof unitSelection]
|
|
|
|
|
}
|
|
|
|
|
onCheckedChange={(value) =>
|
|
|
|
|
setUnitSelection({ ...unitSelection, [key]: value })
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
<Label htmlFor={key}>
|
|
|
|
|
{key.charAt(0).toUpperCase() + key.slice(1)}
|
|
|
|
|
</Label>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2024-12-03 17:49:25 +00:00
|
|
|
<div className="mt-5">
|
2024-12-17 14:27:47 +00:00
|
|
|
<Label>Tipe Penugasan</Label>
|
|
|
|
|
<RadioGroup
|
|
|
|
|
value={detail.assignmentMainType.id.toString()} // State yang dipetakan ke value RadioGroup
|
|
|
|
|
onValueChange={(value) => setMainType(value)}
|
|
|
|
|
// value={String(mainType)}
|
|
|
|
|
// onValueChange={(value) => setMainType(Number(value))}
|
|
|
|
|
className="flex flex-wrap gap-3"
|
|
|
|
|
>
|
|
|
|
|
<RadioGroupItem value="1" id="mediahub" />
|
|
|
|
|
<Label htmlFor="mediahub">Mediahub</Label>
|
|
|
|
|
<RadioGroupItem value="2" id="medsos-mediahub" />
|
|
|
|
|
<Label htmlFor="medsos-mediahub">Medsos Mediahub</Label>
|
|
|
|
|
</RadioGroup>
|
2024-12-03 17:49:25 +00:00
|
|
|
</div>
|
2024-12-17 14:27:47 +00:00
|
|
|
<div className="mt-5">
|
|
|
|
|
<Label>Jenis Tugas </Label>
|
|
|
|
|
<RadioGroup
|
|
|
|
|
value={detail.taskType.toString()}
|
|
|
|
|
onValueChange={(value) => setTaskType(String(value))}
|
|
|
|
|
className="flex flex-wrap gap-3"
|
|
|
|
|
>
|
|
|
|
|
<RadioGroupItem value="atensi-khusus" id="khusus" />
|
|
|
|
|
<Label htmlFor="atensi-khusus">Atensi Khusus</Label>
|
|
|
|
|
<RadioGroupItem value="tugas-harian" id="harian" />
|
|
|
|
|
<Label htmlFor="tugas-harian">Tugas Harian</Label>
|
|
|
|
|
</RadioGroup>
|
|
|
|
|
</div>
|
|
|
|
|
{/* RadioGroup Assignment Category */}
|
|
|
|
|
<div className="mt-5">
|
|
|
|
|
<Label>Jenis Penugasan</Label>
|
|
|
|
|
<RadioGroup
|
|
|
|
|
value={detail.assignmentType.id.toString()} // State yang dipetakan ke value RadioGroup
|
|
|
|
|
onValueChange={(value) => setType(value)} // Mengubah nilai state ketika pilihan berubah
|
|
|
|
|
className="flex flex-wrap gap-3"
|
|
|
|
|
>
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<RadioGroupItem value="1" id="publication" />
|
|
|
|
|
<Label htmlFor="publication">Publikasi</Label>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<RadioGroupItem value="2" id="amplification" />
|
|
|
|
|
<Label htmlFor="amplification">Amplifikasi</Label>
|
2024-12-03 17:49:25 +00:00
|
|
|
</div>
|
2024-12-17 14:27:47 +00:00
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<RadioGroupItem value="3" id="contra" />
|
|
|
|
|
<Label htmlFor="contra">Kontra</Label>
|
|
|
|
|
</div>
|
|
|
|
|
</RadioGroup>
|
2024-12-03 17:49:25 +00:00
|
|
|
</div>
|
2024-12-17 14:27:47 +00:00
|
|
|
<div className="mt-5">
|
|
|
|
|
<Label>Output Tugas</Label>
|
|
|
|
|
<div className="flex flex-wrap gap-3">
|
|
|
|
|
{Object.keys(taskOutput).map((key) => (
|
|
|
|
|
<div className="flex items-center gap-2" key={key}>
|
|
|
|
|
<Checkbox
|
|
|
|
|
id={key}
|
|
|
|
|
checked={taskOutput[key as keyof typeof taskOutput]}
|
|
|
|
|
onCheckedChange={(value) =>
|
|
|
|
|
setTaskOutput({ ...taskOutput, [key]: value })
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
<Label htmlFor={key}>
|
|
|
|
|
{key.charAt(0).toUpperCase() + key.slice(1)}
|
|
|
|
|
</Label>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
2024-12-05 17:02:06 +00:00
|
|
|
</div>
|
2024-12-17 14:27:47 +00:00
|
|
|
</div>
|
|
|
|
|
<div className="mt-5">
|
|
|
|
|
<Label>Broadcast </Label>
|
|
|
|
|
<RadioGroup
|
|
|
|
|
value={broadcastType} // Nilai terpilih diambil dari state broadcastType
|
|
|
|
|
onValueChange={(value) => setBroadcastType(value)} // Mengatur nilai saat radio berubah
|
|
|
|
|
className="flex flex-wrap gap-3"
|
|
|
|
|
>
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<RadioGroupItem value="all" id="all" />
|
|
|
|
|
<Label htmlFor="all">Semua</Label>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<RadioGroupItem value="email" id="email" />
|
|
|
|
|
<Label htmlFor="email">Email Blast</Label>
|
2024-12-03 17:49:25 +00:00
|
|
|
</div>
|
2024-12-17 14:27:47 +00:00
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
<RadioGroupItem value="whatsapp" id="whatsapp" />
|
|
|
|
|
<Label htmlFor="whatsapp">WhatsApp Blast</Label>
|
|
|
|
|
</div>
|
|
|
|
|
</RadioGroup>
|
2024-12-03 17:49:25 +00:00
|
|
|
</div>
|
2024-12-17 14:27:47 +00:00
|
|
|
<div className="mt-5">
|
|
|
|
|
<Label>Narasi Penugasan</Label>
|
|
|
|
|
<Controller
|
|
|
|
|
control={control}
|
|
|
|
|
name="naration"
|
|
|
|
|
render={({ field: { onChange, value } }) => (
|
|
|
|
|
<JoditEditor
|
|
|
|
|
ref={editor}
|
|
|
|
|
value={detail?.narration}
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
className="dark:text-black"
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
{errors.naration?.message && (
|
|
|
|
|
<p className="text-red-400 text-sm">
|
|
|
|
|
{errors.naration.message}
|
|
|
|
|
</p>
|
2024-12-03 17:49:25 +00:00
|
|
|
)}
|
2024-12-17 14:27:47 +00:00
|
|
|
</div>
|
2024-12-03 17:49:25 +00:00
|
|
|
</div>
|
|
|
|
|
|
2024-12-17 14:27:47 +00:00
|
|
|
{/* Submit Button */}
|
|
|
|
|
<div className="mt-4">
|
|
|
|
|
<Button type="submit" color="primary">
|
|
|
|
|
Submit
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
) : (
|
|
|
|
|
""
|
|
|
|
|
)}
|
2024-12-03 17:49:25 +00:00
|
|
|
</div>
|
|
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
}
|