Skip to content

Commit

Permalink
fix(ui): refactor code. Fixes argoproj#1214
Browse files Browse the repository at this point in the history
Signed-off-by: Sairam Arunachalam <sair.aruna@gmail.com>
  • Loading branch information
sairam91 committed Oct 16, 2024
1 parent e8ec0aa commit 46c3503
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
32 changes: 32 additions & 0 deletions ui/src/app/shared/get_workflow_params.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {createBrowserHistory, createPath} from 'history';
import {getWorkflowParametersFromQuery} from './get_workflow_params';

describe('get_workflow_params', () => {
it('should return an empty object when there are no query parameters', () => {
const history = createBrowserHistory();
const result = getWorkflowParametersFromQuery(history);
expect(result).toEqual({});
});

it('should return the parameters provided in the URL', () => {
const history = createBrowserHistory();
const path = createPath({pathname: '/workflows', search: '?parameters[key1]=value1&parameters[key2]=value2'});
history.location.search = path;
const result = getWorkflowParametersFromQuery(history);
expect(result).toEqual({
key1: 'value1',
key2: 'value2'
});
});

it('should only return the parameters provided in the URL', () => {
const history = createBrowserHistory();
const path = createPath({pathname: '/workflows', search: '?parameters[key1]=value1&parameters[key2]=value2&test=123'});
history.location.search = path;
const result = getWorkflowParametersFromQuery(history);
expect(result).toEqual({
key1: 'value1',
key2: 'value2'
});
});
});
11 changes: 2 additions & 9 deletions ui/src/app/shared/get_workflow_params.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import {History} from 'history';
/**
* Method to extract a key from query parameter. The query parameter will be of the following format
* ?parameters[key]=value.
* This method will extract the key from the query parameter.
* @param inputString
* @returns
*/

function extractKey(inputString: string): string | null {
// Use regular expression to match the key within square brackets
const match = inputString.match(/parameters\[(.*?)\]/);
const match = inputString.match(/^parameters\[(.*?)\]$/);

// If a match is found, return the captured key
if (match) {
Expand All @@ -20,7 +14,6 @@ function extractKey(inputString: string): string | null {
}
/**
* Returns the workflow parameters from the query parameters.
* @returns {}
*/
export function getWorkflowParametersFromQuery(history: History): {[key: string]: string} {
const queryParams = new URLSearchParams(history.location.search);
Expand Down
12 changes: 4 additions & 8 deletions ui/src/app/workflows/components/submit-workflow-panel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,10 @@ export function SubmitWorkflowPanel(props: Props) {
useEffect(() => {
const templatePropertiesInQuery = getWorkflowParametersFromQuery(props.history);
// Get the user arguments from the query params
const updatedParams = workflowParameters.map(param => {
const queryValue = templatePropertiesInQuery[param.name];
const p: Parameter = {
name: param.name,
value: queryValue || param.value
};
return p;
});
const updatedParams = workflowParameters.map(param => ({
name: param.name,
value: templatePropertiesInQuery[param.name] || param.value
}));
setWorkflowParameters(updatedParams);
}, [props.history, setWorkflowParameters]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {WorkflowsToolbar} from '../workflows-toolbar/workflows-toolbar';
import './workflows-list.scss';
import useTimestamp, {TIMESTAMP_KEYS} from '../../../shared/use-timestamp';
import {TimestampSwitch} from '../../../shared/components/timestamp';
import {getWorkflowParametersFromQuery} from '../../../shared/get_workflow_params';

interface WorkflowListRenderOptions {
paginationLimit: number;
Expand Down Expand Up @@ -127,7 +126,7 @@ export function WorkflowsList({match, location, history}: RouteComponentProps<an
}
storage.setItem('options', options, {} as WorkflowListRenderOptions);

const params = new URLSearchParams();
const params = new URLSearchParams(history.location.search);
phases?.forEach(phase => params.append('phase', phase));
labels?.forEach(label => params.append('label', label));
if (pagination.offset) {
Expand All @@ -137,17 +136,6 @@ export function WorkflowsList({match, location, history}: RouteComponentProps<an
params.append('limit', pagination.limit.toString());
}

// Add any workflow parameters to the query
const workflowProperties = getWorkflowParametersFromQuery(history);

Object.keys(workflowProperties).forEach(key => {
params.append(`parameters[${key}]`, workflowProperties[key]);
});

// Add the sidePanel query parameter if it exists
params.append('sidePanel', getSidePanel());
params.append('template', queryParams.get('template'));

history.push(historyUrl('workflows' + (nsUtils.getManagedNamespace() ? '' : '/{namespace}'), {namespace, extraSearchParams: params}));
}, [namespace, phases.toString(), labels.toString(), pagination.limit, pagination.offset]); // referential equality, so use values, not refs

Expand Down

0 comments on commit 46c3503

Please sign in to comment.