-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5139 from rvsia/catalog-form-to-react-forms
Converted Catalog form
- Loading branch information
Showing
11 changed files
with
717 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
app/javascript/components/catalog-form/catalog-form.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Grid } from 'patternfly-react'; | ||
import MiqFormRenderer from '../../forms/data-driven-form'; | ||
import createSchema from './catalog-form.schema'; | ||
import { filterOptions, filterValues } from '../dual-list-select/helpers'; | ||
import { API } from '../../http_api'; | ||
import { cleanVirtualDom } from '../../miq-component/helpers'; | ||
|
||
class CatalogForm extends Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
isLoaded: false, | ||
}; | ||
cleanVirtualDom(); | ||
} | ||
|
||
componentDidMount() { | ||
miqSparkleOn(); | ||
const { catalogId } = this.props; | ||
if (catalogId) { | ||
Promise.all([ | ||
API.get('/api/service_templates?expand=resources&filter[]=service_template_catalog_id=null'), | ||
API.get(`/api/service_catalogs/${catalogId}?expand=service_templates`)]) | ||
.then(([{ resources }, { name, description, service_templates }]) => { | ||
const rightValues = service_templates.resources.map(({ href, name }) => ({ key: href, label: name })); | ||
const options = resources.map(({ href, name }) => ({ key: href, label: name })).concat(rightValues); | ||
this.setState(() => ({ | ||
schema: createSchema(options), | ||
initialValues: { | ||
name, | ||
description: description === null ? undefined : description, | ||
service_templates: rightValues.map(({ key }) => key), | ||
}, | ||
originalRightValues: rightValues, | ||
isLoaded: true, | ||
}), miqSparkleOff); | ||
}); | ||
} else { | ||
API.get('/api/service_templates?expand=resources&filter[]=service_template_catalog_id=null').then( | ||
({ resources }) => this.setState({ | ||
schema: createSchema(resources.map(({ href, name }) => ({ key: href, label: name }))), | ||
isLoaded: true, | ||
}, miqSparkleOff), | ||
); | ||
} | ||
} | ||
|
||
handleError = (error) => { | ||
const { data: { error: { message } } } = error; | ||
return message.includes('Name has already been taken') ? __('Name has already been taken') : message; | ||
} | ||
|
||
submitValues = (values) => { | ||
const { catalogId } = this.props; | ||
const { originalRightValues } = this.state; | ||
const { service_templates = [] } = values; | ||
const apiBase = `/api/service_catalogs${catalogId ? `/${catalogId}` : ''}`; | ||
|
||
if (!catalogId) { | ||
return API.post(apiBase, { | ||
action: 'create', | ||
resource: { | ||
...values, | ||
service_templates: service_templates.map(key => ({ href: `${key}` })), | ||
}, | ||
}, { | ||
skipErrors: [400], | ||
}) | ||
.then(() => miqAjaxButton('/catalog/st_catalog_edit?button=add')) | ||
.catch(error => add_flash(this.handleError(error), 'error')); | ||
} | ||
|
||
const unassignedRightValues = filterValues(values.service_templates, originalRightValues.map(({ key }) => key)); | ||
const unassignedLeftValues = filterOptions(originalRightValues, values.service_templates); | ||
const promises = [ | ||
API.post(apiBase, { | ||
action: 'edit', | ||
resource: { | ||
name: values.name, | ||
description: values.description, | ||
}, | ||
}, { | ||
skipErrors: [500], | ||
}), | ||
]; | ||
|
||
if (unassignedRightValues.length > 0) { | ||
promises.push( | ||
API.post(`${apiBase}/service_templates`, { | ||
action: 'assign', | ||
resources: unassignedRightValues.map(key => ({ href: key })), | ||
}), | ||
); | ||
} | ||
if (unassignedLeftValues.length > 0) { | ||
promises.push( | ||
API.post(`${apiBase}/service_templates`, { | ||
action: 'unassign', | ||
resources: unassignedLeftValues.map(({ key }) => ({ href: key })), | ||
}), | ||
); | ||
} | ||
|
||
return Promise.all(promises) | ||
.then(([{ id }]) => miqAjaxButton(`/catalog/st_catalog_edit/${id}?button=save`)) | ||
.catch(error => add_flash(this.handleError(error), 'error')); | ||
}; | ||
|
||
render() { | ||
const { catalogId } = this.props; | ||
const { isLoaded, initialValues, schema } = this.state; | ||
const cancelUrl = `/catalog/st_catalog_edit/${catalogId}?button=cancel`; | ||
if (!isLoaded) return null; | ||
|
||
return ( | ||
<Grid fluid> | ||
<MiqFormRenderer | ||
initialValues={initialValues} | ||
schema={schema} | ||
onSubmit={this.submitValues} | ||
onCancel={() => miqAjaxButton(cancelUrl)} | ||
onReset={() => add_flash(__('All changes have been reset'), 'warn')} | ||
canReset={!!catalogId} | ||
buttonsLabels={{ | ||
submitLabel: catalogId ? __('Save') : __('Add'), | ||
}} | ||
/> | ||
</Grid> | ||
); | ||
} | ||
} | ||
|
||
CatalogForm.propTypes = { | ||
catalogId: PropTypes.number, | ||
}; | ||
|
||
CatalogForm.defaultProps = { | ||
catalogId: undefined, | ||
}; | ||
|
||
export default CatalogForm; |
50 changes: 50 additions & 0 deletions
50
app/javascript/components/catalog-form/catalog-form.schema.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { componentTypes, validatorTypes } from '@data-driven-forms/react-form-renderer'; | ||
|
||
function createSchema(options) { | ||
const fields = [{ | ||
component: componentTypes.SUB_FORM, | ||
title: __('Basic Info'), | ||
fields: [{ | ||
component: componentTypes.TEXT_FIELD, | ||
name: 'name', | ||
validate: [{ | ||
type: validatorTypes.REQUIRED, | ||
message: __("Name can't be blank"), | ||
}], | ||
label: __('Name'), | ||
maxLength: 40, | ||
autoFocus: true, | ||
validateOnMount: true, | ||
}, { | ||
component: componentTypes.TEXT_FIELD, | ||
name: 'description', | ||
label: __('Description'), | ||
maxLength: 60, | ||
}], | ||
}, { | ||
component: 'hr', | ||
name: 'hr', | ||
}, { | ||
component: componentTypes.SUB_FORM, | ||
title: __('Assign Catalog Items'), | ||
fields: [ | ||
{ | ||
component: 'dual-list-select', | ||
leftTitle: __('Unassigned:'), | ||
rightTitle: __('Selected:'), | ||
leftId: 'available_fields', | ||
rightId: 'selected_fields', | ||
allToRight: false, | ||
moveLeftTitle: __('Move Selected buttons left'), | ||
moveRightTitle: __('Move Selected buttons right'), | ||
size: 8, | ||
assignFieldProvider: true, | ||
options, | ||
name: 'service_templates', | ||
}, | ||
], | ||
}]; | ||
return { fields }; | ||
} | ||
|
||
export default createSchema; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import React from 'react'; | ||
import { formFieldsMapper } from '@data-driven-forms/pf3-component-mapper'; | ||
import DualListSelect from '../../components/dual-list-select'; | ||
|
||
const fieldsMapper = { | ||
...formFieldsMapper, | ||
'dual-list-select': DualListSelect, | ||
hr: () => <hr />, | ||
}; | ||
|
||
export default fieldsMapper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.