Fetch new inventory on location change

This commit is contained in:
Marliana Lara 2020-01-16 22:55:47 -05:00
parent d35eba8afb
commit d549c217bb
No known key found for this signature in database
GPG Key ID: 38C73B40DFA809EE
6 changed files with 40 additions and 30 deletions

View File

@ -183,10 +183,6 @@
z-index: 20;
}
.at-u-textRight {
text-align: right;
}
//
// AlertModal styles
//

View File

@ -85,22 +85,16 @@ class Inventories extends Component {
path={`${match.path}/smart_inventory/add`}
render={() => <SmartInventoryAdd />}
/>
<Route
path={`${match.path}/inventory/:id`}
render={({ match: newRouteMatch }) => (
<Config>
{({ me }) => (
<Inventory
history={history}
location={location}
setBreadcrumb={this.setBreadCrumbConfig}
me={me || {}}
match={newRouteMatch}
/>
)}
</Config>
)}
/>
<Route path={`${match.path}/inventory/:id`}>
<Config>
{({ me }) => (
<Inventory
setBreadcrumb={this.setBreadCrumbConfig}
me={me || {}}
/>
)}
</Config>
</Route>
<Route
path={`${match.path}/smart_inventory/:id`}
render={({ match: newRouteMatch }) => (

View File

@ -2,7 +2,15 @@ import React, { useEffect, useState } from 'react';
import { t } from '@lingui/macro';
import { withI18n } from '@lingui/react';
import { Card, PageSection } from '@patternfly/react-core';
import { Switch, Route, Redirect, withRouter, Link } from 'react-router-dom';
import {
Switch,
Route,
Redirect,
Link,
useHistory,
useLocation,
useRouteMatch,
} from 'react-router-dom';
import { TabbedCardHeader } from '@components/Card';
import CardCloseButton from '@components/CardCloseButton';
import ContentError from '@components/ContentError';
@ -17,10 +25,15 @@ import InventorySources from './InventorySources';
import { InventoriesAPI } from '@api';
import InventoryEdit from './InventoryEdit';
function Inventory({ history, i18n, location, match, setBreadcrumb }) {
function Inventory({ i18n, setBreadcrumb }) {
const [contentError, setContentError] = useState(null);
const [hasContentLoading, setHasContentLoading] = useState(true);
const [inventory, setInventory] = useState(null);
const history = useHistory();
const location = useLocation();
const match = useRouteMatch({
path: '/inventories/inventory/:id',
});
useEffect(() => {
async function fetchData() {
@ -36,7 +49,7 @@ function Inventory({ history, i18n, location, match, setBreadcrumb }) {
}
fetchData();
}, [match.params.id, setBreadcrumb]);
}, [match.params.id, location.pathname, setBreadcrumb]);
const tabsArray = [
{ name: i18n._(t`Details`), link: `${match.url}/details`, id: 0 },
@ -132,7 +145,6 @@ function Inventory({ history, i18n, location, match, setBreadcrumb }) {
<InventoryGroups
location={location}
match={match}
history={history}
setBreadcrumb={setBreadcrumb}
inventory={inventory}
/>
@ -178,4 +190,4 @@ function Inventory({ history, i18n, location, match, setBreadcrumb }) {
}
export { Inventory as _Inventory };
export default withI18n()(withRouter(Inventory));
export default withI18n()(Inventory);

View File

@ -7,6 +7,13 @@ import mockInventory from './shared/data.inventory.json';
import Inventory from './Inventory';
jest.mock('@api');
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useRouteMatch: () => ({
url: '/inventories/1',
params: { id: 1 },
}),
}));
InventoriesAPI.readDetail.mockResolvedValue({
data: mockInventory,
@ -17,9 +24,7 @@ describe('<Inventory />', () => {
test('initially renders succesfully', async () => {
await act(async () => {
wrapper = mountWithContexts(
<Inventory setBreadcrumb={() => {}} match={{ params: { id: 1 } }} />
);
wrapper = mountWithContexts(<Inventory setBreadcrumb={() => {}} />);
});
await waitForElement(wrapper, 'ContentLoading', el => el.length === 0);
await waitForElement(wrapper, '.pf-c-tabs__item', el => el.length === 6);

View File

@ -90,12 +90,15 @@ function InventoryEdit({ inventory }) {
setError(err);
}
};
if (contentLoading) {
return <ContentLoading />;
}
if (error) {
return <ContentError />;
}
return (
<CardBody>
<InventoryForm

View File

@ -221,8 +221,8 @@ class InventoriesList extends Component {
inventory={inventory}
detailUrl={
inventory.kind === 'smart'
? `${match.url}/smart_inventory/${inventory.id}`
: `${match.url}/inventory/${inventory.id}`
? `${match.url}/smart_inventory/${inventory.id}/details`
: `${match.url}/inventory/${inventory.id}/details`
}
onSelect={() => this.handleSelect(inventory)}
isSelected={selected.some(row => row.id === inventory.id)}