From 67abb6aae4e9637bcd13db741050407795c27f9d Mon Sep 17 00:00:00 2001 From: tetora1053 Date: Tue, 12 Sep 2023 09:10:51 +0900 Subject: [PATCH 01/10] refactor(ui): workflow panel components from class to functional Signed-off-by: tetora1053 --- .../components/resubmit-workflow-panel.tsx | 166 ++++++++-------- .../components/retry-workflow-panel.tsx | 178 ++++++++--------- .../components/submit-workflow-panel.tsx | 187 ++++++++---------- 3 files changed, 241 insertions(+), 290 deletions(-) diff --git a/ui/src/app/workflows/components/resubmit-workflow-panel.tsx b/ui/src/app/workflows/components/resubmit-workflow-panel.tsx index e6604f467bb6..3e4093678751 100644 --- a/ui/src/app/workflows/components/resubmit-workflow-panel.tsx +++ b/ui/src/app/workflows/components/resubmit-workflow-panel.tsx @@ -1,5 +1,5 @@ import {Checkbox} from 'argo-ui'; -import * as React from 'react'; +import React, {useState} from 'react'; import {Parameter, ResubmitOpts, Workflow} from '../../../models'; import {uiUrl} from '../../shared/base'; import {ErrorNotice} from '../../shared/components/error-notice'; @@ -12,103 +12,93 @@ interface Props { isArchived: boolean; } -interface State { - overrideParameters: boolean; - workflowParameters: Parameter[]; - memoized: boolean; - error?: Error; - isSubmitting: boolean; -} +export function ResubmitWorkflowPanel(props: Props) { + const [overrideParameters, setOverrideParameters] = useState(false); + const [workflowParameters, setWorkflowParameters] = useState(JSON.parse(JSON.stringify(props.workflow.spec.arguments.parameters || []))); + const [memoized, setMemoized] = useState(false); + const [error, setError] = useState(); + const [isSubmitting, setIsSubmitting] = useState(false); -export class ResubmitWorkflowPanel extends React.Component { - constructor(props: any) { - super(props); - const state: State = { - workflowParameters: JSON.parse(JSON.stringify(this.props.workflow.spec.arguments.parameters || [])), - memoized: false, - isSubmitting: false, - overrideParameters: false + const submit = () => { + setIsSubmitting(true); + const parameters: ResubmitOpts['parameters'] = overrideParameters + ? [...workflowParameters.filter(p => Utils.getValueFromParameter(p) !== undefined).map(p => p.name + '=' + Utils.getValueFromParameter(p))] + : []; + const opts: ResubmitOpts = { + parameters, + memoized }; - this.state = state; - } - public render() { - return ( - <> -

Resubmit Workflow

-
- {this.props.workflow.metadata.namespace}/{this.props.workflow.metadata.name} -
- {this.state.error && } -
-
- -
- this.setState({overrideParameters})} /> -
-
+ const handleSuccess = (submitted: Workflow) => { + document.location.href = uiUrl(`workflows/${submitted.metadata.namespace}/${submitted.metadata.name}`); + }; - {this.state.overrideParameters && ( -
- - {this.state.workflowParameters.length > 0 && ( - this.setState({workflowParameters})} /> - )} - {this.state.workflowParameters.length === 0 ? ( - <> -
- - - ) : ( - <> - )} -
- )} + const handleError = (err: Error) => { + setError(err); + setIsSubmitting(false); + }; + + if (!props.isArchived) { + services.workflows + .resubmit(props.workflow.metadata.name, props.workflow.metadata.namespace, opts) + .then(handleSuccess) + .catch(handleError); + } else { + services.workflows + .resubmitArchived(props.workflow.metadata.uid, props.workflow.metadata.namespace, opts) + .then(handleSuccess) + .catch(handleError); + } + }; -
- -
- this.setState({memoized})} /> -
+ return ( + <> +

Resubmit Workflow

+
+ {props.workflow.metadata.namespace}/{props.workflow.metadata.name} +
+ {error && } +
+
+ +
+
+
- {this.state.overrideParameters && this.state.memoized && ( -
- - Overriding parameters on memoized submitted workflows may have unexpected results. -
- )} + {overrideParameters && ( +
+ + {workflowParameters.length > 0 && } + {workflowParameters.length === 0 && ( + <> +
+ + + )} +
+ )} -
- +
+ +
+
- - ); - } - private submit() { - this.setState({isSubmitting: true}); - const parameters: ResubmitOpts['parameters'] = this.state.overrideParameters - ? [...this.state.workflowParameters.filter(p => Utils.getValueFromParameter(p) !== undefined).map(p => p.name + '=' + Utils.getValueFromParameter(p))] - : []; - const opts: ResubmitOpts = { - parameters, - memoized: this.state.memoized - }; + {overrideParameters && memoized && ( +
+ + Overriding parameters on memoized submitted workflows may have unexpected results. +
+ )} - if (!this.props.isArchived) { - services.workflows - .resubmit(this.props.workflow.metadata.name, this.props.workflow.metadata.namespace, opts) - .then((submitted: Workflow) => (document.location.href = uiUrl(`workflows/${submitted.metadata.namespace}/${submitted.metadata.name}`))) - .catch(error => this.setState({error, isSubmitting: false})); - } else { - services.workflows - .resubmitArchived(this.props.workflow.metadata.uid, this.props.workflow.metadata.namespace, opts) - .then((submitted: Workflow) => (document.location.href = uiUrl(`workflows/${submitted.metadata.namespace}/${submitted.metadata.name}`))) - .catch(error => this.setState({error, isSubmitting: false})); - } - } +
+ +
+
+ + ); } diff --git a/ui/src/app/workflows/components/retry-workflow-panel.tsx b/ui/src/app/workflows/components/retry-workflow-panel.tsx index 678088075a74..5943c58a89c3 100644 --- a/ui/src/app/workflows/components/retry-workflow-panel.tsx +++ b/ui/src/app/workflows/components/retry-workflow-panel.tsx @@ -1,5 +1,5 @@ import {Checkbox} from 'argo-ui'; -import * as React from 'react'; +import React, {useState} from 'react'; import {Parameter, RetryOpts, Workflow} from '../../../models'; import {uiUrl} from '../../shared/base'; import {ErrorNotice} from '../../shared/components/error-notice'; @@ -12,112 +12,98 @@ interface Props { isArchived: boolean; } -interface State { - overrideParameters: boolean; - restartSuccessful: boolean; - workflowParameters: Parameter[]; - nodeFieldSelector: string; - error?: Error; - isSubmitting: boolean; -} +export function RetryWorkflowPanel(props: Props) { + const [overrideParameters, setOverrideParameters] = useState(false); + const [restartSuccessful, setRestartSuccessful] = useState(false); + const [workflowParameters, setWorkflowParameters] = useState(JSON.parse(JSON.stringify(props.workflow.spec.arguments.parameters || []))); + const [nodeFieldSelector, setNodeFieldSelector] = useState(''); + const [error, setError] = useState(); + const [isSubmitting, setIsSubmitting] = useState(false); -export class RetryWorkflowPanel extends React.Component { - constructor(props: any) { - super(props); - const state: State = { - workflowParameters: JSON.parse(JSON.stringify(this.props.workflow.spec.arguments.parameters || [])), - isSubmitting: false, - overrideParameters: false, - nodeFieldSelector: '', - restartSuccessful: false + const submit = () => { + setIsSubmitting(true); + const parameters: RetryOpts['parameters'] = overrideParameters + ? [...workflowParameters.filter(p => Utils.getValueFromParameter(p) !== undefined).map(p => p.name + '=' + Utils.getValueFromParameter(p))] + : []; + const opts: RetryOpts = { + parameters, + restartSuccessful, + nodeFieldSelector }; - this.state = state; - } - public render() { - return ( - <> -

Retry Workflow

-
- {this.props.workflow.metadata.namespace}/{this.props.workflow.metadata.name} -
+ const process = props.isArchived + ? services.workflows.retryArchived(props.workflow.metadata.uid, props.workflow.metadata.namespace, opts) + : services.workflows.retry(props.workflow.metadata.name, props.workflow.metadata.namespace, opts); - {this.state.error && } -
-
- -
- this.setState({overrideParameters})} /> -
-
+ process + .then((submitted: Workflow) => { + document.location.href = uiUrl(`workflows/${submitted.metadata.namespace}/${submitted.metadata.name}`); + setIsSubmitting(false); + }) + .catch(err => { + setError(err); + setIsSubmitting(false); + }); + }; - {this.state.overrideParameters && ( -
- - {this.state.workflowParameters.length > 0 && ( - this.setState({workflowParameters})} /> - )} - {this.state.workflowParameters.length === 0 ? ( - <> -
- - - ) : ( - <> - )} -
- )} + return ( + <> +

Retry Workflow

+
+ {props.workflow.metadata.namespace}/{props.workflow.metadata.name} +
-
- -
- this.setState({restartSuccessful})} /> -
+ {error && } +
+ {/* Override Parameters */} +
+ +
+
+
- {this.state.restartSuccessful && ( -
- - -
-