Skip to content

Commit 2ae5b18

Browse files
authored
Replace hardcoded query params with constants (#1896)
1 parent 4cefe05 commit 2ae5b18

File tree

6 files changed

+88
-3
lines changed

6 files changed

+88
-3
lines changed

.changeset/breezy-turkeys-doubt.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@orchestrator-ui/orchestrator-ui-components': minor
3+
---
4+
5+
Replace hardcoded query params with constants and enums

packages/orchestrator-ui-components/src/components/WfoSummary/WfoLatestOutOfSyncSubscriptionSummaryCard.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import { useTranslations } from 'next-intl';
55
import {
66
PATH_SUBSCRIPTIONS,
77
SummaryCardStatus,
8+
WfoSubscriptionListTab,
89
WfoSummaryCard,
910
} from '@/components';
1011
import { mapSubscriptionSummaryToSummaryCardListItem } from '@/pages/startPage/mappers';
1112
import { outOfSyncSubscriptionsListSummaryQueryVariables } from '@/pages/startPage/queryVariables';
1213
import { useGetSubscriptionSummaryListQuery } from '@/rtk';
1314
import { optionalArrayMapper } from '@/utils';
15+
import { WfoQueryParams, getUrlWithQueryParams } from '@/utils/getQueryParams';
1416

1517
export const WfoLatestOutOfSyncSubscriptionSummaryCard = () => {
1618
const t = useTranslations('startPage.outOfSyncSubscriptions');
@@ -23,6 +25,13 @@ export const WfoLatestOutOfSyncSubscriptionSummaryCard = () => {
2325
outOfSyncSubscriptionsListSummaryQueryVariables,
2426
);
2527

28+
const queryParams = {
29+
[WfoQueryParams.ACTIVE_TAB]: WfoSubscriptionListTab.ALL,
30+
[WfoQueryParams.SORT_BY]: 'field-startDate_order-ASC',
31+
[WfoQueryParams.QUERY_STRING]:
32+
'status:(provisioning|active) insync:false',
33+
};
34+
2635
return (
2736
<WfoSummaryCard
2837
headerTitle={t('headerTitle')}
@@ -37,7 +46,7 @@ export const WfoLatestOutOfSyncSubscriptionSummaryCard = () => {
3746
)}
3847
button={{
3948
name: t('buttonText'),
40-
url: `${PATH_SUBSCRIPTIONS}?activeTab=ALL&sortBy=field-startDate_order-ASC&queryString=status%3A%28provisioning%7Cactive%29+insync%3Afalse`,
49+
url: getUrlWithQueryParams(PATH_SUBSCRIPTIONS, queryParams),
4150
}}
4251
isLoading={isLoading}
4352
isFetching={isFetching}

packages/orchestrator-ui-components/src/components/WfoSummary/WfoMyWorkflowsSummaryCard.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@ import {
77
SummaryCardStatus,
88
WfoSummaryCard,
99
} from '@/components/WfoSummary/WfoSummaryCard';
10+
import { WfoWorkflowsListTabType } from '@/pages';
1011
import { mapProcessSummaryToSummaryCardListItem } from '@/pages/startPage/mappers';
1112
import { getMyWorkflowListSummaryQueryVariables } from '@/pages/startPage/queryVariables';
1213
import { useGetProcessListSummaryQuery } from '@/rtk';
13-
import { optionalArrayMapper } from '@/utils';
14+
import {
15+
WfoQueryParams,
16+
getUrlWithQueryParams,
17+
optionalArrayMapper,
18+
} from '@/utils';
1419

1520
export type WfoMyWorkflowsSummaryCardProps = {
1621
username: string;
@@ -29,6 +34,12 @@ export const WfoMyWorkflowsSummaryCard: FC<WfoMyWorkflowsSummaryCardProps> = ({
2934
getMyWorkflowListSummaryQueryVariables(username),
3035
);
3136

37+
const queryParams = {
38+
[WfoQueryParams.ACTIVE_TAB]: WfoWorkflowsListTabType.COMPLETED,
39+
[WfoQueryParams.SORT_BY]: 'field-lastModifiedAt_order-DESC',
40+
[WfoQueryParams.QUERY_STRING]: `createdBy:${username}`,
41+
};
42+
3243
return (
3344
<WfoSummaryCard
3445
headerTitle={t('headerTitle')}
@@ -41,7 +52,7 @@ export const WfoMyWorkflowsSummaryCard: FC<WfoMyWorkflowsSummaryCardProps> = ({
4152
)}
4253
button={{
4354
name: t('buttonText'),
44-
url: `${PATH_WORKFLOWS}?activeTab=COMPLETED&sortBy=field-lastModifiedAt_order-DESC&queryString=createdBy%3A${username}`,
55+
url: getUrlWithQueryParams(PATH_WORKFLOWS, queryParams),
4556
}}
4657
isLoading={isLoading}
4758
isFetching={isFetching}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { WfoQueryParams, getUrlWithQueryParams } from './getQueryParams';
2+
3+
describe('getUrlWithQueryParams', () => {
4+
it('should build URL with one query param', () => {
5+
const url = '/api/list';
6+
const params = { [WfoQueryParams.ACTIVE_TAB]: 'ALL' };
7+
expect(getUrlWithQueryParams(url, params)).toBe(
8+
'/api/list?activeTab=ALL',
9+
);
10+
});
11+
12+
it('should build URL with multiple params', () => {
13+
const url = '/api/list';
14+
const params = {
15+
[WfoQueryParams.ACTIVE_TAB]: 'ALL',
16+
[WfoQueryParams.PAGE]: '2',
17+
[WfoQueryParams.SORT_BY]: 'field-startDate_order-ASC',
18+
};
19+
const result = getUrlWithQueryParams(url, params);
20+
21+
expect(result).toContain('/api/list?');
22+
expect(result).toContain('activeTab=ALL');
23+
expect(result).toContain('page=2');
24+
expect(result).toContain('sortBy=field-startDate_order-ASC');
25+
});
26+
27+
it('should encode params properly', () => {
28+
const url = '/items';
29+
const params = {
30+
[WfoQueryParams.QUERY_STRING]:
31+
'status:(provisioning|active) insync:false',
32+
};
33+
34+
expect(getUrlWithQueryParams(url, params)).toBe(
35+
'/items?queryString=status%3A%28provisioning%7Cactive%29+insync%3Afalse',
36+
);
37+
});
38+
39+
it('should handle empty params object', () => {
40+
const url = '/no-params';
41+
expect(getUrlWithQueryParams(url, {})).toBe('/no-params?');
42+
});
43+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export enum WfoQueryParams {
2+
ACTIVE_TAB = 'activeTab',
3+
PAGE = 'page',
4+
PAGE_SIZE = 'pageSize',
5+
SORT_BY = 'sortBy',
6+
FILTER_BY = 'filterBy',
7+
QUERY_STRING = 'queryString',
8+
}
9+
10+
export const getUrlWithQueryParams = (
11+
url: string,
12+
params: Partial<Record<WfoQueryParams, string>>,
13+
) => {
14+
const queryString = new URLSearchParams(params).toString() ?? '';
15+
return `${url}?${queryString}`;
16+
};

packages/orchestrator-ui-components/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ export * from './toObjectWithSerializedValues';
1818
export * from './toObjectWithSortedKeys';
1919
export * from './uuid';
2020
export * from './cacheTag';
21+
export * from './getQueryParams';

0 commit comments

Comments
 (0)