From c3460322be3e20bfc9a4a3f454de817f8230a760 Mon Sep 17 00:00:00 2001 From: ismay Date: Tue, 17 Oct 2023 16:48:44 +0200 Subject: [PATCH] refactor: add queue-specific actions --- src/components/JobTable/DeleteQueueAction.js | 41 +++++++++++++++++++ src/components/JobTable/EditQueueAction.js | 21 ++++++++++ .../JobTable/{Actions.js => JobActions.js} | 8 ++-- .../{Actions.test.js => JobActions.test.js} | 8 ++-- src/components/JobTable/JobTable.js | 19 +++++++-- src/components/JobTable/JobTableRow.js | 21 ++++++---- src/components/JobTable/QueueActions.js | 29 +++++++++++++ 7 files changed, 129 insertions(+), 18 deletions(-) create mode 100644 src/components/JobTable/DeleteQueueAction.js create mode 100644 src/components/JobTable/EditQueueAction.js rename src/components/JobTable/{Actions.js => JobActions.js} (87%) rename src/components/JobTable/{Actions.test.js => JobActions.test.js} (50%) create mode 100644 src/components/JobTable/QueueActions.js diff --git a/src/components/JobTable/DeleteQueueAction.js b/src/components/JobTable/DeleteQueueAction.js new file mode 100644 index 000000000..71e4640b3 --- /dev/null +++ b/src/components/JobTable/DeleteQueueAction.js @@ -0,0 +1,41 @@ +import React, { useState } from 'react' +import PropTypes from 'prop-types' +import { MenuItem } from '@dhis2/ui' +import i18n from '@dhis2/d2-i18n' +import { DeleteQueueModal } from '../Modal' + +const DeleteQueueAction = ({ name, onSuccess }) => { + const [showModal, setShowModal] = useState(false) + + return ( + + { + setShowModal(true) + }} + label={i18n.t('Delete')} + /> + {showModal && ( + setShowModal(false) + } + onSuccess={onSuccess} + /> + )} + + ) +} + +const { string, func } = PropTypes + +DeleteQueueAction.propTypes = { + name: string.isRequired, + onSuccess: func.isRequired, +} + +export default DeleteQueueAction diff --git a/src/components/JobTable/EditQueueAction.js b/src/components/JobTable/EditQueueAction.js new file mode 100644 index 000000000..07382a128 --- /dev/null +++ b/src/components/JobTable/EditQueueAction.js @@ -0,0 +1,21 @@ +import React from 'react' +import PropTypes from 'prop-types' +import { MenuItem } from '@dhis2/ui' +import i18n from '@dhis2/d2-i18n' +import history from '../../services/history' + +const EditQueueAction = ({ name }) => ( + history.push(`/queue/${name}`)} + label={i18n.t('Edit')} + /> +) + +const { string } = PropTypes + +EditQueueAction.propTypes = { + name: string.isRequired, +} + +export default EditQueueAction diff --git a/src/components/JobTable/Actions.js b/src/components/JobTable/JobActions.js similarity index 87% rename from src/components/JobTable/Actions.js rename to src/components/JobTable/JobActions.js index c3322e8f3..8b4785eb8 100644 --- a/src/components/JobTable/Actions.js +++ b/src/components/JobTable/JobActions.js @@ -7,7 +7,7 @@ import ViewJobAction from './ViewJobAction' import RunJobAction from './RunJobAction' import DeleteJobAction from './DeleteJobAction' -const Actions = ({ id, configurable, enabled, refetch }) => ( +const JobActions = ({ id, configurable, enabled, refetch }) => ( ( ) -Actions.defaultProps = { +JobActions.defaultProps = { configurable: false, } const { string, bool, func } = PropTypes -Actions.propTypes = { +JobActions.propTypes = { id: string.isRequired, refetch: func.isRequired, configurable: bool, enabled: bool, } -export default Actions +export default JobActions diff --git a/src/components/JobTable/Actions.test.js b/src/components/JobTable/JobActions.test.js similarity index 50% rename from src/components/JobTable/Actions.test.js rename to src/components/JobTable/JobActions.test.js index 5dd7c4a35..5dc882cdf 100644 --- a/src/components/JobTable/Actions.test.js +++ b/src/components/JobTable/JobActions.test.js @@ -1,13 +1,13 @@ import React from 'react' import { shallow } from 'enzyme' -import Actions from './Actions' +import JobActions from './JobActions' -describe('', () => { +describe('', () => { it('renders without errors for configurable jobs', () => { - shallow( {}} />) + shallow( {}} />) }) it('renders without errors for non configurable jobs', () => { - shallow( {}} />) + shallow( {}} />) }) }) diff --git a/src/components/JobTable/JobTable.js b/src/components/JobTable/JobTable.js index f4edd4191..32a9a4135 100644 --- a/src/components/JobTable/JobTable.js +++ b/src/components/JobTable/JobTable.js @@ -31,9 +31,22 @@ const JobTable = ({ jobs, refetch }) => ( {i18n.t('No jobs to display')} ) : ( - jobs.map((job) => ( - - )) + jobs.map((job) => { + /** + * This will fail if job.sequence.length does not exist. That should not happen, + * so failing with an error when it does is appropriate. + */ + const isJob = job.sequence.length === 1 + + return ( + + ) + }) )} diff --git a/src/components/JobTable/JobTableRow.js b/src/components/JobTable/JobTableRow.js index 434f1e8ad..dd6e5fad6 100644 --- a/src/components/JobTable/JobTableRow.js +++ b/src/components/JobTable/JobTableRow.js @@ -3,7 +3,8 @@ import PropTypes from 'prop-types' import { TableRow, TableCell } from '@dhis2/ui' import { jobTypesMap } from '../../services/server-translations' import { ToggleJobSwitch } from '../Switches' -import Actions from './Actions' +import JobActions from './JobActions' +import QueueActions from './QueueActions' import Status from './Status' import NextRun from './NextRun' import Schedule from './Schedule' @@ -21,6 +22,7 @@ const JobTableRow = ({ configurable, }, refetch, + isJob, }) => ( {name} @@ -43,12 +45,16 @@ const JobTableRow = ({ /> - + {isJob ? ( + + ) : ( + + )} ) @@ -56,6 +62,7 @@ const JobTableRow = ({ const { shape, string, bool, number, func } = PropTypes JobTableRow.propTypes = { + isJob: bool.isRequired, job: shape({ name: string.isRequired, enabled: bool.isRequired, diff --git a/src/components/JobTable/QueueActions.js b/src/components/JobTable/QueueActions.js new file mode 100644 index 000000000..5ff911ffa --- /dev/null +++ b/src/components/JobTable/QueueActions.js @@ -0,0 +1,29 @@ +import React from 'react' +import PropTypes from 'prop-types' +import { FlyoutMenu, DropdownButton } from '@dhis2/ui' +import i18n from '@dhis2/d2-i18n' +import EditQueueAction from './EditQueueAction' +import DeleteQueueAction from './DeleteQueueAction' + +const QueueActions = ({ name, refetch }) => ( + + + + + } + > + {i18n.t('Actions')} + +) + +const { string, func } = PropTypes + +QueueActions.propTypes = { + name: string.isRequired, + refetch: func.isRequired, +} + +export default QueueActions