|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | +import { Grid } from 'patternfly-react'; |
| 4 | +import MiqFormRenderer from '../../forms/data-driven-form'; |
| 5 | +import createSchema from './create-form-schema'; |
| 6 | +import { |
| 7 | + leftValues, |
| 8 | + addedLeftValues, |
| 9 | + addedRightValues, |
| 10 | + getKeys, |
| 11 | +} from '../dual-list-select/helpers'; |
| 12 | +import httpApi from '../../http_api/http'; |
| 13 | + |
| 14 | + |
| 15 | +class CatalogForm extends Component { |
| 16 | + constructor(props) { |
| 17 | + super(props); |
| 18 | + const { catalogItems, catalogSelectedItems } = this.props; |
| 19 | + const options = catalogItems.concat(catalogSelectedItems).map(item => ({ key: `${item[1]}`, label: item[0] })); |
| 20 | + const rightValues = catalogSelectedItems.map(item => ({ key: `${item[1]}`, label: item[0] })); |
| 21 | + |
| 22 | + this.state = { |
| 23 | + schema: createSchema(options), |
| 24 | + initialValues: { |
| 25 | + name: props.catalogName, |
| 26 | + description: props.catalogDescription, |
| 27 | + duallist: rightValues, |
| 28 | + }, |
| 29 | + originalLeftValues: leftValues(options, rightValues), |
| 30 | + originalOptions: options, |
| 31 | + originalRightValues: rightValues, |
| 32 | + }; |
| 33 | + } |
| 34 | + |
| 35 | + submit = (values) => { |
| 36 | + const { catalogId } = this.props; |
| 37 | + const { originalLeftValues, originalOptions, originalRightValues } = this.state; |
| 38 | + const rightKeys = getKeys(addedRightValues(values.duallist, originalRightValues)); |
| 39 | + const leftKeys = getKeys(addedLeftValues(originalOptions, values.duallist, originalLeftValues)); |
| 40 | + |
| 41 | + if (rightKeys.length > 0) { |
| 42 | + const rightData = rightKeys.map(key => `${encodeURIComponent('available_fields[]')}=${encodeURIComponent(`${key}`)}`); |
| 43 | + |
| 44 | + httpApi.post(`st_catalog_form_field_changed/${catalogId}?button=right`, |
| 45 | + rightData.join('&'), |
| 46 | + { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }); |
| 47 | + } |
| 48 | + |
| 49 | + if (leftKeys.length > 0) { |
| 50 | + const rightData = leftKeys.map(key => `${encodeURIComponent('selected_fields[]')}=${encodeURIComponent(`${key}`)}`); |
| 51 | + |
| 52 | + httpApi.post(`st_catalog_form_field_changed/${catalogId}?button=left`, |
| 53 | + rightData.join('&'), |
| 54 | + { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }); |
| 55 | + } |
| 56 | + |
| 57 | + if (values.description) { |
| 58 | + httpApi.post(`st_catalog_form_field_changed/${catalogId}`, |
| 59 | + `description=${encodeURIComponent(`${values.description}`)}`, |
| 60 | + { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }); |
| 61 | + } |
| 62 | + |
| 63 | + httpApi.post(`st_catalog_form_field_changed/${catalogId}`, |
| 64 | + `name=${encodeURIComponent(`${values.name}`)}`, |
| 65 | + { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }); |
| 66 | + |
| 67 | + httpApi.post(`st_catalog_edit/${catalogId}?button=save`, { }, { headers: { 'Content-Type': 'application/json;' } }); |
| 68 | + }; |
| 69 | + |
| 70 | + render() { |
| 71 | + const { catalogName, catalogId } = this.props; |
| 72 | + const cancelUrl = `/catalog/st_catalog_edit/${catalogId}?button=cancel`; |
| 73 | + return ( |
| 74 | + <Grid fluid> |
| 75 | + <MiqFormRenderer |
| 76 | + initialValues={this.state.initialValues} |
| 77 | + schema={this.state.schema} |
| 78 | + onSubmit={this.submit} |
| 79 | + onCancel={() => miqAjaxButton(cancelUrl)} |
| 80 | + onReset={() => add_flash(__('All changes have been reset'), 'warn')} |
| 81 | + canReset={!!catalogName} |
| 82 | + buttonsLabels={{ |
| 83 | + submitLabel: catalogName ? __('Save') : __('Add'), |
| 84 | + }} |
| 85 | + /> |
| 86 | + </Grid> |
| 87 | + ); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +CatalogForm.propTypes = { |
| 92 | + catalogName: PropTypes.string, |
| 93 | + catalogDescription: PropTypes.string, |
| 94 | + catalogItems: PropTypes.arrayOf(PropTypes.shape({ |
| 95 | + [PropTypes.string]: PropTypes.string, |
| 96 | + })), |
| 97 | + catalogSelectedItems: PropTypes.arrayOf(PropTypes.shape({ |
| 98 | + [PropTypes.string]: PropTypes.string, |
| 99 | + })), |
| 100 | + catalogId: PropTypes.oneOfType([PropTypes.number, PropTypes.string]), |
| 101 | +}; |
| 102 | + |
| 103 | +CatalogForm.defaultProps = { |
| 104 | + catalogName: undefined, |
| 105 | + catalogDescription: undefined, |
| 106 | + catalogItems: [], |
| 107 | + catalogSelectedItems: [], |
| 108 | + catalogId: undefined, |
| 109 | +}; |
| 110 | + |
| 111 | +export default CatalogForm; |
0 commit comments