Skip to content

Commit

Permalink
refactor: simplify creation of initialValues
Browse files Browse the repository at this point in the history
  • Loading branch information
ismay committed Aug 3, 2023
1 parent d852cec commit d1ecf3b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 33 deletions.
26 changes: 8 additions & 18 deletions src/components/Forms/JobEditFormContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@ import JobEditForm from './JobEditForm'

const { Form } = ReactFinalForm

/**
* The fields we need for the initialValues for our form fields. Since we use
* these values to set the initial values in final-form, if we wouldn't filter
* them we'd end up submitting way more data than we intend to.
*/

const initialFields = [
'cronExpression',
'delay',
'jobParameters',
'jobType',
'name',
]

const JobEditFormContainer = ({ job }) => {
const { id } = useParams()
const redirect = () => {
Expand All @@ -30,10 +16,14 @@ const JobEditFormContainer = ({ job }) => {
const [updateJob] = useUpdateJob({ id, onSuccess: redirect })

// Creating an object with just the values we want to use as initial values
const initialValues = initialFields.reduce((filtered, key) => {
filtered[key] = job[key]
return filtered
}, {})
const { cronExpression, delay, jobParameters, jobType, name } = job
const initialValues = {
cronExpression,
delay,
jobParameters,
jobType,
name,
}

return (
<Form
Expand Down
22 changes: 7 additions & 15 deletions src/components/Forms/SequenceEditFormContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,19 @@ import SequenceEditForm from './SequenceEditForm'

const { Form } = ReactFinalForm

/**
* The fields we need for the initialValues for our form fields. Since we use
* these values to set the initial values in final-form, if we wouldn't filter
* them we'd end up submitting way more data than we intend to.
*/

const initialFields = ['cronExpression', 'sequence', 'name']

const SequenceEditFormContainer = ({ sequence }) => {
const redirect = () => {
history.push('/')
}
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) => {
filtered[key] = sequence[key]
return filtered
}, {})

initialValues.sequence = initialValues.sequence.map(({ id }) => id)
// Create an object with only the values we want to use as initial values
const { cronExpression, name } = sequence
const initialValues = {
cronExpression,
sequence: sequence.sequence.map(({ id }) => id),
name,
}

return (
<Form
Expand Down

0 comments on commit d1ecf3b

Please sign in to comment.