diff --git a/components/form/shared/ask-expert-form.tsx b/components/form/shared/ask-expert-form.tsx index 1583813d..c4797497 100644 --- a/components/form/shared/ask-expert-form.tsx +++ b/components/form/shared/ask-expert-form.tsx @@ -25,6 +25,7 @@ import { createTaskTa, getTask, getUserLevelForAssignments, + getUserLevelForExpert, } from "@/service/task"; import { Dialog, @@ -55,6 +56,7 @@ import TimePicker from "react-time-picker"; import "react-time-picker/dist/TimePicker.css"; import "react-clock/dist/Clock.css"; import { detailMedia } from "@/service/curated-content/curated-content"; +import { getListCompetencies } from "@/service/management-user/management-user"; const taskSchema = z.object({ title: z.string().min(1, { message: "Judul diperlukan" }), @@ -130,12 +132,17 @@ export default function FormAskExpert() { const [detail, setDetail] = useState(); const [refresh] = useState(false); const [listDest, setListDest] = useState([]); - const [checkedLevels, setCheckedLevels] = useState(new Set()); + const [checkedLevels, setCheckedLevels] = useState>(new Set()); const [expandedPolda, setExpandedPolda] = useState([{}]); const [isLoading, setIsLoading] = useState(false); const [audioFile, setAudioFile] = useState(null); const [isRecording, setIsRecording] = useState(false); const [timer, setTimer] = useState(120); + const [userCompetencies, setUserCompetencies] = useState([]); + const [selectedCompetencies, setSelectedCompetencies] = useState>( + new Set() + ); + const [listExpert, setListExpert] = useState([]); const t = useTranslations("Form"); const [imageFiles, setImageFiles] = useState([]); @@ -151,21 +158,6 @@ export default function FormAskExpert() { from: new Date(2024, 0, 1), }); - const [platformTypeVisible, setPlatformTypeVisible] = useState(false); - const [unitSelection, setUnitSelection] = useState({ - semua: false, - mabes: false, - polda: false, - polres: false, - satker: false, - }); - const [taskOutput, setTaskOutput] = useState({ - all: false, - video: false, - audio: false, - image: false, - text: false, - }); const [links, setLinks] = useState([""]); const { @@ -179,37 +171,68 @@ export default function FormAskExpert() { mode: "all", }); - // const handleRadioChange = (event: React.ChangeEvent) => { - // const selectedValue = Number(event.target.value); - // setMainType(selectedValue); + useEffect(() => { + getDataAdditional(); + }, []); - // setPlatformTypeVisible(selectedValue === 2); + async function getDataAdditional() { + const resCompetencies = await getListCompetencies(); + console.log("competency", resCompetencies); + setUserCompetencies(resCompetencies?.data?.data); + } useEffect(() => { - async function fetchPoldaPolres() { + async function fetchListExpert() { setIsLoading(true); try { - const response = await getUserLevelForAssignments(); - setListDest(response?.data?.data.list); - console.log("polda", response?.data?.data?.list); - const initialExpandedState = response?.data?.data.list.reduce( - (acc: any, polda: any) => { - acc[polda.id] = false; - return acc; - }, - {} - ); - setExpandedPolda(initialExpandedState); - console.log("polres", initialExpandedState); + const response = await getUserLevelForExpert(id); + setListExpert(response?.data?.data); + console.log("tenaga ahli", response?.data?.data); } catch (error) { console.error("Error fetching Polda/Polres data:", error); } finally { setIsLoading(false); } } - fetchPoldaPolres(); + fetchListExpert(); }, []); + useEffect(() => { + async function initState() { + if (id) { + const response = await detailMedia(id); + const details = response?.data?.data; + + setDetail(details); + } + } + initState(); + }, [id, refresh]); + + useEffect(() => { + const fetchExpertsForCompetencies = async () => { + const allExperts: any[] = []; + + for (const compId of Array.from(selectedCompetencies)) { + const response = await getUserLevelForExpert(compId); + const experts = response?.data?.data || []; + allExperts.push(...experts); + } + + const uniqueExperts = Array.from( + new Map(allExperts.map((e) => [e.id, e])).values() + ); + + setListExpert(uniqueExperts); + }; + + if (selectedCompetencies.size > 0) { + fetchExpertsForCompetencies(); + } else { + setListExpert([]); + } + }, [selectedCompetencies]); + // }; const handleCheckboxChange = (levelId: number) => { setCheckedLevels((prev) => { @@ -223,217 +246,39 @@ export default function FormAskExpert() { }); }; - const handlePoldaPolresChange = () => { - return Array.from(checkedLevels).join(","); // Mengonversi Set ke string + const handleExpertChange = () => { + return Array.from(checkedLevels).join(","); }; - const handleUnitChange = ( - key: keyof typeof unitSelection, - value: boolean - ) => { - if (key === "semua") { - const newState = { - semua: value, - mabes: value, - polda: value, - polres: value, - satker: value, - }; - setUnitSelection(newState); - } else { - const updatedSelection = { - ...unitSelection, - [key]: value, - }; - - const allChecked = ["mabes", "polda", "polres", "satker"].every( - (k) => updatedSelection[k as keyof typeof unitSelection] - ); - - updatedSelection.semua = allChecked; - - setUnitSelection(updatedSelection); - } - }; - - const handleExpertiseOutputChange = ( - key: keyof typeof expertise, - value: boolean - ) => { - if (key === "semua") { - const newState = { - semua: value, - komunikasi: value, - hukum: value, - bahasa: value, - ekonomi: value, - politik: value, - sosiologi: value, - ilmuadministrasipemerintah: value, - ti: value, - }; - setExpertiseOutput(newState); - } else { - const updated = { - ...expertise, - [key]: value, - }; - - const allChecked = [ - "komunikasi", - "hukum", - "bahasa", - "ekonomi", - "politik", - "sosiologi", - "ilmuadministrasipemerintah", - "ti", - ].every((k) => updated[k as keyof typeof expertise]); - - updated.semua = allChecked; - setExpertiseOutput(updated); - } - }; - - const handleExpertOutputChange = ( - key: keyof typeof expert, - value: boolean - ) => { - if (key === "semua") { - const newState = { - semua: value, - }; - setExpertOutput(newState); - } else { - const updated = { - ...expert, - [key]: value, - }; - - const allChecked = ["video", "audio", "image", "text"].every( - (k) => updated[k as keyof typeof expert] - ); - - updated.semua = allChecked; - setExpertOutput(updated); - } - }; - - useEffect(() => { - async function initState() { - if (id) { - const response = await detailMedia(id); - const details = response?.data?.data; - - setDetail(details); + const handleCompetencyChange = async (competencyId: number) => { + setSelectedCompetencies((prev) => { + const updated = new Set(prev); + if (updated.has(competencyId)) { + updated.delete(competencyId); + } else { + updated.add(competencyId); } - } - initState(); - }, [id, refresh]); - - const handleTaskOutputChange = ( - key: keyof typeof taskOutput, - value: boolean - ) => { - if (key === "all") { - const newState = { - all: value, - video: value, - audio: value, - image: value, - text: value, - }; - setTaskOutput(newState); - } else { - const updated = { - ...taskOutput, - [key]: value, - }; - - const allChecked = ["video", "audio", "image", "text"].every( - (k) => updated[k as keyof typeof taskOutput] - ); - - updated.all = allChecked; - setTaskOutput(updated); - } + return updated; + }); }; const save = async (data: TaskSchema) => { - const fileTypeMapping = { - all: "1", - video: "2", - audio: "4", - image: "3", - text: "5", - }; - - const areasMapping = { - semua: "0", - komunikasi: "1", - hukum: "2", - bahasa: "3", - ekonomi: "4", - politik: "5", - sosiologi: "6", - ilmuadministrasipemerintah: "7", - ti: "8", - }; - - const unitMapping = { - allUnit: "0", - mabes: "1", - polda: "2", - polres: "3", - satker: "4", - }; - const assignmentPurposeString = Object.keys(unitSelection) - .filter((key) => unitSelection[key as keyof typeof unitSelection]) - .map((key) => unitMapping[key as keyof typeof unitMapping]) - .join(","); - - const selectedOutputs = Object.keys(taskOutput) - .filter((key) => taskOutput[key as keyof typeof taskOutput]) - .map((key) => fileTypeMapping[key as keyof typeof fileTypeMapping]) - .join(","); - - const selectedAreaExpert = Object.keys(expertise) - .filter((key) => expertise[key as keyof typeof expertise]) - .map((key) => areasMapping[key as keyof typeof areasMapping]) - .join(","); - const requestData: { id?: number; title: string; - assignedToLevel: any; assignedToUsers: any; assignmentTypeId: string; - fileTypeOutput: string; - areasExpertise: string; narration: string; - platformType: string | null; - assignmentMainTypeId: any; assignmentType: string; - assignedToRole: string; - broadcastType: string; expertCompetencies: string; attachmentUrl: string[]; } = { ...data, - // assignmentType, - // assignmentCategory, - assignedToLevel: handlePoldaPolresChange(), - assignedToUsers: assignmentPurposeString, - assignedToRole: selectedTarget, + assignedToUsers: handleExpertChange(), assignmentType: taskType, - broadcastType: broadcastType, - assignmentMainTypeId: mainType, - areasExpertise: selectedAreaExpert, assignmentTypeId: type, - fileTypeOutput: selectedOutputs, narration: data.naration, - platformType: "", - expertCompetencies: "1,2,3", + expertCompetencies: Array.from(selectedCompetencies).join(","), title: data.title, attachmentUrl: links, }; @@ -676,7 +521,6 @@ export default function FormAskExpert() { {detail !== undefined ? (
- {/* Input Title */}
{errors.title.message}

)}
-
- -
-
- -
- {Object.keys(expertise).map((key) => ( -
- - handleExpertiseOutputChange( - key as keyof typeof expertise, - value as boolean - ) - } - /> - -
- ))} -
-
-
- -
- {Object.keys(expert).map((key) => ( -
- - handleExpertOutputChange( - key as keyof typeof expert, - value as boolean - ) - } - /> - -
- ))} -
-
@@ -784,27 +581,54 @@ export default function FormAskExpert() {
- +
- {Object.keys(taskOutput).map((key) => ( -
+ {userCompetencies?.map((item: any) => ( +
- handleTaskOutputChange( - key as keyof typeof taskOutput, - value as boolean - ) - } + id={`comp-${item.id}`} + checked={selectedCompetencies.has(item.id)} + onCheckedChange={() => handleCompetencyChange(item.id)} /> - +
))}
+
+ +
+ + + + + + + Daftar Tenaga Ahli + +
+ {listExpert?.map((expert: any) => ( +
+ +
+ ))} +
+
+
+
+
+
-
- -
- {Object.keys(unitSelection).map((key) => ( -
- - handleUnitChange( - key as keyof typeof unitSelection, - value as boolean - ) - } - /> - -
- ))} -
- - - - - - - - Daftar Wilayah Polda dan Polres - - -
- {listDest.map((polda: any) => ( -
- - {expandedPolda[polda.id] && ( -
- - {polda?.subDestination?.map((polres: any) => ( - - ))} -
- )} -
- ))} -
-
-
-
-
-
+