-
Notifications
You must be signed in to change notification settings - Fork 515
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: vinu tv <[email protected]>
- Loading branch information
1 parent
4a5fdff
commit 1219f29
Showing
17 changed files
with
442 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { useEffect, useMemo, useState } from "react"; | ||
|
||
import { FilterState } from "@/hooks/useFilters"; | ||
|
||
import query from "@/Utils/request/query"; | ||
import { Organization } from "@/types/organization/organization"; | ||
import organizationApi from "@/types/organization/organizationApi"; | ||
|
||
interface UseOrganizationLevelProps { | ||
index: number; | ||
skip: boolean; | ||
selectedLevels: Organization[]; | ||
setOrgTypes: React.Dispatch<React.SetStateAction<string[]>>; | ||
onChange: (Filter: FilterState, index?: number) => void; | ||
getParentId: (index: number) => string; | ||
} | ||
|
||
export function useOrganizationLevel({ | ||
index, | ||
skip, | ||
selectedLevels, | ||
setOrgTypes, | ||
onChange, | ||
getParentId, | ||
}: UseOrganizationLevelProps) { | ||
const [levelSearch, setLevelSearch] = useState(""); | ||
|
||
const { data: availableOrgs } = useQuery<{ results: Organization[] }>({ | ||
queryKey: ["organizations-available", getParentId(index), levelSearch], | ||
queryFn: query.debounced(organizationApi.getPublicOrganizations, { | ||
queryParams: { | ||
...(index > 0 && { parent: getParentId(index) }), | ||
...(index === 0 && { level_cache: 1 }), | ||
name: levelSearch || undefined, | ||
}, | ||
}), | ||
enabled: | ||
!skip && | ||
index <= selectedLevels.length && | ||
(index === 0 || selectedLevels[index - 1] !== undefined), | ||
}); | ||
|
||
// Update org types when we get new data for this level | ||
useEffect(() => { | ||
if (selectedLevels[index]) { | ||
const currentOrg = selectedLevels[index]; | ||
if (currentOrg?.metadata?.govt_org_children_type) { | ||
setOrgTypes((prevTypes) => { | ||
const newTypes = [...prevTypes]; | ||
// Update next level type | ||
if (currentOrg.metadata?.govt_org_children_type) { | ||
if (index === newTypes.length) { | ||
newTypes.push(currentOrg.metadata.govt_org_children_type); | ||
} else { | ||
newTypes[index + 1] = currentOrg.metadata.govt_org_children_type; | ||
} | ||
} | ||
return newTypes; | ||
}); | ||
} | ||
} | ||
}, [selectedLevels, setOrgTypes, index]); | ||
|
||
const options = useMemo(() => { | ||
return ( | ||
availableOrgs?.results?.map((org) => ({ | ||
label: org.name, | ||
value: org.id, | ||
})) || [] | ||
); | ||
}, [availableOrgs?.results]); | ||
|
||
const handleChange = (value: string) => { | ||
const selectedOrg = availableOrgs?.results.find((org) => org.id === value); | ||
if (selectedOrg) { | ||
onChange({ organization: selectedOrg.id }, index); | ||
setLevelSearch(""); | ||
} | ||
}; | ||
|
||
const handleSearch = (value: string) => setLevelSearch(value); | ||
|
||
return { | ||
options, | ||
levelSearch, | ||
handleChange, | ||
handleSearch, | ||
availableOrgs, | ||
}; | ||
} |
Oops, something went wrong.