Skip to content

Commit

Permalink
fix(form): fix editing style object
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Oct 24, 2023
1 parent 9b26d47 commit 1d6d1ed
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions src/pages/dataElements/edit/createJsonPatchOperations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,42 @@ import { JsonPatchOperation } from '../../../types'
import { DataElement } from '../../../types/generated'
import type { FormValues } from '../form'

type DataElementKey = keyof DataElement

interface FormatFormValuesArgs {
dataElement: DataElement
dirtyFields: { [name: string]: boolean }
values: FormValues
}

const sanitizeDirtyValueKeys = (keys: DataElementKey[]) => {
const attributeValuesDirty = keys.find((key) =>
key.startsWith('attributeValues')
const sanitizeDirtyValueKeys = (keys: string[]) => {
// these are removed from the dirtyKeys
// attributeValues is an array in the form, thus fields will be attributeValues[0] etc
// style.code should post to style, not style.code, because it's a complex object
const keyStartsWithToRemove = ['attributeValues', 'style'] as const
const shouldInclude = Object.fromEntries(
keys.map((key) => [key, false])
) as Record<(typeof keyStartsWithToRemove)[number], boolean>

const keysWithout = keys.filter(
(key) =>
!keyStartsWithToRemove.some((keyToRemove) => {
const shouldRemove = key.startsWith(keyToRemove)
if (shouldRemove) {
shouldInclude[keyToRemove] = true
}
return shouldRemove
})
)

if (!attributeValuesDirty) {
// no difference
if (keysWithout.length === keys.length) {
return keys
}

return [
...keys.filter((key) => !key.startsWith('attributeValues')),
'attributeValues' as DataElementKey,
]
const keysToInclude = Object.entries(shouldInclude)
.filter(([, val]) => val)
.map(([key]) => key)

return keysWithout.concat(keysToInclude)
}

export function createJsonPatchOperations({
Expand All @@ -39,9 +54,8 @@ export function createJsonPatchOperations({
),
}

const dirtyFieldsKeys = Object.keys(dirtyFields) as DataElementKey[]
const adjustedDirtyFieldsKeys: DataElementKey[] =
sanitizeDirtyValueKeys(dirtyFieldsKeys)
const dirtyFieldsKeys = Object.keys(dirtyFields)
const adjustedDirtyFieldsKeys = sanitizeDirtyValueKeys(dirtyFieldsKeys)

return adjustedDirtyFieldsKeys.map((name) => ({
op: get(name, dataElement) ? 'replace' : 'add',
Expand Down

0 comments on commit 1d6d1ed

Please sign in to comment.