Skip to content

Commit

Permalink
refactor: pass refetch to runjobmodal instead of using hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ismay committed Apr 12, 2023
1 parent 99643bc commit 274e596
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 21 deletions.
9 changes: 6 additions & 3 deletions src/components/JobTable/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ViewJobAction from './ViewJobAction'
import RunJobAction from './RunJobAction'
import DeleteJobAction from './DeleteJobAction'

const Actions = ({ id, configurable, enabled }) => (
const Actions = ({ id, configurable, enabled, refetch }) => (
<DropdownButton
small
component={
Expand All @@ -17,7 +17,9 @@ const Actions = ({ id, configurable, enabled }) => (
) : (
<ViewJobAction id={id} />
)}
{configurable && <RunJobAction enabled={enabled} id={id} />}
{configurable && (
<RunJobAction enabled={enabled} id={id} refetch={refetch} />
)}
{configurable && <DeleteJobAction id={id} />}
</FlyoutMenu>
}
Expand All @@ -30,10 +32,11 @@ Actions.defaultProps = {
configurable: false,
}

const { string, bool } = PropTypes
const { string, bool, func } = PropTypes

Actions.propTypes = {
id: string.isRequired,
refetch: func.isRequired,
configurable: bool,
enabled: bool,
}
Expand Down
9 changes: 6 additions & 3 deletions src/components/JobTable/JobTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import i18n from '@dhis2/d2-i18n'
import { PropTypes } from '@dhis2/prop-types'
import JobTableRow from './JobTableRow'

const JobTable = ({ jobs }) => (
const JobTable = ({ jobs, refetch }) => (
<Table>
<TableHead>
<TableRowHead>
Expand All @@ -31,16 +31,19 @@ const JobTable = ({ jobs }) => (
<TableCell>{i18n.t('No jobs to display')}</TableCell>
</TableRow>
) : (
jobs.map((job) => <JobTableRow key={job.id} job={job} />)
jobs.map((job) => (
<JobTableRow key={job.id} job={job} refetch={refetch} />
))
)}
</TableBody>
</Table>
)

const { arrayOf, object } = PropTypes
const { arrayOf, object, func } = PropTypes

JobTable.propTypes = {
jobs: arrayOf(object).isRequired,
refetch: func.isRequired,
}

export default JobTable
11 changes: 9 additions & 2 deletions src/components/JobTable/JobTableRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const JobTableRow = ({
enabled,
configurable,
},
refetch,
}) => (
<TableRow>
<TableCell role="rowheader">{name}</TableCell>
Expand All @@ -41,12 +42,17 @@ const JobTableRow = ({
/>
</TableCell>
<TableCell>
<Actions id={id} enabled={enabled} configurable={configurable} />
<Actions
id={id}
enabled={enabled}
configurable={configurable}
refetch={refetch}
/>
</TableCell>
</TableRow>
)

const { shape, string, bool, number } = PropTypes
const { shape, string, bool, number, func } = PropTypes

JobTableRow.propTypes = {
job: shape({
Expand All @@ -59,6 +65,7 @@ JobTableRow.propTypes = {
delay: number,
nextExecutionTime: string,
}).isRequired,
refetch: func.isRequired,
}

export default JobTableRow
6 changes: 4 additions & 2 deletions src/components/JobTable/RunJobAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MenuItem } from '@dhis2/ui'
import i18n from '@dhis2/d2-i18n'
import { RunJobModal } from '../Modal'

const RunJobAction = ({ id, enabled }) => {
const RunJobAction = ({ id, enabled, refetch }) => {
const [showModal, setShowModal] = useState(false)

return (
Expand All @@ -24,16 +24,18 @@ const RunJobAction = ({ id, enabled }) => {
/* istanbul ignore next */
() => setShowModal(false)
}
refetch={refetch}
/>
)}
</React.Fragment>
)
}

const { string, bool } = PropTypes
const { string, bool, func } = PropTypes

RunJobAction.propTypes = {
id: string.isRequired,
refetch: func.isRequired,
enabled: bool,
}

Expand Down
5 changes: 2 additions & 3 deletions src/components/Modal/RunJobModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ import {
NoticeBox,
} from '@dhis2/ui'
import i18n from '@dhis2/d2-i18n'
import { useJobSchedules } from '../../hooks/job-schedules'

const RunJobModal = ({ id, hideModal }) => {
const RunJobModal = ({ id, hideModal, refetch }) => {
const [mutation] = useState({
resource: `jobConfigurations/${id}/execute`,
type: 'create',
})
const { refetch } = useJobSchedules()
const [runJob, { loading, error }] = useDataMutation(mutation, {
onComplete: () => {
hideModal()
Expand Down Expand Up @@ -64,6 +62,7 @@ const { func, string } = PropTypes
RunJobModal.propTypes = {
hideModal: func.isRequired,
id: string.isRequired,
refetch: func.isRequired,
}

export default RunJobModal
8 changes: 2 additions & 6 deletions src/components/Modal/RunJobModal.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react'
import { shallow, mount } from 'enzyme'
import { useDataMutation } from '@dhis2/app-runtime'
import { StoreContext } from '../Store'
import RunJobModal from './RunJobModal'

jest.mock('@dhis2/app-runtime', () => ({
Expand Down Expand Up @@ -75,12 +74,9 @@ describe('<RunJobModal>', () => {
const props = {
id: 'id',
hideModal: hideModalSpy,
refetch: refetchSpy,
}
const wrapper = mount(
<StoreContext.Provider value={{ refetchJobs: refetchSpy }}>
<RunJobModal {...props} />
</StoreContext.Provider>
)
const wrapper = mount(<RunJobModal {...props} />)

wrapper
.find('button')
Expand Down
4 changes: 2 additions & 2 deletions src/pages/JobList/JobList.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const infoLink =
const JobList = () => {
const [jobFilter, setJobFilter] = useJobFilter()
const [showSystemJobs, setShowSystemJobs] = useShowSystemJobs()
const { data, loading, error } = useJobSchedules()
const { data, loading, error, refetch } = useJobSchedules()

if (loading) {
return <Spinner />
Expand Down Expand Up @@ -78,7 +78,7 @@ const JobList = () => {
<LinkButton to="/add">{i18n.t('New job')}</LinkButton>
</div>
</div>
<JobTable jobs={jobs} />
<JobTable jobs={jobs} refetch={refetch} />
</Card>
</React.Fragment>
)
Expand Down

0 comments on commit 274e596

Please sign in to comment.