Skip to content

Commit

Permalink
chore: refactor location and adminstration area components
Browse files Browse the repository at this point in the history
  • Loading branch information
Nil20 committed Jan 30, 2025
1 parent f44e8aa commit 7370dfe
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import { FileInput } from './inputs/FileInput/FileInput'
import { RadioGroup } from '@client/v2-events/features/events/registered-fields'
import { BulletList } from '@client/v2-events/features/events/registered-fields/BulletList'
import { Checkbox } from '@client/v2-events/features/events/registered-fields/Checkbox'
import { Location } from '@client/v2-events/features/events/registered-fields/Location'
import { AdministrativeArea } from '@client/v2-events/features/events/registered-fields/AdministrativeArea'
import { LocationSearch } from '@client/v2-events/features/events/registered-fields/LocationSearch'
import { Select } from '@client/v2-events/features/events/registered-fields/Select'
import { SelectCountry } from '@client/v2-events/features/events/registered-fields/SelectCountry'
Expand Down Expand Up @@ -275,17 +275,17 @@ const GeneratedInputField = React.memo(
)
}
if (
fieldDefinition.type === 'LOCATION_SEARCH' ||
fieldDefinition.type === 'LOCATION_SEARCH_FACILITIES' ||
fieldDefinition.type === 'LOCATION_SEARCH_OFFICES'
fieldDefinition.type === 'LOCATION' ||
fieldDefinition.type === 'OFFICE' ||
fieldDefinition.type === 'FACILITY'
) {
const resourceMap: Record<
string,
('locations' | 'facilities' | 'offices')[]
> = {
LOCATION_SEARCH: ['locations'],
LOCATION_SEARCH_FACILITIES: ['facilities'],
LOCATION_SEARCH_OFFICES: ['offices']
LOCATION: ['locations'],
FACILITY: ['facilities'],
OFFICE: ['offices']
}

return (
Expand All @@ -298,10 +298,10 @@ const GeneratedInputField = React.memo(
)
}

if (fieldDefinition.type === 'LOCATION') {
if (fieldDefinition.type === 'ADMINISTRATIVE_AREA') {
return (
<InputField {...inputFieldProps}>
<Location
<AdministrativeArea
{...fieldDefinition}
value={value as LocationFieldValue}
setFieldValue={setFieldValue}
Expand Down
17 changes: 10 additions & 7 deletions packages/client/src/v2-events/components/forms/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ import {
validate,
DateFieldValue,
TextFieldValue,
RadioGroupFieldValue,
FileFieldValue
RadioGroupFieldValue
} from '@opencrvs/commons/client'
import {
CheckboxFieldValue,
Expand All @@ -41,7 +40,7 @@ import {
} from '@client/v2-events/features/events/registered-fields'
import { selectFieldToString } from '@client/v2-events/features/events/registered-fields/Select'
import { selectCountryFieldToString } from '@client/v2-events/features/events/registered-fields/SelectCountry'
import { ILocation } from '@client/v2-events/features/events/registered-fields/Location'
import { ILocation } from '@client/v2-events/features/events/registered-fields/AdministrativeArea'
import { checkboxToString } from '@client/v2-events/features/events/registered-fields/Checkbox'

export function handleInitialValue(
Expand Down Expand Up @@ -118,13 +117,15 @@ const initialValueMapping: Record<FieldType, FieldValue | null> = {
[FieldType.DATE]: INITIAL_DATE_VALUE,
[FieldType.RADIO_GROUP]: INITIAL_RADIO_GROUP_VALUE,
[FieldType.PARAGRAPH]: INITIAL_PARAGRAPH_VALUE,
[FieldType.LOCATION_SEARCH_INPUT]: null,
[FieldType.LOCATION]: null,
[FieldType.FACILITY]: null,
[FieldType.OFFICE]: null,
[FieldType.FILE]: null,
[FieldType.HIDDEN]: null,
[FieldType.BULLET_LIST]: null,
[FieldType.CHECKBOX]: null,
[FieldType.COUNTRY]: null,
[FieldType.LOCATION]: null,
[FieldType.ADMINISTRATIVE_AREA]: null,
[FieldType.SELECT]: null,
[FieldType.PAGE_HEADER]: null,
[FieldType.DIVIDER]: null
Expand Down Expand Up @@ -173,8 +174,10 @@ export function fieldValueToString({
)
case FieldType.COUNTRY:
return selectCountryFieldToString(value as SelectFieldValue, intl)
case FieldType.LOCATION_SEARCH_INPUT:
case FieldType.LOCATION: {
case FieldType.LOCATION:
case FieldType.FACILITY:
case FieldType.OFFICE:
case FieldType.ADMINISTRATIVE_AREA: {
let location
if (_.isString(value)) {
location = locations[value].name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ function useAdminLocations(partOf: string) {
}))
}

export function Location({
export function AdministrativeArea({
setFieldValue,
value,
partOf,
...props
}: FieldProps<'LOCATION'> & {
}: FieldProps<'ADMINISTRATIVE_AREA'> & {
setFieldValue: (name: string, val: string | undefined) => void
partOf: string | null
value?: LocationFieldValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ export function LocationSearch({
value,
searchableResource,
...props
}: FieldProps<
'LOCATION_SEARCH' | 'LOCATION_SEARCH_FACILITIES' | 'LOCATION_SEARCH_OFFICES'
> & {
}: FieldProps<'LOCATION' | 'OFFICE' | 'FACILITY'> & {
setFieldValue: (name: string, val: string | undefined) => void
searchableResource: ('locations' | 'facilities' | 'offices')[]
value?: LocationFieldValue
Expand Down
52 changes: 26 additions & 26 deletions packages/commons/src/events/FieldConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ export const FieldType = {
CHECKBOX: 'CHECKBOX',
SELECT: 'SELECT',
COUNTRY: 'COUNTRY',
LOCATION: 'LOCATION',
ADMINISTRATIVE_AREA: 'ADMINISTRATIVE_AREA',
DIVIDER: 'DIVIDER',
LOCATION_SEARCH: 'LOCATION_SEARCH',
LOCATION_SEARCH_FACILITIES: 'LOCATION_SEARCH_FACILITIES',
LOCATION_SEARCH_OFFICES: 'LOCATION_SEARCH_OFFICES'
LOCATION: 'LOCATION',
FACILITY: 'FACILITY',
OFFICE: 'OFFICE'
} as const

export const fieldTypes = Object.values(FieldType)
Expand All @@ -119,7 +119,7 @@ export interface FieldValueByType {
[FieldType.BULLET_LIST]: BulletListFieldValue
[FieldType.CHECKBOX]: CheckboxFieldValue
[FieldType.COUNTRY]: CountryFieldValue
[FieldType.LOCATION]: LocationFieldValue
[FieldType.ADMINISTRATIVE_AREA]: LocationFieldValue
[FieldType.FILE]: FileFieldValue
[FieldType.SELECT]: SelectFieldValue
}
Expand Down Expand Up @@ -216,7 +216,7 @@ const Country = BaseField.extend({
type: z.literal(FieldType.COUNTRY)
}).describe('Country select field')

const LocationOptions = z.object({
const AdminAreaOptions = z.object({
partOf: z
.object({
$data: z.string()
Expand All @@ -226,21 +226,21 @@ const LocationOptions = z.object({
type: z.enum(['ADMIN_STRUCTURE', 'HEALTH_FACILITY', 'CRVS_OFFICE'])
})

const Location = BaseField.extend({
type: z.literal(FieldType.LOCATION),
options: LocationOptions
}).describe('Location input field')
const AdministrativeArea = BaseField.extend({
type: z.literal(FieldType.ADMINISTRATIVE_AREA),
options: AdminAreaOptions
}).describe('Administrative area input field')

const LocationSearch = BaseField.extend({
type: z.literal(FieldType.LOCATION_SEARCH)
const Location = BaseField.extend({
type: z.literal(FieldType.LOCATION)
})

const LocationSearchFacilities = BaseField.extend({
type: z.literal(FieldType.LOCATION_SEARCH_FACILITIES)
const Facility = BaseField.extend({
type: z.literal(FieldType.FACILITY)
})

const LocationSearchOffices = BaseField.extend({
type: z.literal(FieldType.LOCATION_SEARCH_OFFICES)
const Office = BaseField.extend({
type: z.literal(FieldType.OFFICE)
})

/*
Expand All @@ -261,11 +261,11 @@ export type AllFields =
| typeof Checkbox
| typeof File
| typeof Country
| typeof Location
| typeof AdministrativeArea
| typeof Divider
| typeof LocationSearch
| typeof LocationSearchFacilities
| typeof LocationSearchOffices
| typeof Location
| typeof Facility
| typeof Office

export const FieldConfig = z.discriminatedUnion('type', [
TextField,
Expand All @@ -278,18 +278,18 @@ export const FieldConfig = z.discriminatedUnion('type', [
Checkbox,
File,
Country,
Location,
AdministrativeArea,
Divider,
LocationSearch,
LocationSearchFacilities,
LocationSearchOffices
Location,
Facility,
Office
]) as unknown as z.ZodDiscriminatedUnion<'type', AllFields[]>

export type SelectField = z.infer<typeof Select>
export type LocationField = z.infer<typeof Location>
export type AdministrativeArea = z.infer<typeof AdministrativeArea>
export type FieldConfig = z.infer<typeof FieldConfig>

export type FieldProps<T extends FieldType> = Extract<FieldConfig, { type: T }>
export type SelectOption = z.infer<typeof SelectOption>
export type LocationOptions = z.infer<typeof LocationOptions>
export type AdminAreaOptions = z.infer<typeof AdminAreaOptions>
export type FieldConditional = z.infer<typeof FieldConditional>
6 changes: 3 additions & 3 deletions packages/events/src/service/indexing/indexing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ function getElasticsearchMappingForType(field: FieldConfig) {
case 'SELECT':
case 'COUNTRY':
case 'CHECKBOX':
case 'ADMINISTRATIVE_AREA':
case 'LOCATION':
case 'LOCATION_SEARCH':
case 'LOCATION_SEARCH_FACILITIES':
case 'LOCATION_SEARCH_OFFICES':
case 'FACILITY':
case 'OFFICE':
return { type: 'keyword' }
case 'FILE':
return {
Expand Down

0 comments on commit 7370dfe

Please sign in to comment.