Skip to content

Commit

Permalink
chore: add selected values to existing queueables
Browse files Browse the repository at this point in the history
  • Loading branch information
ismay committed Jul 19, 2023
1 parent 7817038 commit 3f74f67
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React from 'react'
import { ReactFinalForm, CircularLoader, NoticeBox } from '@dhis2/ui'
import i18n from '@dhis2/d2-i18n'
import { jobTypesMap } from '../../../services/server-translations'
import { useQueueables } from '../../../hooks/queueables'
import SequenceTransfer from './SequenceTransfer'

const { Field } = ReactFinalForm

// The key under which this field will be sent to the backend
const FIELD_NAME = 'sequence'
const hasEnoughJobs = (value) =>
value?.length > 1 ? undefined : i18n.t('Please select at least two jobs')

const SequenceOrderEditField = ({ selectedValues }) => {
const { loading, error, data } = useQueueables()

if (loading) {
return <CircularLoader />
}

if (error) {
return (
<NoticeBox
error
title={i18n.t(
'Something went wrong whilst fetching the queueable jobs'
)}
/>
)
}

// The selected values aren't part of the queueables, so we need to add them
const options = data.concat(selectedValues).map(({ name, id, type }) => ({
label: name,
value: id,
type: jobTypesMap[type],
}))

return (
<Field
name={FIELD_NAME}
component={SequenceTransfer}
options={options}
validate={hasEnoughJobs}
/>
)
}

export default SequenceOrderEditField
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ const { Field } = ReactFinalForm

// The key under which this field will be sent to the backend
const FIELD_NAME = 'sequence'
const initialValue = []
const hasEnoughJobs = (value) =>
value.length > 1 ? undefined : i18n.t('Please select at least two jobs')
value?.length > 1 ? undefined : i18n.t('Please select at least two jobs')

const SequenceOrderField = () => {
const { loading, error, data } = useQueueables()
Expand Down Expand Up @@ -42,7 +41,6 @@ const SequenceOrderField = () => {
name={FIELD_NAME}
component={SequenceTransfer}
options={options}
initialValue={initialValue}
validate={hasEnoughJobs}
/>
)
Expand Down
3 changes: 2 additions & 1 deletion src/components/FormFields/SequenceOrderField/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { default } from './SequenceOrderField'
export { default as SequenceOrderField} from './SequenceOrderField'
export { default as SequenceOrderEditField} from './SequenceOrderEditField'
5 changes: 4 additions & 1 deletion src/components/FormFields/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ export { default as JobTypeField } from './JobTypeField'
export { default as NameField } from './NameField'
export { default as ParameterFields } from './ParameterFields'
export { default as ScheduleField } from './ScheduleField'
export { default as SequenceOrderField } from './SequenceOrderField'
export {
SequenceOrderField,
SequenceOrderEditField,
} from './SequenceOrderField'

export { fieldNames }
5 changes: 3 additions & 2 deletions src/components/Forms/SequenceEditForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import i18n from '@dhis2/d2-i18n'
import { Button, CircularLoader, Box, ReactFinalForm } from '@dhis2/ui'
import { DiscardFormButton } from '../Buttons'
import { FormErrorBox } from '../FormErrorBox'
import { NameField, CronField, SequenceOrderField } from '../FormFields'
import { NameField, CronField, SequenceOrderEditField } from '../FormFields'
import styles from './SequenceEditForm.module.css'

const { useForm } = ReactFinalForm
Expand All @@ -16,6 +16,7 @@ const SequenceEditForm = ({
submitError,
hasSubmitErrors,
setIsPristine,
selectedValues
}) => {
const { subscribe } = useForm()

Expand Down Expand Up @@ -44,7 +45,7 @@ const SequenceEditForm = ({
<CronField />
</Box>
<Box marginTop="16px">
<SequenceOrderField />
<SequenceOrderEditField selectedValues={selectedValues} />
</Box>
{hasSubmitErrors && (
<Box marginTop="32px" maxWidth="600px">
Expand Down
6 changes: 3 additions & 3 deletions src/components/Forms/SequenceEditFormContainer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react'
import PropTypes from 'prop-types'
import { ReactFinalForm } from '@dhis2/ui'
import { useParams } from 'react-router-dom'
import history from '../../services/history'
import { useSubmitJobQueue } from '../../hooks/job-queues'
import SequenceEditForm from './SequenceEditForm'
Expand All @@ -21,7 +20,6 @@ const initialFields = [
]

const SequenceEditFormContainer = ({ sequence, setIsPristine }) => {
const { id } = useParams()
const redirect = () => {
history.push('/')
}
Expand All @@ -33,6 +31,8 @@ const SequenceEditFormContainer = ({ sequence, setIsPristine }) => {
return filtered
}, {})

initialValues.sequence = initialValues.sequence.map(({ id }) => id)

/**
* destroyOnUnregister is enabled so that dynamic fields will be unregistered
* when they're removed from the form, for instance when the jobType changes.
Expand All @@ -41,10 +41,10 @@ const SequenceEditFormContainer = ({ sequence, setIsPristine }) => {
<Form
component={SequenceEditForm}
destroyOnUnregister
id={id}
initialValues={initialValues}
onSubmit={submitJobQueue}
setIsPristine={setIsPristine}
selectedValues={sequence.sequence}
/>
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Routes/Routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Routes = () => (
<Route path="/view/:id" component={JobView} />
<Route path="/add" component={JobAdd} />
<Route path="/add-sequence" component={SequenceAdd} />
<Route path="/edit-sequence" component={SequenceEdit} />
<Route path="/edit-sequence/:id" component={SequenceEdit} />
</Router>
)

Expand Down
23 changes: 21 additions & 2 deletions src/pages/SequenceEdit/SequenceEdit.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
import React, { useState } from 'react'
import { Card, IconInfo16 } from '@dhis2/ui'
import { Card, IconInfo16 , NoticeBox } from '@dhis2/ui'
import { useParams } from 'react-router-dom'
import i18n from '@dhis2/d2-i18n'
import { Spinner } from '../../components/Spinner'
import { DiscardFormButton } from '../../components/Buttons'
import { SequenceEditFormContainer } from '../../components/Forms'
import { useJobScheduleById } from '../../hooks/job-schedules'
import styles from './SequenceEdit.module.css'

const SequenceEdit = () => {
const [isPristine, setIsPristine] = useState(true)
const { id } = useParams()
const { data, fetching, error } = useJobScheduleById(id)

if (fetching) {
return <Spinner />
}

if (error) {
return (
<NoticeBox error title={i18n.t('Could not load requested schedule')}>
{i18n.t(
'Something went wrong whilst loading the requested schedule. Make sure it has not been deleted and try refreshing the page.'
)}
</NoticeBox>
)
}

return (
<React.Fragment>
Expand Down Expand Up @@ -36,7 +55,7 @@ const SequenceEdit = () => {
)}
</span>
</header>
<SequenceEditFormContainer setIsPristine={setIsPristine} />
<SequenceEditFormContainer sequence={data} setIsPristine={setIsPristine} />
</Card>
</React.Fragment>
)
Expand Down

0 comments on commit 3f74f67

Please sign in to comment.