From 7d5878e40be7fd7100dbd699dc142ca874813c72 Mon Sep 17 00:00:00 2001 From: Robert Bo Davis Date: Tue, 12 Dec 2023 13:24:42 -0500 Subject: [PATCH] refactor: use URLSearchParams for getProfileList service --- .../src/services/ProfileService.ts | 36 +++++++++++++------ .../src/states/profile/profileList.ts | 4 ++- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/plugin-hrm-form/src/services/ProfileService.ts b/plugin-hrm-form/src/services/ProfileService.ts index 68e8dc62bb..2c455356e3 100644 --- a/plugin-hrm-form/src/services/ProfileService.ts +++ b/plugin-hrm-form/src/services/ProfileService.ts @@ -16,6 +16,7 @@ import { fetchHrmApi } from './fetchHrmApi'; import { Identifier, Profile, ProfileFlag, ProfileSection } from '../states/profile/types'; +import search from 'components/search'; type ProfileId = Profile['id']; type ProfileFlagId = ProfileFlag['id']; @@ -64,13 +65,28 @@ export const updateProfileSection = (profileId: ProfileId, sectionId: ProfileSec }); }; -export const getProfileList = ( - offset: number = 1, - limit: number = 10, - sortBy: string | number = 'id', // id or name - sortDirection: string = null, - profileFlagIds: string = null, -) => - fetchHrmApi( - `/profiles?offset=${offset}&limit=${limit}&sortBy=${sortBy}&sortDirection=${sortDirection}&profileFlagIds=${profileFlagIds}`, - ); +type GetProfileListParams = { + offset?: number; + limit?: number; + sortBy?: 'id' | 'name' | 'createdAt' | 'updatedAt'; + sortDirection?: string; + profileFlagIds?: string[]; +}; + +export const getProfileList = ({ + offset = 0, + limit = 10, + sortBy = 'id', + sortDirection = null, + profileFlagIds = null, +}: // TODO: remove default empty object once params are passed through +GetProfileListParams = {}) => { + const searchParams = new URLSearchParams(); + searchParams.append('offset', offset.toString()); + searchParams.append('limit', limit.toString()); + searchParams.append('sortBy', sortBy); + if (sortDirection) searchParams.append('sortDirection', sortDirection); + if (profileFlagIds) searchParams.append('profileFlagIds', profileFlagIds.join(',')); + + return fetchHrmApi(`/profiles?${searchParams.toString()}`); +}; diff --git a/plugin-hrm-form/src/states/profile/profileList.ts b/plugin-hrm-form/src/states/profile/profileList.ts index 510b70421c..691e60ab3b 100644 --- a/plugin-hrm-form/src/states/profile/profileList.ts +++ b/plugin-hrm-form/src/states/profile/profileList.ts @@ -18,7 +18,9 @@ import { createAsyncAction, createReducer } from 'redux-promise-middleware-actio import * as t from './types'; import { getProfileList } from '../../services/ProfileService'; -export const loadProfileListAsync = createAsyncAction(t.LOAD_PROFILES_LIST, getProfileList); +export const loadProfileListAsync = createAsyncAction(t.LOAD_PROFILES_LIST, (params: any = {}) => { + return getProfileList(params); +}); const loadProfileListEntryIntoRedux = (state: t.ProfileListState, profilesListUpdate: any) => ({ ...state,