update ScheduleList api read to single prop

This commit is contained in:
John Mitchell
2020-03-02 09:22:32 -05:00
parent 989ef3538e
commit c1c382a941
6 changed files with 35 additions and 21 deletions

View File

@@ -20,7 +20,7 @@ const QS_CONFIG = getQSConfig('schedule', {
order_by: 'unified_job_template__polymorphic_ctype__model', order_by: 'unified_job_template__polymorphic_ctype__model',
}); });
function ScheduleList({ i18n, apiModel, resource }) { function ScheduleList({ i18n, loadSchedules }) {
const [selected, setSelected] = useState([]); const [selected, setSelected] = useState([]);
const location = useLocation(); const location = useLocation();
@@ -33,17 +33,12 @@ function ScheduleList({ i18n, apiModel, resource }) {
} = useRequest( } = useRequest(
useCallback(async () => { useCallback(async () => {
const params = parseQueryString(QS_CONFIG, location.search); const params = parseQueryString(QS_CONFIG, location.search);
const response = apiModel const response = loadSchedules(params);
? apiModel.readScheduleList(resource.id, params)
: SchedulesAPI.read(params);
const { const {
data: { count, results }, data: { count, results },
} = await response; } = await response;
return { return { itemCount: count, schedules: results };
itemCount: count, }, [location, loadSchedules]),
schedules: results,
};
}, [location, apiModel, resource]),
{ {
schedules: [], schedules: [],
itemCount: 0, itemCount: 0,

View File

@@ -22,8 +22,11 @@ describe('ScheduleList', () => {
describe('read call successful', () => { describe('read call successful', () => {
beforeAll(async () => { beforeAll(async () => {
SchedulesAPI.read.mockResolvedValue({ data: mockSchedules }); SchedulesAPI.read.mockResolvedValue({ data: mockSchedules });
const loadSchedules = params => SchedulesAPI.read(params);
await act(async () => { await act(async () => {
wrapper = mountWithContexts(<ScheduleList />); wrapper = mountWithContexts(
<ScheduleList loadSchedules={loadSchedules} />
);
}); });
wrapper.update(); wrapper.update();
}); });

View File

@@ -30,6 +30,7 @@ class Project extends Component {
}; };
this.loadProject = this.loadProject.bind(this); this.loadProject = this.loadProject.bind(this);
this.loadProjectAndRoles = this.loadProjectAndRoles.bind(this); this.loadProjectAndRoles = this.loadProjectAndRoles.bind(this);
this.loadSchedules = this.loadSchedules.bind(this);
} }
async componentDidMount() { async componentDidMount() {
@@ -103,6 +104,11 @@ class Project extends Component {
} }
} }
loadSchedules(params) {
const { project } = this.state;
return ProjectsAPI.readScheduleList(project.id, params);
}
render() { render() {
const { location, match, me, i18n } = this.props; const { location, match, me, i18n } = this.props;
@@ -235,7 +241,7 @@ class Project extends Component {
<Route <Route
path="/projects/:id/schedules" path="/projects/:id/schedules"
render={() => ( render={() => (
<ScheduleList resource={project} apiModel={ProjectsAPI} /> <ScheduleList loadSchedules={this.loadSchedules} />
)} )}
/> />
)} )}

View File

@@ -5,8 +5,13 @@ import { t } from '@lingui/macro';
import Breadcrumbs from '@components/Breadcrumbs'; import Breadcrumbs from '@components/Breadcrumbs';
import ScheduleList from '@components/ScheduleList'; import ScheduleList from '@components/ScheduleList';
import { SchedulesAPI } from '@api';
function Schedules({ i18n }) { function Schedules({ i18n }) {
const loadSchedules = params => {
return SchedulesAPI.read(params);
};
return ( return (
<> <>
<Breadcrumbs <Breadcrumbs
@@ -16,7 +21,7 @@ function Schedules({ i18n }) {
/> />
<Switch> <Switch>
<Route path="/schedules"> <Route path="/schedules">
<ScheduleList /> <ScheduleList loadSchedules={loadSchedules} />
</Route> </Route>
</Switch> </Switch>
</> </>

View File

@@ -28,6 +28,7 @@ class Template extends Component {
}; };
this.loadTemplate = this.loadTemplate.bind(this); this.loadTemplate = this.loadTemplate.bind(this);
this.loadTemplateAndRoles = this.loadTemplateAndRoles.bind(this); this.loadTemplateAndRoles = this.loadTemplateAndRoles.bind(this);
this.loadSchedules = this.loadSchedules.bind(this);
} }
async componentDidMount() { async componentDidMount() {
@@ -82,6 +83,11 @@ class Template extends Component {
} }
} }
loadSchedules(params) {
const { template } = this.state;
return JobTemplatesAPI.readScheduleList(template.id, params);
}
render() { render() {
const { i18n, location, match, me } = this.props; const { i18n, location, match, me } = this.props;
const { const {
@@ -221,9 +227,7 @@ class Template extends Component {
{template && ( {template && (
<Route <Route
path="/templates/:templateType/:id/schedules" path="/templates/:templateType/:id/schedules"
render={() => ( render={() => <ScheduleList loadSchedules={this.loadSchedules} />}
<ScheduleList resource={template} apiModel={JobTemplatesAPI} />
)}
/> />
)} )}
<Route <Route

View File

@@ -25,6 +25,7 @@ class WorkflowJobTemplate extends Component {
template: null, template: null,
}; };
this.loadTemplate = this.loadTemplate.bind(this); this.loadTemplate = this.loadTemplate.bind(this);
this.loadSchedules = this.loadSchedules.bind(this);
} }
async componentDidMount() { async componentDidMount() {
@@ -71,6 +72,11 @@ class WorkflowJobTemplate extends Component {
} }
} }
loadSchedules(params) {
const { template } = this.state;
return WorkflowJobTemplatesAPI.readScheduleList(template.id, params);
}
render() { render() {
const { i18n, location, match } = this.props; const { i18n, location, match } = this.props;
const { const {
@@ -173,12 +179,7 @@ class WorkflowJobTemplate extends Component {
{template && ( {template && (
<Route <Route
path="/templates/:templateType/:id/schedules" path="/templates/:templateType/:id/schedules"
render={() => ( render={() => <ScheduleList loadSchedules={this.loadSchedules} />}
<ScheduleList
resource={template}
apiModel={WorkflowJobTemplatesAPI}
/>
)}
/> />
)} )}
<Route <Route