Skip to content

Commit

Permalink
Add graphql query for analytical framework roles
Browse files Browse the repository at this point in the history
  • Loading branch information
shreeyash07 committed Apr 16, 2024
1 parent c8f84be commit 5d3596f
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions app/views/AnalyticalFramework/UserTable/AddUserModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,22 @@ import {
Button,
PendingMessage,
} from '@the-deep/deep-ui';
import { gql, useQuery } from '@apollo/client';

import { AnalysisFrameworkRolesQuery } from '#generated/types';
import NonFieldError from '#components/NonFieldError';
import { useRequest, useLazyRequest } from '#base/utils/restRequest';
import { MultiResponse, Membership } from '#types';
import { useLazyRequest } from '#base/utils/restRequest';
import { Membership } from '#types';
import NewUserSelectInput, { User } from '#components/selections/NewUserSelectInput';
import _ts from '#ts';

import styles from './styles.css';

type Member = Pick<Membership, 'id' | 'member' | 'memberName' | 'role'>;
interface Role {
id: number;
title: string;
}
type AnalysisFrameworkRolesType = NonNullable<NonNullable<AnalysisFrameworkRolesQuery>['analysisFrameworkRoles']>[number];

const roleKeySelector = (d: Role) => d.id;
const roleLabelSelector = (d: Role) => d.title;
const roleKeySelector = (d: AnalysisFrameworkRolesType) => Number(d.id);
const roleLabelSelector = (d: AnalysisFrameworkRolesType) => d.title;

type FormType = {
id?: number;
Expand Down Expand Up @@ -59,6 +58,18 @@ const schema: FormSchema = {

const defaultFormValue: PartialForm<FormType> = {};

const ANALYSIS_FRAMEWORK_ROLES = gql`
query AnalysisFrameworkRoles {
analysisFrameworkRoles {
id
type
title
isPrivateRole
isDefaultRole
}
}
`;

interface ValueToSend {
role: number;
member?: number;
Expand Down Expand Up @@ -109,21 +120,19 @@ function AddUserModal(props: Props) {

const error = getErrorObject(riskyError);

const queryForRoles = useMemo(
() => (isPrivateFramework ? ({ is_default_role: false }) : undefined),
[isPrivateFramework],
const {
data: analysisFrameworkRolesResponse,
loading: analysisFrameworkRolesLoading,
} = useQuery<AnalysisFrameworkRolesQuery>(
ANALYSIS_FRAMEWORK_ROLES,
);

const {
pending: pendingRoles,
response: frameworkRolesResponse,
} = useRequest<MultiResponse<Role>>({
url: isPrivateFramework
? 'server://private-framework-roles/'
: 'server://public-framework-roles/',
method: 'GET',
query: queryForRoles,
});
const analysisFrameworkRoles = useMemo(() => (
analysisFrameworkRolesResponse
?.analysisFrameworkRoles?.filter(
(role) => role.isPrivateRole === isPrivateFramework,
)
), [analysisFrameworkRolesResponse, isPrivateFramework]);

const {
pending: pendingAddAction,
Expand Down Expand Up @@ -182,7 +191,7 @@ function AddUserModal(props: Props) {
] : []
), [userValue]);

const pendingRequests = pendingRoles;
const pendingRequests = analysisFrameworkRolesLoading;

return (
<Modal
Expand Down Expand Up @@ -225,7 +234,7 @@ function AddUserModal(props: Props) {
/>
<SelectInput
name="role"
options={frameworkRolesResponse?.results}
options={analysisFrameworkRoles}
keySelector={roleKeySelector}
labelSelector={roleLabelSelector}
onChange={setFieldValue}
Expand Down

0 comments on commit 5d3596f

Please sign in to comment.