refactor OrganizationTeams/OrganizationTeamsList

This commit is contained in:
Keith Grant
2019-04-05 10:52:52 -04:00
parent c2a223bbb4
commit 89ecddf662
3 changed files with 119 additions and 131 deletions

View File

@@ -159,10 +159,9 @@ class Organization extends Component {
path="/organizations/:id/teams"
render={() => (
<OrganizationTeams
id={Number(match.params.id)}
searchString={location.search}
api={api}
match={match}
location={location}
history={history}
/>
)}
/>

View File

@@ -1,34 +1,93 @@
import React from 'react';
import PropTypes from 'prop-types';
import { withRouter } from 'react-router-dom';
import OrganizationTeamsList from '../../components/OrganizationTeamsList';
import { parseQueryString } from '../../../../qs';
const DEFAULT_QUERY_PARAMS = {
page: 1,
page_size: 5,
order_by: 'name',
};
class OrganizationTeams extends React.Component {
constructor (props) {
super(props);
this.readOrganizationTeamsList = this.readOrganizationTeamsList.bind(this);
this.state = {
isLoading: false,
error: null,
itemCount: 0,
teams: [],
};
}
readOrganizationTeamsList (id, params) {
const { api } = this.props;
return api.readOrganizationTeamsList(id, params);
componentDidMount () {
this.readOrganizationTeamsList();
}
componentDidUpdate (prevProps) {
const { location } = this.props;
if (location !== prevProps.location) {
this.readOrganizationTeamsList();
}
}
getQueryParams () {
const { searchString } = this.props;
const searchParams = parseQueryString(searchString.substring(1));
return {
...DEFAULT_QUERY_PARAMS,
...searchParams,
};
}
async readOrganizationTeamsList () {
const { api, id } = this.props;
const params = this.getQueryParams();
this.setState({ isLoading: true });
try {
const {
data: { count = 0, results = [] },
} = await api.readOrganizationTeamsList(id, params);
this.setState({
itemCount: count,
teams: results,
isLoading: false,
});
} catch (error) {
this.setState({
error,
isLoading: false
});
}
}
render () {
const {
location,
match,
history,
} = this.props;
const { teams, itemCount, isLoading } = this.state;
if (isLoading) {
return <div>Loading...</div>;
}
return (
<OrganizationTeamsList
onReadTeamsList={this.readOrganizationTeamsList}
match={match}
location={location}
history={history}
teams={teams}
itemCount={itemCount}
queryParams={this.getQueryParams()}
/>
);
}
}
export default OrganizationTeams;
OrganizationTeams.propTypes = {
id: PropTypes.number.isRequired,
searchString: PropTypes.string.isRequired,
api: PropTypes.shape().isRequired, // TODO: remove?
};
export { OrganizationTeams as _OrganizationTeams };
export default withRouter(OrganizationTeams);