Skip to content

Commit

Permalink
Merge pull request #784 from Arnei/typing-groupdetails
Browse files Browse the repository at this point in the history
Add typing to group details
  • Loading branch information
Arnei authored Jul 5, 2024
2 parents 8c2e491 + 60357f3 commit 9f829e5
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 27 deletions.
8 changes: 3 additions & 5 deletions src/components/users/partials/modal/GroupDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { EditGroupSchema } from "../../../../utils/validate";
import { getGroupDetails } from "../../../../selectors/groupDetailsSelectors";
import ModalNavigation from "../../../shared/modals/ModalNavigation";
import { useAppDispatch, useAppSelector } from "../../../../store";
import { updateGroupDetails } from "../../../../slices/groupDetailsSlice";
import { UpdateGroupDetailsState, updateGroupDetails } from "../../../../slices/groupDetailsSlice";

/**
* This component manages the pages of the group details
Expand Down Expand Up @@ -60,13 +60,11 @@ const GroupDetails: React.FC<{
},
];

// @ts-expect-error TS(7006): Parameter 'tabNr' implicitly has an 'any' type.
const openTab = (tabNr) => {
const openTab = (tabNr: number) => {
setPage(tabNr);
};

// @ts-expect-error TS(7006): Parameter 'values' implicitly has an 'any' type.
const handleSubmit = (values) => {
const handleSubmit = (values: UpdateGroupDetailsState) => {
dispatch(updateGroupDetails({values: values, groupId: groupDetails.id}));
close();
};
Expand Down
9 changes: 6 additions & 3 deletions src/components/users/partials/modal/GroupDetailsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ import { availableHotkeys } from "../../../../configs/hotkeysConfig";
* This component renders the modal for displaying group details
*/
const GroupDetailsModal = ({
close,
groupName
}: any) => {
close,
groupName
}: {
close: () => void,
groupName: string,
}) => {
const { t } = useTranslation();

useHotkeys(
Expand Down
3 changes: 1 addition & 2 deletions src/components/users/partials/wizard/GroupUsersPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const GroupUsersPage = <T,>({
isEdit?: boolean,
}) => {
// users that can be chosen by user
const [users, setUsers] = useState([]);
const [users, setUsers] = useState<{ id: string, name: string }[]>([]);
// flag for API call
const [loading, setLoading] = useState(false);

Expand All @@ -37,7 +37,6 @@ const GroupUsersPage = <T,>({
});
}

// @ts-expect-error TS(2345): Argument of type '{ id: string; name: any; }[]' is... Remove this comment to see the full error message
setUsers(userNames);
setLoading(false);
}
Expand Down
10 changes: 2 additions & 8 deletions src/components/users/partials/wizard/NewGroupSummaryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@ import WizardNavigationButtons from "../../../shared/wizard/WizardNavigationButt
import { useTranslation } from "react-i18next";
import Notifications from "../../../shared/Notifications";
import { FormikProps } from "formik";
import { initialFormValuesNewGroup } from "../../../../configs/modalConfig";

/**
* This component renders the summary page for new groups in the new group wizard.
*/
interface RequiredFormProps {
name: string,
description: string,
roles: string[],
users: string[],
}

const NewGroupSummaryPage = <T extends RequiredFormProps>({
const NewGroupSummaryPage = <T extends typeof initialFormValuesNewGroup>({
formik,
previousPage,
}: {
Expand Down
7 changes: 6 additions & 1 deletion src/configs/modalConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,12 @@ export const initialFormValuesNewAcl: {

// All fields for new group form that are fix and not depending on response of backend
// InitialValues of Formik form (others computed dynamically depending on responses from backend)
export const initialFormValuesNewGroup = {
export const initialFormValuesNewGroup: {
name: string,
description: string,
roles: { name: string }[],
users: { id: string, name: string }[],
} = {
name: "",
description: "",
roles: [],
Expand Down
11 changes: 7 additions & 4 deletions src/slices/groupDetailsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { createAppAsyncThunk } from '../createAsyncThunkWithTypes';
/**
* This file contains redux reducer for actions affecting the state of details of a group
*/
type GroupDetailsState = {
export type GroupDetailsState = {
status: 'uninitialized' | 'loading' | 'succeeded' | 'failed',
error: SerializedError | null,
role: string,
Expand All @@ -18,6 +18,10 @@ type GroupDetailsState = {
users: {id: string, name: string}[],
}

export interface UpdateGroupDetailsState extends Omit<GroupDetailsState, 'roles'> {
roles: { name: string}[]
}

// initial redux state
const initialState: GroupDetailsState = {
status: 'uninitialized',
Expand All @@ -37,8 +41,7 @@ export const fetchGroupDetails = createAppAsyncThunk('groupDetails/fetchGroupDet

let users: GroupDetailsState["users"] = [];
if (response.users.length > 0) {
// @ts-expect-error TS(7006): Parameter 'user' implicitly has an 'any' type.
users = response.users.map((user) => {
users = response.users.map((user: { username: string, name: string }) => {
return {
id: user.username,
name: user.name,
Expand All @@ -60,7 +63,7 @@ export const fetchGroupDetails = createAppAsyncThunk('groupDetails/fetchGroupDet

// update details of a certain group
export const updateGroupDetails = createAppAsyncThunk('groupDetails/updateGroupDetails', async (params: {
values: GroupDetailsState,
values: UpdateGroupDetailsState,
groupId: string
}, {dispatch}) => {
const { values, groupId } = params
Expand Down
5 changes: 3 additions & 2 deletions src/slices/groupSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import { buildGroupBody, getURLParams } from '../utils/resourceUtils';
import { addNotification } from './notificationSlice';
import { TableConfig } from '../configs/tableConfigs/aclsTableConfig';
import { createAppAsyncThunk } from '../createAsyncThunkWithTypes';
import { initialFormValuesNewGroup } from '../configs/modalConfig';

/**
* This file contains redux reducer for actions affecting the state of groups
*/
type Group = {
export type Group = {
description: string,
id: string,
name: string,
Expand Down Expand Up @@ -58,7 +59,7 @@ export const fetchGroups = createAppAsyncThunk('groups/fetchGroups', async (_, {
});

// post new group to backend
export const postNewGroup = createAppAsyncThunk('groups/postNewGroup', async (values: Group, {dispatch}) => {
export const postNewGroup = createAppAsyncThunk('groups/postNewGroup', async (values: typeof initialFormValuesNewGroup, {dispatch}) => {
// get URL params used for post request
let data = buildGroupBody(values);

Expand Down
6 changes: 4 additions & 2 deletions src/utils/resourceUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { UserInfoState } from "../slices/userInfoSlice";
import { hasAccess, isJson } from "./utils";
import { RootState } from "../store";
import { MetadataCatalog } from "../slices/eventSlice";
import { initialFormValuesNewGroup } from '../configs/modalConfig';
import { UpdateUser } from '../slices/userDetailsSlice';

/**
Expand Down Expand Up @@ -87,8 +88,9 @@ export const buildUserBody = (values: NewUser | UpdateUser) => {
};

// used for create URLSearchParams for API requests used to create/update group
// @ts-expect-error TS(7006): Parameter 'values' implicitly has an 'any' type.
export const buildGroupBody = (values) => {
export const buildGroupBody = (
values: typeof initialFormValuesNewGroup
) => {
let roles = [],
users = [];

Expand Down

0 comments on commit 9f829e5

Please sign in to comment.