Skip to content

Commit

Permalink
refactor: use URLSearchParams for getProfileList service
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-bo-davis committed Dec 12, 2023
1 parent 50bdf77 commit 7d5878e
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
36 changes: 26 additions & 10 deletions plugin-hrm-form/src/services/ProfileService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import { fetchHrmApi } from './fetchHrmApi';
import { Identifier, Profile, ProfileFlag, ProfileSection } from '../states/profile/types';

Check failure on line 18 in plugin-hrm-form/src/services/ProfileService.ts

View workflow job for this annotation

GitHub Actions / flex-ci / lint

There should be at least one empty line between import groups
import search from 'components/search';

Check failure on line 19 in plugin-hrm-form/src/services/ProfileService.ts

View workflow job for this annotation

GitHub Actions / flex-ci / lint

`components/search` import should occur before import of `./fetchHrmApi`

Check failure on line 19 in plugin-hrm-form/src/services/ProfileService.ts

View workflow job for this annotation

GitHub Actions / flex-ci / lint

Unable to resolve path to module 'components/search'

type ProfileId = Profile['id'];
type ProfileFlagId = ProfileFlag['id'];
Expand Down Expand Up @@ -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()}`);
};
4 changes: 3 additions & 1 deletion plugin-hrm-form/src/states/profile/profileList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 7d5878e

Please sign in to comment.