Skip to content

Commit

Permalink
Replace dummy data by real data from API
Browse files Browse the repository at this point in the history
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
carma12 committed Feb 7, 2024
1 parent 1550b9c commit b944a7d
Show file tree
Hide file tree
Showing 6 changed files with 528 additions and 439 deletions.
65 changes: 65 additions & 0 deletions src/hooks/useUserMemberOfData.tsx
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 };
Loading

0 comments on commit b944a7d

Please sign in to comment.