upd: dokumen divisi
Deskripsi: - move dokumen divisi - copy dokumen divisi - share dokumen divisi NO issues
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import Styles from "@/constants/Styles"
|
||||
import { apiGetDivisionGroup } from "@/lib/api"
|
||||
import { apiGetDivisionGroup, apiGetDocumentInformasi, apiGetListDivisionByIdDivision } from "@/lib/api"
|
||||
import { useAuthSession } from "@/providers/AuthProvider"
|
||||
import { AntDesign } from "@expo/vector-icons"
|
||||
import { useEffect, useState } from "react"
|
||||
@@ -15,6 +15,7 @@ type Props = {
|
||||
choose: string
|
||||
onSelect: (value: any[]) => void
|
||||
value?: any
|
||||
item?: any
|
||||
}
|
||||
|
||||
type CheckedState = {
|
||||
@@ -30,11 +31,12 @@ type GroupData = {
|
||||
}[];
|
||||
}
|
||||
|
||||
export default function ModalSelectMultiple({ open, close, title, category, choose, onSelect, value }: Props) {
|
||||
export default function ModalSelectMultiple({ open, close, title, category, choose, onSelect, value, item }: Props) {
|
||||
const [isChoose, setChoose] = useState(choose)
|
||||
const { token, decryptToken } = useAuthSession()
|
||||
const [data, setData] = useState<any>([])
|
||||
const [checked, setChecked] = useState<CheckedState>({});
|
||||
const [selectedDivision, setSelectedDivision] = useState<any>([]);
|
||||
|
||||
async function handleLoadChooseDivision() {
|
||||
try {
|
||||
@@ -54,12 +56,27 @@ export default function ModalSelectMultiple({ open, close, title, category, choo
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (category == 'choose-division') {
|
||||
handleLoadChooseDivision()
|
||||
} else if (category == 'share-division') {
|
||||
// handleLoadPosition()
|
||||
async function handleLoadShareDivision() {
|
||||
try {
|
||||
const hasil = await decryptToken(String(token?.current))
|
||||
const response = await apiGetListDivisionByIdDivision({ user: hasil, search: '', division: value })
|
||||
const response2 = await apiGetDocumentInformasi({ user: hasil, item: item, cat: 'share' })
|
||||
setData(response.data.filter((i: any) => i.id != value))
|
||||
setSelectedDivision(response2.data)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (open) {
|
||||
if (category == 'choose-division') {
|
||||
handleLoadChooseDivision()
|
||||
} else if (category == 'share-division') {
|
||||
handleLoadShareDivision()
|
||||
}
|
||||
}
|
||||
|
||||
}, [open]);
|
||||
|
||||
const handleCheck = (groupId: string, divisionId: string) => {
|
||||
@@ -93,19 +110,38 @@ export default function ModalSelectMultiple({ open, close, title, category, choo
|
||||
setChecked(newChecked);
|
||||
};
|
||||
|
||||
const handleDivisionClick = (index: number) => {
|
||||
if (selectedDivision.some((i: any) => i.id == data[index].id)) {
|
||||
setSelectedDivision(
|
||||
selectedDivision.filter((i: any) => i.id != data[index].id)
|
||||
);
|
||||
} else {
|
||||
setSelectedDivision([
|
||||
...selectedDivision,
|
||||
{ id: data[index].id, name: data[index].name },
|
||||
]);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
const handleSubmit = () => {
|
||||
const selectedGroups: GroupData[] = [];
|
||||
Object.keys(checked).forEach((groupId) => {
|
||||
const group = data.find((item: { id: string }) => item.id === groupId);
|
||||
if (group) {
|
||||
selectedGroups.push({
|
||||
id: group.id,
|
||||
name: group.name,
|
||||
Division: group.Division.filter((division: { id: string }) => checked[groupId].includes(division.id)),
|
||||
});
|
||||
}
|
||||
});
|
||||
onSelect(selectedGroups);
|
||||
if (category == "choose-division") {
|
||||
const selectedGroups: GroupData[] = [];
|
||||
Object.keys(checked).forEach((groupId) => {
|
||||
const group = data.find((item: { id: string }) => item.id === groupId);
|
||||
if (group) {
|
||||
selectedGroups.push({
|
||||
id: group.id,
|
||||
name: group.name,
|
||||
Division: group.Division.filter((division: { id: string }) => checked[groupId].includes(division.id)),
|
||||
});
|
||||
}
|
||||
});
|
||||
onSelect(selectedGroups);
|
||||
} else {
|
||||
onSelect(selectedDivision);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
return (
|
||||
@@ -114,22 +150,22 @@ export default function ModalSelectMultiple({ open, close, title, category, choo
|
||||
{
|
||||
category == 'share-division' ?
|
||||
<>
|
||||
<Pressable style={[Styles.itemSelectModal]} onPress={() => {
|
||||
setChoose('dinas')
|
||||
close(false)
|
||||
}}>
|
||||
<Text style={[Styles.textDefaultSemiBold]}>Sosial Kemasyarakatan</Text>
|
||||
<AntDesign name="check" size={20} />
|
||||
</Pressable>
|
||||
{/* <Pressable style={[Styles.itemSelectModal]}>
|
||||
<Text>Kaur Pemerintahan</Text>
|
||||
</Pressable>
|
||||
<Pressable style={[Styles.itemSelectModal]}>
|
||||
<Text>Kasi Kemasyarakatan</Text>
|
||||
</Pressable>
|
||||
<Pressable style={[Styles.itemSelectModal]}>
|
||||
<Text>PKK</Text>
|
||||
</Pressable> */}
|
||||
{
|
||||
data.map((item: any, index: number) => {
|
||||
return (
|
||||
<Pressable key={index} style={[Styles.itemSelectModal]} onPress={() => {
|
||||
handleDivisionClick(index)
|
||||
}}>
|
||||
<Text numberOfLines={1} style={[Styles.w80]}>{item.name}</Text>
|
||||
{
|
||||
selectedDivision.some((i: any) => i.id == item.id)
|
||||
? <AntDesign name="check" size={18} />
|
||||
: <></>
|
||||
}
|
||||
</Pressable>
|
||||
)
|
||||
})
|
||||
}
|
||||
</>
|
||||
:
|
||||
data.map((item: any, index: number) => {
|
||||
|
||||
Reference in New Issue
Block a user