-
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.
- Loading branch information
1 parent
bed0cca
commit 61f092e
Showing
19 changed files
with
394 additions
and
670 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
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 |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import MiqFormRenderer from '@@ddf'; | ||
import createSchema from './subnet-form.schema'; | ||
import miqRedirectBack from '../../helpers/miq-redirect-back'; | ||
import { API } from '../../http_api'; | ||
|
||
const SubnetForm = ({ recordId }) => { | ||
const [{ initialValues, isLoading, fields }, setState] = useState({ isLoading: !!recordId, fields: [] }); | ||
const submitLabel = !!recordId ? __('Save') : __('Add'); | ||
|
||
const loadSchema = (appendState = {}) => ({ data: { form_schema: { fields } } }) => { | ||
if (!!recordId && appendState.initialValues.type === 'ManageIQ::Providers::Openstack::NetworkManager::CloudSubnet') { | ||
Object.assign(fields[0], {isDisabled: true}); | ||
Object.assign(fields[1], {isDisabled: true}); | ||
Object.assign(fields[4], {isDisabled: true}); | ||
} | ||
setState((state) => ({ | ||
...state, | ||
...appendState, | ||
fields, | ||
})); | ||
}; | ||
|
||
useEffect(() => { | ||
if (recordId) { | ||
API.get(`/api/cloud_subnets/${recordId}`).then((initialValues) => { | ||
if(typeof initialValues.cloud_network_id ==="string") { | ||
initialValues.cloud_network_id=Number(initialValues.cloud_network_id); | ||
} | ||
if(typeof initialValues.cloud_tenant_id ==="string") { | ||
initialValues.cloud_tenant_id=Number(initialValues.cloud_tenant_id); | ||
} | ||
API.options(`/api/cloud_subnets?ems_id=${initialValues.ems_id}`).then(loadSchema({ initialValues, isLoading: false })); | ||
}); | ||
} | ||
}, [recordId]); | ||
|
||
const onSubmit = (values) => { | ||
API.get(`/api/providers/${values.ems_id}`).then(({ type }) => { | ||
if (type === 'ManageIQ::Providers::Openstack::NetworkManager') { | ||
if (values.ip_version === undefined) { | ||
values.ip_version = '4'; | ||
} | ||
if (values.dhcp_enabled === undefined) { | ||
values.dhcp_enabled = false; | ||
} | ||
values.enable_dhcp = values.dhcp_enabled; | ||
delete values.dhcp_enabled; | ||
delete Object.assign(values, values.extra_attributes).extra_attributes; | ||
|
||
API.get(`/api/cloud_networks/${values.cloud_network_id}`).then( ({ ems_ref }) => { | ||
values.network_id = ems_ref; | ||
miqSparkleOn(); | ||
const request = recordId ? API.patch(`/api/cloud_subnets/${recordId}`, values) : API.post('/api/cloud_subnets', values); | ||
|
||
request.then(() => { | ||
const message = sprintf(recordId | ||
? __('Modification of Cloud Subnet %s has been successfully queued') | ||
: __('Add of Cloud Subnet "%s" has been successfully queued.'), | ||
values.name); | ||
|
||
miqRedirectBack(message, 'success', '/cloud_subnet/show_list'); | ||
}).catch(miqSparkleOff); | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
const onCancel = () => { | ||
const message = sprintf( | ||
recordId | ||
? __('Edit of Cloud Subnet "%s" was canceled by the user.') | ||
: __('Creation of new Cloud Subnet was canceled by the user.'), | ||
initialValues && initialValues.name, | ||
); | ||
miqRedirectBack(message, 'warning', '/cloud_subnet/show_list'); | ||
}; | ||
|
||
return !isLoading && ( | ||
<MiqFormRenderer | ||
schema={createSchema(!!recordId, fields, loadSchema)} | ||
initialValues={initialValues} | ||
canReset={!!recordId} | ||
onSubmit={onSubmit} | ||
onCancel={onCancel} | ||
buttonsLabels={{ submitLabel }} | ||
/> | ||
); | ||
}; | ||
|
||
SubnetForm.propTypes = { | ||
recordId: PropTypes.string, | ||
}; | ||
SubnetForm.defaultProps = { | ||
recordId: undefined, | ||
}; | ||
|
||
export default SubnetForm; |
Oops, something went wrong.