-
Notifications
You must be signed in to change notification settings - Fork 357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Transforms haml forms to react for Add and Edit feature of Automate Class #9301
Open
elsamaryv
wants to merge
5
commits into
ManageIQ:master
Choose a base branch
from
elsamaryv:class-form-converison
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d359d80
Transforms haml forms to react for Automate class
elsamaryv 6029c95
Minor fixes
GilbertCherrie b90b934
Uses placeholder for translations in the UI code
elsamaryv e7802ea
Formats Fully Qualified Name field
elsamaryv 7b7ba0c
Cypress config 'numTestsKeptInMemory' put back to 0
elsamaryv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 38 additions & 0 deletions
38
app/javascript/components/miq-ae-class/class-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,38 @@ | ||
import { componentTypes } from '@@ddf'; | ||
|
||
const createSchema = (fqname) => ({ | ||
fields: [ | ||
{ | ||
component: componentTypes.TEXT_FIELD, | ||
name: 'fqname', | ||
label: 'Fully Qualified Name', | ||
value: `${fqname}`, | ||
disabled: true, | ||
}, | ||
{ | ||
component: componentTypes.TEXT_FIELD, | ||
id: 'name', | ||
name: 'name', | ||
label: __('Name'), | ||
maxLength: 128, | ||
validate: [{ type: 'customValidatorForNameField' }], | ||
isRequired: true, | ||
}, | ||
{ | ||
component: componentTypes.TEXT_FIELD, | ||
id: 'display_name', | ||
name: 'display_name', | ||
label: __('Display Name'), | ||
maxLength: 128, | ||
}, | ||
{ | ||
component: componentTypes.TEXT_FIELD, | ||
id: 'description', | ||
name: 'description', | ||
label: __('Description'), | ||
maxLength: 255, | ||
}, | ||
], | ||
}); | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { FormSpy } from '@data-driven-forms/react-form-renderer'; | ||
import { Button } from 'carbon-components-react'; | ||
import MiqFormRenderer, { useFormApi } from '@@ddf'; | ||
import PropTypes from 'prop-types'; | ||
import createSchema from './class-form.schema'; | ||
import miqRedirectBack from '../../helpers/miq-redirect-back'; | ||
import miqFlash from '../../helpers/miq-flash'; | ||
|
||
const MiqAeClass = ({ classRecord, fqname }) => { | ||
const formattedFqname = fqname.replace(/\s+/g, ''); | ||
const [data, setData] = useState({ | ||
isLoading: true, | ||
initialValues: undefined, | ||
}); | ||
|
||
const isEdit = !!(classRecord && classRecord.id); | ||
|
||
useEffect(() => { | ||
if (isEdit) { | ||
http.get(`/miq_ae_class/edit_class_record/${classRecord.id}/`).then((recordValues) => { | ||
if (recordValues) { | ||
setData({ ...data, isLoading: false, initialValues: recordValues }); | ||
} | ||
}); | ||
} else { | ||
const initialValues = { | ||
formattedFqname, | ||
name: classRecord && classRecord.name, | ||
display_name: classRecord && classRecord.display_name, | ||
description: classRecord && classRecord.description, | ||
}; | ||
setData({ ...data, isLoading: false, initialValues }); | ||
} | ||
}, [classRecord]); | ||
|
||
const onSubmit = (values) => { | ||
miqSparkleOn(); | ||
|
||
const params = { | ||
action: isEdit ? 'edit' : 'create', | ||
name: values.name, | ||
display_name: values.display_name, | ||
description: values.description, | ||
old_data: data.initialValues, | ||
button: classRecord.id ? 'save' : 'add', | ||
}; | ||
|
||
const request = isEdit | ||
? http.post(`/miq_ae_class/class_update/${classRecord.id}`, params) | ||
: http.post(`/miq_ae_class/class_update/`, params); | ||
|
||
request | ||
.then((response) => { | ||
if (response.status === 200) { | ||
const confirmation = isEdit ? __(`Class "%s" was saved`) : __(`Class "%s" was added`); | ||
const message = sprintf(confirmation, values.name); | ||
miqRedirectBack(message, 'success', '/miq_ae_class/explorer'); | ||
} else { | ||
miqSparkleOff(); | ||
miqFlash('error', response.error); | ||
} | ||
}) | ||
.catch(miqSparkleOff); | ||
}; | ||
|
||
const onCancel = () => { | ||
const confirmation = classRecord.id ? __(`Edit of Class "%s" cancelled by the user`) | ||
: __(`Add of new Class was cancelled by the user`); | ||
const message = sprintf(confirmation, classRecord.name); | ||
miqRedirectBack(message, 'warning', '/miq_ae_class/explorer'); | ||
}; | ||
|
||
const customValidatorMapper = { | ||
customValidatorForNameField: () => (value) => { | ||
if (!value) { | ||
return __('Required'); | ||
} | ||
if (!value.match('^[a-zA-Z0-9_.-]*$')) { | ||
return __('Name may contain only alphanumeric and _ . - characters'); | ||
} | ||
return false; | ||
}, | ||
}; | ||
|
||
return (!data.isLoading | ||
? ( | ||
<div className="dialog-provision-form"> | ||
<MiqFormRenderer | ||
schema={createSchema(formattedFqname)} | ||
initialValues={data.initialValues} | ||
validatorMapper={customValidatorMapper} | ||
onSubmit={onSubmit} | ||
onCancel={onCancel} | ||
canReset={!!classRecord.id} | ||
validate={() => {}} | ||
FormTemplate={(props) => <FormTemplate {...props} recId={classRecord.id} />} | ||
/> | ||
</div> | ||
) : null | ||
); | ||
}; | ||
|
||
const FormTemplate = ({ | ||
formFields, recId, | ||
}) => { | ||
const { | ||
handleSubmit, onReset, onCancel, getState, | ||
} = useFormApi(); | ||
const { valid, pristine } = getState(); | ||
const submitLabel = !!recId ? __('Save') : __('Add'); | ||
return ( | ||
<form onSubmit={handleSubmit}> | ||
{formFields} | ||
<FormSpy> | ||
{() => ( | ||
<div className="custom-button-wrapper"> | ||
{ !recId | ||
? ( | ||
<Button | ||
disabled={!valid} | ||
kind="primary" | ||
className="btnRight" | ||
type="submit" | ||
variant="contained" | ||
> | ||
{submitLabel} | ||
</Button> | ||
) : ( | ||
<Button | ||
disabled={!valid || pristine} | ||
kind="primary" | ||
className="btnRight" | ||
type="submit" | ||
variant="contained" | ||
> | ||
{submitLabel} | ||
</Button> | ||
)} | ||
{!!recId | ||
? ( | ||
<Button | ||
disabled={pristine} | ||
kind="secondary" | ||
className="btnRight" | ||
variant="contained" | ||
onClick={onReset} | ||
type="button" | ||
> | ||
{ __('Reset')} | ||
</Button> | ||
) : null} | ||
|
||
<Button variant="contained" type="button" onClick={onCancel} kind="secondary"> | ||
{ __('Cancel')} | ||
</Button> | ||
</div> | ||
)} | ||
</FormSpy> | ||
</form> | ||
); | ||
}; | ||
|
||
MiqAeClass.propTypes = { | ||
classRecord: PropTypes.shape({ | ||
id: PropTypes.number, | ||
name: PropTypes.string, | ||
display_name: PropTypes.string, | ||
description: PropTypes.string, | ||
}), | ||
fqname: PropTypes.string.isRequired, | ||
}; | ||
|
||
MiqAeClass.defaultProps = { | ||
classRecord: undefined, | ||
}; | ||
|
||
FormTemplate.propTypes = { | ||
formFields: PropTypes.arrayOf( | ||
PropTypes.shape({ id: PropTypes.number }), | ||
PropTypes.shape({ name: PropTypes.string }), | ||
PropTypes.shape({ display_name: PropTypes.string }), | ||
PropTypes.shape({ description: PropTypes.string }), | ||
), | ||
recId: PropTypes.number, | ||
}; | ||
|
||
FormTemplate.defaultProps = { | ||
formFields: undefined, | ||
recId: undefined, | ||
}; | ||
|
||
export default MiqAeClass; |
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
5 changes: 5 additions & 0 deletions
5
app/javascript/spec/miq-ae-class-form/__snapshots__/miq-ae-class-form.spec.js.snap
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,5 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`MiqAeClass Form Component should render add class form correctly 1`] = `""`; | ||
|
||
exports[`MiqAeClass Form Component should render edit class form correctly 1`] = `""`; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this implying that the fqname comes back from the API with spaces?
I was actually expecting any client-side formatting code to just be deleted, so this is surprising.
EDIT: Here's what the backend returns, which is why I'm surprised:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was going through the MiqAeClassController and saw this line of code which is creating those spaces.
Not sure if this line was added intentionally. If it's removed, it may affect the Fully Qualified Name displayed during the creation/editing of Domain, Namespace, Class (and potentially other areas). Could you please confirm if it's fine to remove it? @Fryguy