-
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.
Converted Add/Remove Security groups form to react
- Loading branch information
1 parent
70548e4
commit 32115ef
Showing
12 changed files
with
483 additions
and
183 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
app/javascript/components/vm-cloud-add-remove-security-group-form/index.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,120 @@ | ||
/* eslint-disable camelcase */ | ||
import React, { useState, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Loading } from 'carbon-components-react'; | ||
import MiqFormRenderer from '../../forms/data-driven-form'; | ||
import createSchema from './vm-cloud-add-security-form.schema'; | ||
import { API } from '../../http_api'; | ||
import miqRedirectBack from '../../helpers/miq-redirect-back'; | ||
|
||
const AddRemoveSecurityGroupForm = ({ | ||
recordId, redirectURL, isAdd, | ||
}) => { | ||
const [{ | ||
isLoading, securityGroups, | ||
}, setState] = useState({ | ||
isLoading: !!recordId, | ||
}); | ||
|
||
const getSecurityGroups = (currentSecurityGroups) => { | ||
API.get(`/api/instances/${recordId}`).then((data) => { | ||
API.get(`/api/cloud_tenants/${data.cloud_tenant_id}/security_groups?expand=resources&attributes=id,name`) | ||
.then((data) => { | ||
const securityGroups = []; | ||
data.resources.forEach((securityGroup) => { | ||
if (!currentSecurityGroups.includes(securityGroup.id)) { | ||
securityGroups.push({ label: securityGroup.name, value: securityGroup.name }); | ||
} | ||
}); | ||
setState({ | ||
securityGroups, | ||
isLoading: false, | ||
}); | ||
}); | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
if (isAdd && isLoading) { | ||
API.get(`/api/instances/${recordId}/security_groups?expand=resources&attributes=id,name`) | ||
.then((data) => { | ||
const currentSecurityGroups = []; | ||
data.resources.forEach((securityGroup) => { | ||
currentSecurityGroups.push(securityGroup.id); | ||
}); | ||
return currentSecurityGroups; | ||
}).then((currentSecurityGroups) => { | ||
getSecurityGroups(currentSecurityGroups); | ||
}); | ||
} else if (isLoading) { | ||
API.get(`/api/instances/${recordId}/security_groups?expand=resources&attributes=id,name`) | ||
.then((data) => { | ||
const currentSecurityGroups = []; | ||
data.resources.forEach((securityGroup) => { | ||
currentSecurityGroups.push({ label: securityGroup.name, value: securityGroup.name }); | ||
}); | ||
setState({ | ||
securityGroups: currentSecurityGroups, | ||
isLoading: false, | ||
}); | ||
}); | ||
} | ||
}); | ||
|
||
const onSubmit = (values) => { | ||
let message = __(`${values.security_group} has been successfully removed.`); | ||
if (isAdd) { | ||
const saveObject = { | ||
name: values.security_group, | ||
action: 'add', | ||
}; | ||
message = __(`${values.security_group} has been successfully added.`); | ||
miqSparkleOn(); | ||
API.post(`/api/instances/${recordId}/security_groups/`, saveObject) | ||
.then(miqRedirectBack(message, 'success', redirectURL)); | ||
} else { | ||
const saveObject = { | ||
name: values.security_group, | ||
action: 'remove', | ||
}; | ||
miqSparkleOn(); | ||
API.post(`/api/instances/${recordId}/security_groups/`, saveObject) | ||
.then(miqRedirectBack(message, 'success', redirectURL)); | ||
} | ||
}; | ||
|
||
const onCancel = () => { | ||
let message = __('Removal of security group was canceled by the user.'); | ||
miqSparkleOn(); | ||
if (isAdd) { | ||
message = __('Addition of security group was canceled by the user.'); | ||
miqRedirectBack(message, 'success', redirectURL); | ||
} else { | ||
miqRedirectBack(message, 'success', redirectURL); | ||
} | ||
}; | ||
|
||
if (isLoading) return <Loading className="export-spinner" withOverlay={false} small />; | ||
return !isLoading && ( | ||
<MiqFormRenderer | ||
schema={createSchema(securityGroups)} | ||
onSubmit={onSubmit} | ||
onCancel={onCancel} | ||
buttonsLabels={{ | ||
submitLabel: recordId ? __('Save') : __('Add'), | ||
}} | ||
/> | ||
); | ||
}; | ||
|
||
AddRemoveSecurityGroupForm.propTypes = { | ||
recordId: PropTypes.string.isRequired, | ||
redirectURL: PropTypes.string.isRequired, | ||
isAdd: PropTypes.bool, | ||
}; | ||
|
||
AddRemoveSecurityGroupForm.defaultProps = { | ||
isAdd: false, | ||
}; | ||
|
||
export default AddRemoveSecurityGroupForm; |
18 changes: 18 additions & 0 deletions
18
...t/components/vm-cloud-add-remove-security-group-form/vm-cloud-add-security-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,18 @@ | ||
import { componentTypes } from '@@ddf'; | ||
|
||
function createSchema(securityGroups) { | ||
const fields = [ | ||
{ | ||
component: componentTypes.SELECT, | ||
id: 'security_group', | ||
name: 'security_group', | ||
label: __('Security Group'), | ||
placeholder: __('<Choose>'), | ||
includeEmpty: true, | ||
options: securityGroups, | ||
}, | ||
]; | ||
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
65 changes: 0 additions & 65 deletions
65
app/javascript/oldjs/components/vm_cloud/vm-cloud-add-security-group.js
This file was deleted.
Oops, something went wrong.
50 changes: 0 additions & 50 deletions
50
app/javascript/oldjs/components/vm_cloud/vm-cloud-remove-security-group.js
This file was deleted.
Oops, something went wrong.
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.