1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-01 08:21:15 +03:00

Fixes InvGroupForm submission bug.

The inventory id now comes through useParams instead of through props.
Also updates tests to reflect those changes
This commit is contained in:
Alex Corey 2020-01-23 10:50:23 -05:00
parent 46e530ceeb
commit 0a8fe4d812
4 changed files with 19 additions and 35 deletions

View File

@ -1,28 +1,28 @@
import React, { useState, useEffect } from 'react';
import React, { useState } from 'react';
import { withI18n } from '@lingui/react';
import { withRouter } from 'react-router-dom';
import { useHistory, useParams } from 'react-router-dom';
import { GroupsAPI } from '@api';
import { Card } from '@patternfly/react-core';
import InventoryGroupForm from '../shared/InventoryGroupForm';
function InventoryGroupsAdd({ history, inventory, setBreadcrumb }) {
function InventoryGroupsAdd() {
const [error, setError] = useState(null);
useEffect(() => setBreadcrumb(inventory), [inventory, setBreadcrumb]);
const { id } = useParams();
const history = useHistory();
const handleSubmit = async values => {
values.inventory = inventory.id;
values.inventory = id;
try {
const { data } = await GroupsAPI.create(values);
history.push(`/inventories/inventory/${inventory.id}/groups/${data.id}`);
history.push(`/inventories/inventory/${id}/groups/${data.id}`);
} catch (err) {
setError(err);
}
};
const handleCancel = () => {
history.push(`/inventories/inventory/${inventory.id}/groups`);
history.push(`/inventories/inventory/${id}/groups`);
};
return (
@ -35,5 +35,5 @@ function InventoryGroupsAdd({ history, inventory, setBreadcrumb }) {
</Card>
);
}
export default withI18n()(withRouter(InventoryGroupsAdd));
export default withI18n()(InventoryGroupsAdd);
export { InventoryGroupsAdd as _InventoryGroupsAdd };

View File

@ -21,9 +21,7 @@ describe('<InventoryGroupAdd />', () => {
wrapper = mountWithContexts(
<Route
path="/inventories/inventory/:id/groups/add"
component={() => (
<InventoryGroupAdd setBreadcrumb={() => {}} inventory={{ id: 1 }} />
)}
component={() => <InventoryGroupAdd />}
/>,
{
context: {

View File

@ -1,28 +1,26 @@
import React, { useState } from 'react';
import { withI18n } from '@lingui/react';
import { withRouter } from 'react-router-dom';
import { useParams, useHistory } from 'react-router-dom';
import { GroupsAPI } from '@api';
import InventoryGroupForm from '../shared/InventoryGroupForm';
function InventoryGroupEdit({ history, inventoryGroup, inventory, match }) {
function InventoryGroupEdit({ inventoryGroup }) {
const [error, setError] = useState(null);
const { id, groupId } = useParams();
const history = useHistory();
const handleSubmit = async values => {
try {
await GroupsAPI.update(match.params.groupId, values);
history.push(
`/inventories/inventory/${inventory.id}/groups/${inventoryGroup.id}`
);
await GroupsAPI.update(groupId, values);
history.push(`/inventories/inventory/${id}/groups/${groupId}`);
} catch (err) {
setError(err);
}
};
const handleCancel = () => {
history.push(
`/inventories/inventory/${inventory.id}/groups/${inventoryGroup.id}`
);
history.push(`/inventories/inventory/${id}/groups/${groupId}`);
};
return (
@ -34,5 +32,5 @@ function InventoryGroupEdit({ history, inventoryGroup, inventory, match }) {
/>
);
}
export default withI18n()(withRouter(InventoryGroupEdit));
export default withI18n()(InventoryGroupEdit);
export { InventoryGroupEdit as _InventoryGroupEdit };

View File

@ -26,24 +26,12 @@ describe('<InventoryGroupEdit />', () => {
wrapper = mountWithContexts(
<Route
path="/inventories/inventory/:id/groups/:groupId/edit"
component={() => (
<InventoryGroupEdit
setBreadcrumb={() => {}}
inventory={{ id: 1 }}
inventoryGroup={{ id: 2 }}
/>
)}
component={() => <InventoryGroupEdit inventoryGroup={{ id: 2 }} />}
/>,
{
context: {
router: {
history,
route: {
match: {
params: { groupId: 13 },
},
location: history.location,
},
},
},
}