Skip to content

Commit

Permalink
fix(de form fields): ignore "itself" when validating uniqueness
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammer5 committed Oct 30, 2023
1 parent 903a9a9 commit 489626b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
15 changes: 13 additions & 2 deletions src/pages/dataElements/form/fields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import React, { useRef } from 'react'
import { Field as FieldRFF, useField } from 'react-final-form'
import { useHref } from 'react-router'
import { useParams } from 'react-router-dom'
import {
AggregationLevelMultiSelect,
ColorAndIconPicker,
Expand All @@ -26,8 +27,13 @@ import { EditableFieldWrapper } from './EditableFieldWrapper'
import { useIsFieldValueUnique } from './useIsFieldValueUnique'

export function NameField() {
const params = useParams()
const dataElementId = params.id as string
const checkIsValueTaken = useIsFieldValueUnique({
field: 'name',
id: dataElementId,
})
const { meta } = useField('name')
const checkIsValueTaken = useIsFieldValueUnique('name')

return (
<FieldRFF
Expand All @@ -52,8 +58,13 @@ export function NameField() {
}

export function ShortNameField() {
const params = useParams()
const dataElementId = params.id as string
const checkIsValueTaken = useIsFieldValueUnique({
field: 'shortName',
id: dataElementId,
})
const { meta } = useField('shortName')
const checkIsValueTaken = useIsFieldValueUnique('shortName')

return (
<FieldRFF
Expand Down
26 changes: 18 additions & 8 deletions src/pages/dataElements/form/useIsFieldValueUnique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@ import { Pager } from '../../../types/generated'
const HAS_FIELD_VALUE_QUERY = {
dataElements: {
resource: 'dataElements',
params: (variables: Record<string, string>) => ({
pageSize: 1,
fields: 'id',
filter: [`${variables.field}:eq:${variables.value}`],
}),
params: (variables: Record<string, string>) => {
const filter = [`${variables.field}:eq:${variables.value}`]

if (variables.id) {
filter.push(`id:ne:${variables.id}`)
}

return { pageSize: 1, fields: 'id', filter }
},
},
}

Expand All @@ -22,7 +26,13 @@ interface QueryResponse {
}
}

export function useIsFieldValueUnique(field: string) {
export function useIsFieldValueUnique({
field,
id,
}: {
field: string
id: string
}) {
const engine = useDataEngine()

const validate = useMemo(
Expand All @@ -33,7 +43,7 @@ export function useIsFieldValueUnique(field: string) {
}

const data = (await engine.query(HAS_FIELD_VALUE_QUERY, {
variables: { field, value },
variables: { field, value, id },
})) as unknown as QueryResponse

if (data.dataElements.pager.total > 0) {
Expand All @@ -42,7 +52,7 @@ export function useIsFieldValueUnique(field: string) {
)
}
}),
[field, engine]
[field, engine, id]
)

return useDebouncedCallback(validate, 200, { leading: true })
Expand Down

0 comments on commit 489626b

Please sign in to comment.