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
Replaces angular ownership form with data-driven-form #5046
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f412d11
Added babel-plugin-transform-class-properties to jest.
Hyperkid123 0e3a363
Disabled no-extra-boolean-cast eslint rule.
Hyperkid123 250dfcd
Changed default content-type of http.post to json.
Hyperkid123 754760d
Added default button labels to data-driven-form.
Hyperkid123 8a2b939
Added set-service-ownership-form React component.
Hyperkid123 30db930
Removed ownership_form_controller code.
Hyperkid123 376e4e5
Added controller to name to request URLs for ownership form.
Hyperkid123 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
77 changes: 0 additions & 77 deletions
77
app/assets/javascripts/controllers/ownership/ownership_form_controller.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
57 changes: 57 additions & 0 deletions
57
app/javascript/components/set-service-ownership-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,57 @@ | ||
import React, { Component } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Grid } from 'patternfly-react'; | ||
import MiqFormRenderer from '../../forms/data-driven-form'; | ||
import { http } from '../../http_api'; | ||
import { cleanVirtualDom } from '../../miq-component/helpers'; | ||
import createSchema from './schema'; | ||
|
||
class SetServiceOwnershipForm extends Component { | ||
state = { | ||
initialValues: {}, | ||
} | ||
|
||
componentDidMount() { | ||
cleanVirtualDom(); | ||
const { ownershipIds } = this.props; | ||
this.loadInitialData(ownershipIds); | ||
} | ||
|
||
loadInitialData = objectIds => | ||
http.post('/service/ownership_form_fields', { object_ids: objectIds }) | ||
.then(data => this.setState({ initialValues: data })) | ||
|
||
|
||
handleSubmit = (values, objectIds, url) => miqAjaxButton(url, { | ||
objectIds, | ||
...values, | ||
}) | ||
|
||
render() { | ||
const { groupOptions, ownerOptions, ownershipIds } = this.props; | ||
const { initialValues } = this.state; | ||
const cancelUrl = '/service/ownership_update/?button=cancel'; | ||
const submitUrl = '/service/ownership_update/?button=save'; | ||
|
||
return ( | ||
<Grid fluid id="set-ownership-form"> | ||
<MiqFormRenderer | ||
initialValues={initialValues} | ||
schema={createSchema(ownerOptions, groupOptions)} | ||
onSubmit={values => this.handleSubmit(values, ownershipIds, submitUrl)} | ||
onReset={() => add_flash(__('All changes have been reset'), 'warn')} | ||
onCancel={() => miqAjaxButton(cancelUrl)} | ||
canReset | ||
/> | ||
</Grid> | ||
); | ||
} | ||
} | ||
|
||
SetServiceOwnershipForm.propTypes = { | ||
ownershipIds: PropTypes.arrayOf(PropTypes.string).isRequired, | ||
groupOptions: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.string)).isRequired, | ||
ownerOptions: PropTypes.arrayOf(PropTypes.arrayOf(PropTypes.string)).isRequired, | ||
}; | ||
|
||
export default SetServiceOwnershipForm; |
25 changes: 25 additions & 0 deletions
25
app/javascript/components/set-service-ownership-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,25 @@ | ||
import { componentTypes } from '@data-driven-forms/react-form-renderer'; | ||
|
||
const createSchema = (ownerOptions, groupOptions) => ({ | ||
fields: [{ | ||
component: componentTypes.SELECT_COMPONENT, | ||
name: 'user', | ||
id: 'user_name', | ||
label: __('Select an Owner:'), | ||
options: ownerOptions.map(([label, value]) => ({ | ||
label, | ||
value, | ||
})), | ||
}, { | ||
component: componentTypes.SELECT_COMPONENT, | ||
name: 'group', | ||
id: 'group_name', | ||
label: __('Select a Group:'), | ||
options: groupOptions.map(([label, value]) => ({ | ||
label, | ||
value, | ||
})), | ||
}], | ||
}); | ||
|
||
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
81 changes: 81 additions & 0 deletions
81
app/javascript/spec/set-service-ownership-form/set-service-ownership-form.spec.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,81 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import FormRenderer from '@data-driven-forms/react-form-renderer'; | ||
import SetServiceOwnershipForm from '../../components/set-service-ownership-form'; | ||
import createSchema from '../../components/set-service-ownership-form/schema'; | ||
import { http } from '../../http_api'; | ||
|
||
describe('Set service ownership form component', () => { | ||
let initialProps; | ||
const httpSpy = jest.spyOn(http, 'post'); | ||
const submitSpy = jest.spyOn(window, 'miqAjaxButton'); | ||
const flashSpy = jest.spyOn(window, 'add_flash'); | ||
|
||
beforeEach(() => { | ||
initialProps = { | ||
ownershipIds: ['123456'], | ||
groupOptions: [['Foo', '1'], ['Bar', '2']], | ||
ownerOptions: [['Baz', '3'], ['Quxx', '4']], | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
submitSpy.mockReset(); | ||
flashSpy.mockReset(); | ||
}); | ||
|
||
it('should correctly map group and owner options ', () => { | ||
const expectedResult = [ | ||
expect.objectContaining({ | ||
options: [{ | ||
value: '3', | ||
label: 'Baz', | ||
}, { | ||
value: '4', | ||
label: 'Quxx', | ||
}], | ||
}), | ||
expect.objectContaining({ | ||
options: [{ | ||
value: '1', | ||
label: 'Foo', | ||
}, { | ||
value: '2', | ||
label: 'Bar', | ||
}], | ||
}), | ||
]; | ||
const { fields } = createSchema(initialProps.ownerOptions, initialProps.groupOptions); | ||
expect(fields).toEqual(expectedResult); | ||
}); | ||
|
||
// TO DO replace with actual request mock | ||
it('should request initialForm values after mount', () => { | ||
mount(<SetServiceOwnershipForm {...initialProps} />); | ||
expect(httpSpy).toHaveBeenCalledWith('/service/ownership_form_fields', { object_ids: ['123456'] }); | ||
}); | ||
|
||
it('should send correct data on save', () => { | ||
const wrapper = mount(<SetServiceOwnershipForm {...initialProps} />); | ||
const Form = wrapper.find(FormRenderer).childAt(0); | ||
Form.instance().form.change('user', 'foo'); | ||
wrapper.find('button').at(0).simulate('click'); | ||
expect(submitSpy).toHaveBeenCalledWith('/service/ownership_update/?button=save', { objectIds: ['123456'], user: 'foo' }); | ||
}); | ||
|
||
it('should send correct data on cancel', () => { | ||
const wrapper = mount(<SetServiceOwnershipForm {...initialProps} />); | ||
wrapper.find('button').last().simulate('click'); | ||
expect(submitSpy).toHaveBeenCalledWith('/service/ownership_update/?button=cancel'); | ||
}); | ||
|
||
it('should reset formValues and add flash messages on reset click', () => { | ||
const wrapper = mount(<SetServiceOwnershipForm {...initialProps} />); | ||
const Form = wrapper.find(FormRenderer).childAt(0); | ||
Form.instance().form.change('user', 'foo'); | ||
expect(Form.instance().form.getState().values).toEqual({ user: 'foo' }); | ||
wrapper.find('button').at(1).simulate('click'); | ||
expect(Form.instance().form.getState().values).toEqual({}); | ||
expect(flashSpy).toHaveBeenCalledWith(expect.any(String), 'warn'); | ||
}); | ||
}); |
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.
Sorry, all those
/service/..
are wrong :(. (also goes forcancelUrl
andsubmitUrl
)We need to be using the current controller, otherwise this fails for all the screens changing ownership for non-services.
Looks like the code is expecting the controller to be one of:
(
get_class_from_controller_param
)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.
You can use
/${ManageIQ.controller}/ownership_form_fields
if it helps.