Skip to content

Commit

Permalink
feat: add run queue menu item
Browse files Browse the repository at this point in the history
  • Loading branch information
ismay authored and ismay committed Feb 21, 2024
1 parent ae56ba1 commit 4e06e28
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 10 deletions.
10 changes: 8 additions & 2 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2024-02-15T11:19:20.108Z\n"
"PO-Revision-Date: 2024-02-15T11:19:20.108Z\n"
"POT-Creation-Date: 2024-02-21T09:54:31.937Z\n"
"PO-Revision-Date: 2024-02-21T09:54:31.937Z\n"

msgid "Something went wrong"
msgstr "Something went wrong"
Expand Down Expand Up @@ -252,9 +252,15 @@ msgstr "Yes, discard changes"
msgid "Error running job"
msgstr "Error running job"

msgid "Error running queue"
msgstr "Error running queue"

msgid "Are you sure you want to run this job?"
msgstr "Are you sure you want to run this job?"

msgid "Are you sure you want to run this queue?"
msgstr "Are you sure you want to run this queue?"

msgid "Run"
msgstr "Run"

Expand Down
12 changes: 10 additions & 2 deletions src/components/JobTable/QueueActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ import PropTypes from 'prop-types'
import { FlyoutMenu, DropdownButton } from '@dhis2/ui'
import i18n from '@dhis2/d2-i18n'
import EditQueueAction from './EditQueueAction'
import RunQueueAction from './RunQueueAction'
import DeleteQueueAction from './DeleteQueueAction'

const QueueActions = ({ name, refetch }) => (
const QueueActions = ({ name, refetch, id, enabled }) => (
<DropdownButton
small
component={
<FlyoutMenu>
<EditQueueAction name={name} />
<RunQueueAction
enabled={enabled}
id={id}
onComplete={refetch}
/>
<DeleteQueueAction name={name} onSuccess={refetch} />
</FlyoutMenu>
}
Expand All @@ -19,9 +25,11 @@ const QueueActions = ({ name, refetch }) => (
</DropdownButton>
)

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

QueueActions.propTypes = {
enabled: bool.isRequired,
id: string.isRequired,
name: string.isRequired,
refetch: func.isRequired,
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/JobTable/QueueActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import QueueActions from './QueueActions'

describe('<QueueActions>', () => {
it('renders without errors', () => {
shallow(<QueueActions name="1" refetch={() => {}} />)
shallow(<QueueActions name="1" refetch={() => {}} id="1" enabled />)
})
})
7 changes: 6 additions & 1 deletion src/components/JobTable/QueueTableRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ const QueueTableRow = ({
/>
</TableCell>
<TableCell>
<QueueActions name={name} refetch={refetch} />
<QueueActions
name={name}
refetch={refetch}
id={id}
enabled={enabled}
/>
</TableCell>
</TableRow>
{showJobs
Expand Down
43 changes: 43 additions & 0 deletions src/components/JobTable/RunQueueAction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import React, { useState } from 'react'
import PropTypes from 'prop-types'
import { MenuItem } from '@dhis2/ui'
import i18n from '@dhis2/d2-i18n'
import { RunJobModal } from '../Modal'

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

return (
<React.Fragment>
<MenuItem
dense
onClick={() => {
setShowModal(true)
}}
disabled={!enabled}
label={i18n.t('Run manually')}
/>
{showModal && (
<RunJobModal
id={id}
hideModal={
/* istanbul ignore next */
() => setShowModal(false)
}
onComplete={onComplete}
isQueue
/>
)}
</React.Fragment>
)
}

const { string, bool, func } = PropTypes

RunQueueAction.propTypes = {
id: string.isRequired,
onComplete: func.isRequired,
enabled: bool,
}

export default RunQueueAction
23 changes: 23 additions & 0 deletions src/components/JobTable/RunQueueAction.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react'
import { mount } from 'enzyme'
import RunQueueAction from './RunQueueAction'

describe('<RunQueueAction>', () => {
it('shows the modal when MenuItem is clicked and the queue is enabled', () => {
const wrapper = mount(
<RunQueueAction id="id" enabled onComplete={() => {}} />
)

expect(wrapper.find('RunJobModal')).toHaveLength(0)
wrapper.find('a').simulate('click')
expect(wrapper.find('RunJobModal')).toHaveLength(1)
})

it('does not show the modal when MenuItem is clicked and the queue is disabled', () => {
const wrapper = mount(<RunQueueAction id="id" onComplete={() => {}} />)

expect(wrapper.find('RunJobModal')).toHaveLength(0)
wrapper.find('a').simulate('click')
expect(wrapper.find('RunJobModal')).toHaveLength(0)
})
})
27 changes: 23 additions & 4 deletions src/components/Modal/RunJobModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,23 @@ import {
} from '@dhis2/ui'
import i18n from '@dhis2/d2-i18n'

const RunJobModal = ({ id, hideModal, onComplete }) => {
const messages = {
error: {
job: i18n.t('Error running job'),
queue: i18n.t('Error running queue'),
},
confirm: {
job: i18n.t('Are you sure you want to run this job?'),
queue: i18n.t('Are you sure you want to run this queue?'),
},
}

/**
* This modal can be used to trigger a job or a queue. To start
* a queue, pass the id of the first job of that queue.
*/

const RunJobModal = ({ id, hideModal, onComplete, isQueue }) => {
const [mutation] = useState({
resource: `jobConfigurations/${id}/execute`,
type: 'create',
Expand All @@ -22,16 +38,18 @@ const RunJobModal = ({ id, hideModal, onComplete }) => {
onComplete()
},
})
const errorTitle = isQueue ? messages.error.queue : messages.error.job
const confirmation = isQueue ? messages.confirm.queue : messages.confirm.job

return (
<Modal open small onClose={hideModal}>
<ModalContent>
{error && (
<NoticeBox error title={i18n.t('Error running job')}>
<NoticeBox error title={errorTitle}>
{error.message}
</NoticeBox>
)}
<p>{i18n.t('Are you sure you want to run this job?')}</p>
<p>{confirmation}</p>
</ModalContent>
<ModalActions>
<ButtonStrip end>
Expand All @@ -57,12 +75,13 @@ const RunJobModal = ({ id, hideModal, onComplete }) => {
)
}

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

RunJobModal.propTypes = {
hideModal: func.isRequired,
id: string.isRequired,
onComplete: func.isRequired,
isQueue: bool,
}

export default RunJobModal

0 comments on commit 4e06e28

Please sign in to comment.