-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(de name shortname inputs): add uniqueness validation
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { useDataQuery } from '@dhis2/app-runtime' | ||
import i18n from '@dhis2/d2-i18n' | ||
import { useMemo } from 'react' | ||
import { Pager } from '../../../types/generated' | ||
|
||
const HAS_FIELD_VALUE_QUERY = { | ||
dataElements: { | ||
resource: 'dataElements', | ||
params: (variables: Record<string, string>) => ({ | ||
pageSize: 1, | ||
fields: '', | ||
filter: [`${variables.field}:eq:${variables.value}`], | ||
}), | ||
}, | ||
} | ||
|
||
interface QueryResponse { | ||
dataElements: { | ||
pager: Pager | ||
} | ||
} | ||
|
||
export function useHasFieldValue(field: string) { | ||
const queryResult = useDataQuery<QueryResponse>(HAS_FIELD_VALUE_QUERY, { | ||
lazy: true, | ||
}) | ||
|
||
return useMemo( | ||
() => ({ | ||
...queryResult, | ||
refetch: (value: string) => { | ||
if (!value) { | ||
return | ||
} | ||
|
||
return queryResult.refetch({ field, value }).then( | ||
// We don't have access to app-runtime's internal type `JsonMap`, | ||
// so we have to ignore the type error | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
(data: QueryResponse) => | ||
data.dataElements.pager.total | ||
? i18n.t( | ||
'This {{field}} already exists, please choose antoher one', | ||
{ field } | ||
) | ||
: undefined | ||
) | ||
}, | ||
}), | ||
[field, queryResult] | ||
) | ||
} |