-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace dummy data by real data from API
The dummy data must be replaced by the data from the API for the 'User groups' only. Signed-off-by: Carla Martinez <[email protected]>
- Loading branch information
Showing
6 changed files
with
528 additions
and
439 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// RPC | ||
import React from "react"; | ||
import { BatchRPCResponse, useGetUserGroupsQuery } from "src/services/rpc"; | ||
// Data types | ||
import { UserGroupNew } from "src/utils/datatypes/globalDataTypes"; | ||
// Utils | ||
import { API_VERSION_BACKUP, normalizeString } from "src/utils/utils"; | ||
|
||
type MemberOfData = { | ||
isLoading: boolean; | ||
isFetching: boolean; | ||
refetch: () => void; | ||
userGroupsFullList: UserGroupNew[]; | ||
}; | ||
|
||
const useUserMemberOfData = (): MemberOfData => { | ||
// [API call] User groups | ||
// TODO: Normalize data to prevent array of arrays | ||
const userGroupsQuery = useGetUserGroupsQuery({ | ||
searchValue: "", | ||
sizeLimit: 0, | ||
apiVersion: API_VERSION_BACKUP, | ||
}); | ||
|
||
const [userGroupsFullList, setUserGroupsFullList] = React.useState< | ||
UserGroupNew[] | ||
>([]); | ||
const userGroupsData = userGroupsQuery.data || {}; | ||
const isUserGroupsLoading = userGroupsQuery.isLoading; | ||
|
||
React.useEffect(() => { | ||
if (userGroupsData !== undefined && !userGroupsQuery.isFetching) { | ||
const dataParsed = userGroupsData as BatchRPCResponse; | ||
const count = dataParsed.result.count; | ||
const results = dataParsed.result.results; | ||
|
||
const userGroupsTempList: UserGroupNew[] = []; | ||
|
||
for (let i = 0; i < count; i++) { | ||
userGroupsTempList.push({ | ||
cn: normalizeString(results[i].result.cn), | ||
gidnumber: normalizeString(results[i].result.gidnumber), | ||
description: normalizeString(results[i].result.description), | ||
dn: results[i].result.dn, | ||
}); | ||
} | ||
setUserGroupsFullList(userGroupsTempList); | ||
} | ||
}, [userGroupsData, userGroupsQuery.isFetching]); | ||
|
||
// [API call] Refresh | ||
const refetch = () => { | ||
userGroupsQuery.refetch(); | ||
}; | ||
|
||
// Return data | ||
return { | ||
isFetching: userGroupsQuery.isFetching, | ||
isLoading: isUserGroupsLoading, | ||
refetch, | ||
userGroupsFullList, | ||
} as MemberOfData; | ||
}; | ||
|
||
export { useUserMemberOfData }; |
Oops, something went wrong.