353 lines
12 KiB
TypeScript
353 lines
12 KiB
TypeScript
"use client";
|
|
import React, { useEffect, useRef, useState } from "react";
|
|
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";
|
|
import { useParams, useRouter } from "next/navigation";
|
|
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";
|
|
import { createTask } from "@/service/task";
|
|
import { cn } from "@/lib/utils";
|
|
import { format } from "date-fns";
|
|
import { CalendarIcon } from "lucide-react";
|
|
import { id } from "date-fns/locale";
|
|
import { getPlanningById } from "@/service/planning/planning";
|
|
|
|
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.",
|
|
}),
|
|
});
|
|
|
|
export type mediahubDetail = {
|
|
id: number;
|
|
title: string;
|
|
fileTypeOutput: string;
|
|
assignedToTopLevel: string;
|
|
assignmentType: {
|
|
id: number;
|
|
name: string;
|
|
};
|
|
date: string;
|
|
description: string;
|
|
is_active: string;
|
|
};
|
|
|
|
export default function PublishMediahub() {
|
|
const MySwal = withReactContent(Swal);
|
|
const router = useRouter();
|
|
const editor = useRef(null);
|
|
type TaskSchema = z.infer<typeof taskSchema>;
|
|
const { id } = useParams() as { id: string };
|
|
console.log(id);
|
|
const [taskOutput, setTaskOutput] = useState({
|
|
all: false,
|
|
video: false,
|
|
audio: false,
|
|
image: false,
|
|
text: false,
|
|
});
|
|
const [mainType, setMainType] = useState<number>(1);
|
|
const [taskType, setTaskType] = useState<string>("atensi-khusus");
|
|
const [broadcastType, setBroadcastType] = useState<string>("all"); // untuk Tipe Penugasan
|
|
const [type, setType] = useState<string>("1");
|
|
const [selectedTarget, setSelectedTarget] = useState("all");
|
|
const [startDate, setStartDate] = useState<Date>(new Date());
|
|
const [detail, setDetail] = useState<mediahubDetail>();
|
|
const [refresh] = useState(false);
|
|
|
|
const [platformTypeVisible, setPlatformTypeVisible] = useState(false);
|
|
const [unitSelection, setUnitSelection] = useState({
|
|
allUnit: false,
|
|
mabes: false,
|
|
polda: false,
|
|
polres: false,
|
|
});
|
|
|
|
const {
|
|
control,
|
|
handleSubmit,
|
|
formState: { errors },
|
|
} = useForm<TaskSchema>({
|
|
resolver: zodResolver(taskSchema),
|
|
});
|
|
|
|
const handleRadioChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
const selectedValue = Number(event.target.value);
|
|
setMainType(selectedValue);
|
|
|
|
setPlatformTypeVisible(selectedValue === 2);
|
|
};
|
|
|
|
useEffect(() => {
|
|
async function initState() {
|
|
if (id) {
|
|
const response = await getPlanningById(id);
|
|
const details = response.data?.data;
|
|
|
|
setDetail(details);
|
|
if (details?.date) {
|
|
setStartDate(new Date(details.date)); // Konversi string tanggal ke objek Date
|
|
}
|
|
}
|
|
}
|
|
initState();
|
|
}, [id, refresh]);
|
|
|
|
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)
|
|
); // Membagi string ke dalam array dan mengonversi ke nomor
|
|
setUnitSelection({
|
|
allUnit: outputSet.has(0),
|
|
mabes: outputSet.has(1),
|
|
polda: outputSet.has(2),
|
|
polres: outputSet.has(3),
|
|
});
|
|
}
|
|
}, [detail?.fileTypeOutput]);
|
|
|
|
const save = async (data: TaskSchema) => {
|
|
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(",");
|
|
|
|
const requestData = {
|
|
...data,
|
|
// assignmentType,
|
|
// assignmentCategory,
|
|
target: selectedTarget,
|
|
unitSelection,
|
|
assignedToRole: "3",
|
|
taskType: taskType,
|
|
broadcastType: broadcastType,
|
|
assignmentMainTypeId: mainType,
|
|
assignmentPurpose: "1",
|
|
assignmentTypeId: type,
|
|
fileTypeOutput: selectedOutputs,
|
|
id: null,
|
|
narration: data.naration,
|
|
platformType: "",
|
|
title: data.title,
|
|
};
|
|
|
|
const response = await createTask(requestData);
|
|
|
|
console.log("Form Data Submitted:", requestData);
|
|
console.log("response", response);
|
|
|
|
MySwal.fire({
|
|
title: "Sukses",
|
|
text: "Data berhasil disimpan.",
|
|
icon: "success",
|
|
confirmButtonColor: "#3085d6",
|
|
confirmButtonText: "OK",
|
|
}).then(() => {
|
|
router.push("/en/contributor/task");
|
|
});
|
|
};
|
|
|
|
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">Perencanaan Mediahub</p>
|
|
{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>
|
|
)}
|
|
</div>
|
|
<div className="mt-5">
|
|
<Label>Output Tugas</Label>
|
|
<div className="flex flex-wrap gap-3 mt-1">
|
|
{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>
|
|
))}
|
|
</div>
|
|
</div>
|
|
<div className="flex flex-row items-center">
|
|
<div className="mt-5">
|
|
<Label>Pelaksana Tugas</Label>
|
|
<div className="flex flex-wrap mt-1 gap-2">
|
|
{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>
|
|
</div>
|
|
<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>
|
|
</div>
|
|
<div className="flex items-center gap-2">
|
|
<RadioGroupItem value="3" id="contra" />
|
|
<Label htmlFor="contra">Kontra</Label>
|
|
</div>
|
|
</RadioGroup>
|
|
</div>
|
|
<div className="mt-5">
|
|
<div className="flex flex-col">
|
|
<Label>Date</Label>
|
|
<div>
|
|
<Button
|
|
value={detail.date}
|
|
variant="outline"
|
|
size="md"
|
|
className={cn(
|
|
" justify-between text-left font-normal border-default-200 text-default-600 md:px-4 w-3/12",
|
|
!startDate && "text-muted-foreground"
|
|
)}
|
|
>
|
|
{startDate ? (
|
|
format(startDate, "PP")
|
|
) : (
|
|
<span>Pick a date</span>
|
|
)}
|
|
<CalendarIcon className="h-4 w-4" />
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div className="mt-5">
|
|
<Label>Narasi Penugasan</Label>
|
|
<Controller
|
|
control={control}
|
|
name="naration"
|
|
render={({ field: { onChange, value } }) => (
|
|
<JoditEditor
|
|
ref={editor}
|
|
value={detail?.description}
|
|
onChange={onChange}
|
|
className="dark:text-black"
|
|
/>
|
|
)}
|
|
/>
|
|
{errors.naration?.message && (
|
|
<p className="text-red-400 text-sm">
|
|
{errors.naration.message}
|
|
</p>
|
|
)}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Submit Button */}
|
|
<div className="mt-4">
|
|
<Button type="submit" color="primary">
|
|
Submit
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
) : (
|
|
""
|
|
)}
|
|
</div>
|
|
</Card>
|
|
);
|
|
}
|