-
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.
Converts Catalogs > Catalog > Add/Edit to react forms
Showing
5 changed files
with
190 additions
and
3 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
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,111 @@ | ||
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 './create-form-schema'; | ||
import { | ||
leftValues, | ||
addedLeftValues, | ||
addedRightValues, | ||
getKeys, | ||
} from '../dual-list-select/helpers'; | ||
import httpApi from '../../http_api/http'; | ||
|
||
|
||
class CatalogForm extends Component { | ||
constructor(props) { | ||
super(props); | ||
const { catalogItems, catalogSelectedItems } = this.props; | ||
const options = catalogItems.concat(catalogSelectedItems).map(item => ({ key: `${item[1]}`, label: item[0] })); | ||
const rightValues = catalogSelectedItems.map(item => ({ key: `${item[1]}`, label: item[0] })); | ||
|
||
this.state = { | ||
schema: createSchema(options), | ||
initialValues: { | ||
name: props.catalogName, | ||
description: props.catalogDescription, | ||
duallist: rightValues, | ||
}, | ||
originalLeftValues: leftValues(options, rightValues), | ||
originalOptions: options, | ||
originalRightValues: rightValues, | ||
}; | ||
} | ||
|
||
submit = (values) => { | ||
const { catalogId } = this.props; | ||
const { originalLeftValues, originalOptions, originalRightValues } = this.state; | ||
const rightKeys = getKeys(addedRightValues(values.duallist, originalRightValues)); | ||
const leftKeys = getKeys(addedLeftValues(originalOptions, values.duallist, originalLeftValues)); | ||
|
||
if (rightKeys.length > 0) { | ||
const rightData = rightKeys.map(key => `${encodeURIComponent('available_fields[]')}=${encodeURIComponent(`${key}`)}`); | ||
|
||
httpApi.post(`st_catalog_form_field_changed/${catalogId}?button=right`, | ||
rightData.join('&'), | ||
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }); | ||
} | ||
|
||
if (leftKeys.length > 0) { | ||
const rightData = leftKeys.map(key => `${encodeURIComponent('selected_fields[]')}=${encodeURIComponent(`${key}`)}`); | ||
|
||
httpApi.post(`st_catalog_form_field_changed/${catalogId}?button=left`, | ||
rightData.join('&'), | ||
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }); | ||
} | ||
|
||
if (values.description) { | ||
httpApi.post(`st_catalog_form_field_changed/${catalogId}`, | ||
`description=${encodeURIComponent(`${values.description}`)}`, | ||
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }); | ||
} | ||
|
||
httpApi.post(`st_catalog_form_field_changed/${catalogId}`, | ||
`name=${encodeURIComponent(`${values.name}`)}`, | ||
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }); | ||
|
||
httpApi.post(`st_catalog_edit/${catalogId}?button=save`, { }, { headers: { 'Content-Type': 'application/json;' } }); | ||
}; | ||
|
||
render() { | ||
const { catalogName, catalogId } = this.props; | ||
const cancelUrl = `/catalog/st_catalog_edit/${catalogId}?button=cancel`; | ||
return ( | ||
<Grid fluid> | ||
<MiqFormRenderer | ||
initialValues={this.state.initialValues} | ||
schema={this.state.schema} | ||
onSubmit={this.submit} | ||
onCancel={() => miqAjaxButton(cancelUrl)} | ||
onReset={() => add_flash(__('All changes have been reset'), 'warn')} | ||
canReset={!!catalogName} | ||
buttonsLabels={{ | ||
submitLabel: catalogName ? __('Save') : __('Add'), | ||
}} | ||
/> | ||
</Grid> | ||
); | ||
} | ||
} | ||
|
||
CatalogForm.propTypes = { | ||
catalogName: PropTypes.string, | ||
catalogDescription: PropTypes.string, | ||
catalogItems: PropTypes.arrayOf(PropTypes.shape({ | ||
[PropTypes.string]: PropTypes.string, | ||
})), | ||
catalogSelectedItems: PropTypes.arrayOf(PropTypes.shape({ | ||
[PropTypes.string]: PropTypes.string, | ||
})), | ||
catalogId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), | ||
}; | ||
|
||
CatalogForm.defaultProps = { | ||
catalogName: undefined, | ||
catalogDescription: undefined, | ||
catalogItems: [], | ||
catalogSelectedItems: [], | ||
catalogId: undefined, | ||
}; | ||
|
||
export default CatalogForm; |
48 changes: 48 additions & 0 deletions
48
app/javascript/components/catalog-form/create-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,48 @@ | ||
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, | ||
}, { | ||
component: componentTypes.TEXT_FIELD, | ||
name: 'description', | ||
label: __('Description'), | ||
maxLength: 60, | ||
}], | ||
}, { | ||
component: '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: 'duallist', | ||
}, | ||
], | ||
}]; | ||
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
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