From c35e2e65bdfe90add650cdd0f1bea1cad44e6070 Mon Sep 17 00:00:00 2001 From: amaliadwiy Date: Fri, 29 Aug 2025 15:23:08 +0800 Subject: [PATCH] upd: tambah jabatan Deskripsi: - pisah class modal form tambah jabatan supaya bisa double modal No Issues --- .../position/headerRightPositionList.tsx | 133 +----------------- .../position/modalFormCreatePosition.tsx | 133 ++++++++++++++++++ 2 files changed, 137 insertions(+), 129 deletions(-) create mode 100644 components/position/modalFormCreatePosition.tsx diff --git a/components/position/headerRightPositionList.tsx b/components/position/headerRightPositionList.tsx index 2e20af1..549623f 100644 --- a/components/position/headerRightPositionList.tsx +++ b/components/position/headerRightPositionList.tsx @@ -1,93 +1,19 @@ import Styles from "@/constants/Styles" -import { apiCreatePosition } from "@/lib/api" -import { setUpdatePosition } from "@/lib/positionSlice" -import { useAuthSession } from "@/providers/AuthProvider" import { AntDesign } from "@expo/vector-icons" -import { useEffect, useState } from "react" +import { useState } from "react" import { View } from "react-native" -import Toast from "react-native-toast-message" -import { useDispatch, useSelector } from "react-redux" -import { ButtonForm } from "../buttonForm" +import { useSelector } from "react-redux" import ButtonMenuHeader from "../buttonMenuHeader" import DrawerBottom from "../drawerBottom" -import { InputForm } from "../inputForm" import MenuItemRow from "../menuItemRow" import ModalFilter from "../modalFilter" -import ModalSelect from "../modalSelect" -import SelectForm from "../selectForm" +import ModalFormCreatePosition from "./modalFormCreatePosition" export default function HeaderRightPositionList() { - const dispatch = useDispatch() - const update = useSelector((state: any) => state.positionUpdate) - const { token, decryptToken } = useAuthSession() const entityUser = useSelector((state: any) => state.user) const [isVisible, setVisible] = useState(false) const [isVisibleTambah, setVisibleTambah] = useState(false) const [isFilter, setFilter] = useState(false) - const [isSelect, setSelect] = useState(false) - const [choose, setChoose] = useState({ val: '', label: '' }) - const [disable, setDisable] = useState(true) - const [dataForm, setDataForm] = useState({ - name: "", - idGroup: "", - }) - const [error, setError] = useState({ - name: false, - idGroup: false - }); - - function validationForm(val: any, cat: 'name' | 'idGroup') { - if (cat === 'name') { - setDataForm({ ...dataForm, name: val }) - if (val == "") { - setError({ ...error, name: true }) - } else { - setError({ ...error, name: false }) - } - } else if (cat === "idGroup") { - setDataForm({ ...dataForm, idGroup: val }) - if (val == "") { - setError({ ...error, idGroup: true }) - } else { - setError({ ...error, idGroup: false }) - } - } - } - - function checkAll() { - let nilai = false - if (dataForm.name == "") { - nilai = true - } - - if ((entityUser.role == "supadmin" || entityUser.role == "developer") && (dataForm.idGroup == "" || String(dataForm.idGroup) == "null")) { - nilai = true - } - - setDisable(nilai) - } - - useEffect(() => { - checkAll() - }, [dataForm]) - - async function handleTambah() { - try { - setDisable(true) - const hasil = await decryptToken(String(token?.current)) - const response = await apiCreatePosition({ user: hasil, name: dataForm.name, idGroup: dataForm.idGroup }) - dispatch(setUpdatePosition(!update)) - } catch (error) { - console.error(error) - } finally { - setDisable(false) - setVisibleTambah(false) - setVisible(false) - Toast.show({ type: 'small', text1: 'Berhasil menambahkan data', }) - } - - } - return ( <> @@ -121,64 +47,13 @@ export default function HeaderRightPositionList() { setVisibleTambah(false)} title="Tambah Jabatan"> - - - { - (entityUser.role == 'supadmin' || entityUser.role == 'developer') && - { - setVisibleTambah(false) - setTimeout(() => { - setSelect(true) - }, 600) - }} - error={error.idGroup} - errorText="Lembaga Desa harus diisi" - /> - } - { validationForm(value, 'name') }} - error={error.name} - errorText="Nama jabatan harus diisi" - value={dataForm.name} - /> - - - { handleTambah() }} - disabled={disable} /> - - + setVisibleTambah(false)} /> { setFilter(false) setVisible(false) }} open={isFilter} page="position" /> - - { - validationForm(value.val, 'idGroup') - setChoose(value) - setSelect(false) - setTimeout(() => { - setVisibleTambah(true) - }, 600) - }} - title="Lembaga Desa" - open={isSelect} - /> ) } \ No newline at end of file diff --git a/components/position/modalFormCreatePosition.tsx b/components/position/modalFormCreatePosition.tsx new file mode 100644 index 0000000..256a3f3 --- /dev/null +++ b/components/position/modalFormCreatePosition.tsx @@ -0,0 +1,133 @@ +import Styles from "@/constants/Styles" +import { apiCreatePosition } from "@/lib/api" +import { setUpdatePosition } from "@/lib/positionSlice" +import { useAuthSession } from "@/providers/AuthProvider" +import { update } from "@react-native-firebase/database" +import { useEffect, useState } from "react" +import { View } from "react-native" +import Toast from "react-native-toast-message" +import { useDispatch, useSelector } from "react-redux" +import { ButtonForm } from "../buttonForm" +import { InputForm } from "../inputForm" +import SelectForm from "../selectForm" +import ModalSelect from "../modalSelect" + +export default function ModalFormCreatePosition({ onClose }: { onClose: () => void }) { + const dispatch = useDispatch() + const { token, decryptToken } = useAuthSession() + const entityUser = useSelector((state: any) => state.user) + const [choose, setChoose] = useState({ val: '', label: '' }) + const [isSelect, setSelect] = useState(false) + const [disable, setDisable] = useState(true) + const [dataForm, setDataForm] = useState({ + name: "", + idGroup: "", + }) + const [error, setError] = useState({ + name: false, + idGroup: false + }); + + function validationForm(val: any, cat: 'name' | 'idGroup') { + if (cat === 'name') { + setDataForm({ ...dataForm, name: val }) + if (val == "") { + setError({ ...error, name: true }) + } else { + setError({ ...error, name: false }) + } + } else if (cat === "idGroup") { + setDataForm({ ...dataForm, idGroup: val }) + if (val == "") { + setError({ ...error, idGroup: true }) + } else { + setError({ ...error, idGroup: false }) + } + } + } + + function checkAll() { + let nilai = false + if (dataForm.name == "") { + nilai = true + } + + if ((entityUser.role == "supadmin" || entityUser.role == "developer") && (dataForm.idGroup == "" || String(dataForm.idGroup) == "null")) { + nilai = true + } + + setDisable(nilai) + } + + useEffect(() => { + checkAll() + }, [dataForm]) + + async function handleTambah() { + try { + setDisable(true) + const hasil = await decryptToken(String(token?.current)) + const response = await apiCreatePosition({ user: hasil, name: dataForm.name, idGroup: dataForm.idGroup }) + dispatch(setUpdatePosition(!update)) + } catch (error) { + console.error(error) + } finally { + setDisable(false) + Toast.show({ type: 'small', text1: 'Berhasil menambahkan data', }) + onClose() + } + + } + + return ( + <> + + + { + (entityUser.role == 'supadmin' || entityUser.role == 'developer') && + { + setSelect(true) + }} + error={error.idGroup} + errorText="Lembaga Desa harus diisi" + /> + } + { validationForm(value, 'name') }} + error={error.name} + errorText="Nama jabatan harus diisi" + value={dataForm.name} + /> + + + { handleTambah() }} + disabled={disable} /> + + + + { + validationForm(value.val, 'idGroup') + setChoose(value) + setSelect(false) + }} + title="Lembaga Desa" + open={isSelect} + valChoose={choose.val} + /> + + ) +} \ No newline at end of file