From 15aa5b6d1c77c5a0d5cf87c8ce8953a9cac45ed9 Mon Sep 17 00:00:00 2001 From: ismay Date: Wed, 26 Jul 2023 16:05:39 +0200 Subject: [PATCH] chore: add update queue hook --- .../Forms/SequenceEditFormContainer.js | 4 +-- src/hooks/job-queues/index.js | 1 + src/hooks/job-queues/use-update-job-queue.js | 27 +++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 src/hooks/job-queues/use-update-job-queue.js diff --git a/src/components/Forms/SequenceEditFormContainer.js b/src/components/Forms/SequenceEditFormContainer.js index b74b79608..7d4b85572 100644 --- a/src/components/Forms/SequenceEditFormContainer.js +++ b/src/components/Forms/SequenceEditFormContainer.js @@ -2,7 +2,7 @@ import React from 'react' import PropTypes from 'prop-types' import { ReactFinalForm } from '@dhis2/ui' import history from '../../services/history' -import { useSubmitJobQueue } from '../../hooks/job-queues' +import { useUpdateJobQueue } from '../../hooks/job-queues' import SequenceEditForm from './SequenceEditForm' const { Form } = ReactFinalForm @@ -19,7 +19,7 @@ const SequenceEditFormContainer = ({ sequence }) => { const redirect = () => { history.push('/') } - const [submitJobQueue] = useSubmitJobQueue({ onSuccess: redirect }) + const [submitJobQueue] = useUpdateJobQueue({ onSuccess: redirect }) // Creating an object with just the values we want to use as initial values const initialValues = initialFields.reduce((filtered, key) => { diff --git a/src/hooks/job-queues/index.js b/src/hooks/job-queues/index.js index bddefdda4..beaead6d8 100644 --- a/src/hooks/job-queues/index.js +++ b/src/hooks/job-queues/index.js @@ -1 +1,2 @@ export { default as useSubmitJobQueue } from './use-submit-job-queue' +export { default as useUpdateJobQueue } from './use-update-job-queue' diff --git a/src/hooks/job-queues/use-update-job-queue.js b/src/hooks/job-queues/use-update-job-queue.js new file mode 100644 index 000000000..dbd061841 --- /dev/null +++ b/src/hooks/job-queues/use-update-job-queue.js @@ -0,0 +1,27 @@ +import { useDataEngine } from '@dhis2/app-runtime' +import formatError from '../../services/format-error' + +const createMutation = (name) => ({ + resource: `scheduler/queues/${name}`, + type: 'update', + data: ({ queue }) => queue, +}) + +const useUpdateJobQueue = ({ onSuccess } = {}) => { + const engine = useDataEngine() + const submitJobQueue = (queue) => { + const mutation = createMutation(queue.name) + return engine + .mutate(mutation, { variables: { queue } }) + .then(() => { + if (onSuccess) { + onSuccess() + } + }) + .catch((error) => formatError(error)) + } + + return [submitJobQueue] +} + +export default useUpdateJobQueue