Skip to content

Commit

Permalink
chore: add update queue hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ismay committed Jul 26, 2023
1 parent 234afea commit 15aa5b6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/components/Forms/SequenceEditFormContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) => {
Expand Down
1 change: 1 addition & 0 deletions src/hooks/job-queues/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default as useSubmitJobQueue } from './use-submit-job-queue'
export { default as useUpdateJobQueue } from './use-update-job-queue'
27 changes: 27 additions & 0 deletions src/hooks/job-queues/use-update-job-queue.js
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 15aa5b6

Please sign in to comment.