Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Improved error handling and refactor #2184

Merged
merged 4 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import {Page, SlidingPanel} from 'argo-ui';
import * as React from 'react';
import {Link, RouteComponentProps} from 'react-router-dom';
import * as models from '../../../../models';
import {CronWorkflow} from '../../../../models';
import {uiUrl} from '../../../shared/base';
import {BasePage} from '../../../shared/components/base-page';
import {Loading} from '../../../shared/components/loading';
import {NamespaceFilter} from '../../../shared/components/namespace-filter';
import {ResourceSubmit} from '../../../shared/components/resource-submit';
import {Timestamp} from '../../../shared/components/timestamp';
import {YamlEditor} from '../../../shared/components/yaml/yaml-editor';
import {ZeroState} from '../../../shared/components/zero-state';
import {Consumer} from '../../../shared/context';
import {exampleCronWorkflow} from '../../../shared/examples';
Expand Down Expand Up @@ -87,15 +86,13 @@ export class CronWorkflowList extends BasePage<RouteComponentProps<any>, State>
<div className='columns small-12'>{this.renderCronWorkflows()}</div>
</div>
<SlidingPanel isShown={this.sidePanel !== null} onClose={() => (this.sidePanel = null)}>
<YamlEditor
title='Create New Cron Workflow'
editing={true}
value={exampleCronWorkflow(this.namespace)}
onSubmit={(value: CronWorkflow) => {
<ResourceSubmit<models.CronWorkflow>
resourceName={'Cron Workflow'}
defaultResource={exampleCronWorkflow(this.namespace)}
onSubmit={cronWf => {
return services.cronWorkflows
.create(value, value.metadata.namespace)
.then(res => ctx.navigation.goto(uiUrl(`cron-workflows/${res.metadata.namespace}/${res.metadata.name}`)))
.catch(error => this.setState({error}));
.create(cronWf, cronWf.metadata.namespace)
.then(res => ctx.navigation.goto(uiUrl(`cron-workflows/${res.metadata.namespace}/${res.metadata.name}`)));
}}
/>
</SlidingPanel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,32 @@ import {Formik} from 'formik';
import * as jsYaml from 'js-yaml';
import * as React from 'react';
import * as models from '../../../models';
import {uiUrl} from '../../shared/base';
import {ContextApis} from '../../shared/context';
import {services} from '../../shared/services';

interface WorkflowSubmitProps {
defaultWorkflow: models.Workflow;
ctx: ContextApis;
currentNamespace: string;
interface ResourceSubmitProps<T> {
defaultResource: T;
resourceName: string;
onSubmit: (value: T) => Promise<void>;
}

interface WorkflowSubmitState {
error?: Error;
interface ResourceSubmitState {
invalid: boolean;
error?: any;
}

export class WorkflowSubmit extends React.Component<WorkflowSubmitProps, WorkflowSubmitState> {
constructor(props: WorkflowSubmitProps) {
export class ResourceSubmit<T> extends React.Component<ResourceSubmitProps<T>, ResourceSubmitState> {
constructor(props: ResourceSubmitProps<T>) {
super(props);
this.state = {};
this.state = {invalid: false};
}

public render() {
return (
<div>
<Formik
initialValues={{wf: this.props.defaultWorkflow, wfString: jsYaml.dump(this.props.defaultWorkflow)}}
initialValues={{resource: this.props.defaultResource, resourceString: jsYaml.dump(this.props.defaultResource)}}
onSubmit={(values, {setSubmitting}) => {
services.workflows
.create(values.wf, values.wf.metadata.namespace || this.props.currentNamespace)
.then(wf => this.props.ctx.navigation.goto(uiUrl(`workflows/${wf.metadata.namespace}/${wf.metadata.name}`)))
this.props
.onSubmit(values.resource)
.then(_ => setSubmitting(false))
.catch(error => {
this.setState({error});
Expand All @@ -40,35 +37,40 @@ export class WorkflowSubmit extends React.Component<WorkflowSubmitProps, Workflo
{(formikApi: any) => (
<form onSubmit={formikApi.handleSubmit}>
<div className='white-box editable-panel'>
<h4>Submit New Workflow</h4>
<button type='submit' className='argo-button argo-button--base' disabled={formikApi.isSubmitting}>
<h4>Submit New {this.props.resourceName}</h4>
<button type='submit' className='argo-button argo-button--base' disabled={formikApi.isSubmitting || this.state.invalid}>
Submit
</button>
{this.state.error && (
<p>
<i className='fa fa-exclamation-triangle status-icon--failed' /> {this.state.error.message}
<i className='fa fa-exclamation-triangle status-icon--failed' />
{this.state.error.response && this.state.error.response.body && this.state.error.response.body.message
? this.state.error.response.body.message
: this.state.error.message}
</p>
)}
<textarea
name={'wfString'}
name={'resourceString'}
className='yaml'
value={formikApi.values.wfString}
value={formikApi.values.resourceString}
onChange={e => {
formikApi.handleChange(e);
}}
onBlur={e => {
formikApi.handleBlur(e);
try {
formikApi.setFieldValue('wf', jsYaml.load(e.currentTarget.value));
formikApi.setFieldValue('resource', jsYaml.load(e.currentTarget.value));
this.setState({
error: undefined
error: undefined,
invalid: false
});
} catch (e) {
this.setState({
error: {
name: 'Workflow is invalid',
message: 'Workflow is invalid'
}
name: this.props.resourceName + ' is invalid',
message: this.props.resourceName + ' is invalid' + (e.reason ? ': ' + e.reason : '')
},
invalid: true
});
}
}}
Expand All @@ -77,11 +79,12 @@ export class WorkflowSubmit extends React.Component<WorkflowSubmitProps, Workflo
/>

{/* Workflow-level parameters*/}
{formikApi.values.wf &&
formikApi.values.wf.spec &&
formikApi.values.wf.spec.arguments &&
formikApi.values.wf.spec.arguments.parameters &&
this.renderParameterFields('Workflow Parameters', 'wf.spec.arguments', formikApi.values.wf.spec.arguments.parameters, formikApi)}
{this.props.resourceName === 'Workflow' &&
formikApi.values.resource &&
formikApi.values.resource.spec &&
formikApi.values.resource.spec.arguments &&
formikApi.values.resource.spec.arguments.parameters &&
this.renderParameterFields('Workflow Parameters', 'resource.spec.arguments', formikApi.values.resource.spec.arguments.parameters, formikApi)}
</div>
</form>
)}
Expand Down Expand Up @@ -110,7 +113,7 @@ export class WorkflowSubmit extends React.Component<WorkflowSubmitProps, Workflo
onChange={formikApi.handleChange}
onBlur={e => {
formikApi.handleBlur(e);
formikApi.setFieldValue('wfString', jsYaml.dump(formikApi.values.wf));
formikApi.setFieldValue('resourceString', jsYaml.dump(formikApi.values.resource));
}}
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import {NotificationType, Page} from 'argo-ui';
import {SlidingPanel} from 'argo-ui/src/index';
import * as React from 'react';
import {RouteComponentProps} from 'react-router';
import {Workflow, WorkflowTemplate} from '../../../../models';
import * as models from '../../../../models';
simster7 marked this conversation as resolved.
Show resolved Hide resolved
import {uiUrl} from '../../../shared/base';
import {BasePage} from '../../../shared/components/base-page';
import {Loading} from '../../../shared/components/loading';
import {YamlEditor} from '../../../shared/components/yaml/yaml-editor';
import {ResourceSubmit} from '../../../shared/components/resource-submit';
import {Consumer} from '../../../shared/context';
import {services} from '../../../shared/services';
import {WorkflowTemplateSummaryPanel} from '../workflow-template-summary-panel';

require('../../../workflows/components/workflow-details/workflow-details.scss');

interface State {
template?: WorkflowTemplate;
workflow?: Workflow;
template?: models.WorkflowTemplate;
error?: Error;
}

Expand All @@ -27,15 +27,25 @@ export class WorkflowTemplateDetails extends BasePage<RouteComponentProps<any>,
return this.props.match.params.name;
}

private get sidePanel() {
return this.queryParam('sidePanel');
}

private set sidePanel(sidePanel) {
this.setQueryParams({sidePanel});
}

constructor(props: RouteComponentProps<any>, context: any) {
super(props, context);
this.state = {};
}

public componentDidMount(): void {
public componentWillMount(): void {
services.workflowTemplate
.get(this.name, this.namespace)
.then(template => this.setState({template}))
.then(template => {
this.setState({template});
})
.catch(error => this.setState({error}));
}

Expand All @@ -44,48 +54,52 @@ export class WorkflowTemplateDetails extends BasePage<RouteComponentProps<any>,
throw this.state.error;
}
return (
<Page
title='Workflow Template Details'
toolbar={{
actionMenu: {
items: [
{
title: 'Submit',
iconClassName: 'fa fa-plus',
action: () => this.openSubmissionPanel()
<Consumer>
{ctx => (
<Page
title='Workflow Template Details'
toolbar={{
actionMenu: {
items: [
{
title: 'Submit',
iconClassName: 'fa fa-plus',
action: () => (this.sidePanel = 'new')
},
{
title: 'Delete',
iconClassName: 'fa fa-trash',
action: () => this.deleteWorkflowTemplate()
}
]
},
{
title: 'Delete',
iconClassName: 'fa fa-trash',
action: () => this.deleteWorkflowTemplate()
}
]
},
breadcrumbs: [
{
title: 'Workflow Template',
path: uiUrl('workflow-templates')
},
{title: this.namespace + '/' + this.name}
]
}}>
<div className='argo-container'>
<div className='workflow-details__content'>{this.renderWorkflowTemplate()}</div>
</div>
<SlidingPanel isShown={!!this.state.workflow} onClose={() => this.setState({workflow: null})}>
<YamlEditor
editing={true}
title='Submit Workflow'
value={this.state.workflow}
onSubmit={(value: Workflow) => {
services.workflows
.create(value, value.metadata.namespace)
.then(workflow => (document.location.href = uiUrl(`workflows/${workflow.metadata.namespace}/${workflow.metadata.name}`)))
.catch(error => this.setState({error}));
}}
/>
</SlidingPanel>
</Page>
breadcrumbs: [
{
title: 'Workflow Template',
path: uiUrl('workflow-templates')
},
{title: this.namespace + '/' + this.name}
]
}}>
<div className='argo-container'>
<div className='workflow-details__content'>{this.renderWorkflowTemplate()}</div>
</div>
{this.state.template && (
<SlidingPanel isShown={this.sidePanel !== null} onClose={() => (this.sidePanel = null)}>
<ResourceSubmit<models.Workflow>
resourceName={'Workflow'}
defaultResource={this.getWorkflow(this.state.template)}
onSubmit={wfValue => {
return services.workflows
.create(wfValue, wfValue.metadata.namespace)
.then(workflow => ctx.navigation.goto(uiUrl(`workflows/${workflow.metadata.namespace}/${workflow.metadata.name}`)));
}}
/>
</SlidingPanel>
)}
</Page>
)}
</Consumer>
);
}

Expand Down Expand Up @@ -113,24 +127,22 @@ export class WorkflowTemplateDetails extends BasePage<RouteComponentProps<any>,
});
}

private openSubmissionPanel() {
this.setState({
workflow: {
metadata: {
generateName: this.state.template.metadata.name + '-',
namespace: this.state.template.metadata.namespace
},
spec: {
entrypoint: this.state.template.spec.templates[0].name,
templates: this.state.template.spec.templates.map(t => ({
name: t.name,
templateRef: {
name: this.state.template.metadata.name,
template: t.name
}
}))
}
private getWorkflow(template: models.WorkflowTemplate): models.Workflow {
return {
metadata: {
generateName: template.metadata.name + '-',
namespace: template.metadata.namespace
},
spec: {
entrypoint: template.spec.templates[0].name,
templates: template.spec.templates.map(t => ({
name: t.name,
templateRef: {
name: template.metadata.name,
template: t.name
}
}))
}
});
};
}
}
Loading