Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add indicator list type list view #433

Merged
merged 10 commits into from
Nov 25, 2024
10 changes: 6 additions & 4 deletions src/components/form/FormBase.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NoticeBox } from '@dhis2/ui'
// import { NoticeBox } from '@dhis2/ui'
import React, { useMemo } from 'react'
Chisomchima marked this conversation as resolved.
Show resolved Hide resolved
import { FormProps, Form as ReactFinalForm } from 'react-final-form'
import {
Expand All @@ -10,6 +10,8 @@ import { useCustomAttributesQuery } from './attributes'

type MaybeModelWithAttributes = {
id?: string
name?: string
factor?: string
attributeValues?: PartialAttributeValue[] | undefined
Chisomchima marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -44,9 +46,9 @@ export function FormBase<TInitialValues extends MaybeModelWithAttributes>({
}
}, [customAttributes.data, initialValues, includeAttributes])

if (customAttributes.error) {
return <NoticeBox error title="Failed to load custom attributes" />
Chisomchima marked this conversation as resolved.
Show resolved Hide resolved
}
Chisomchima marked this conversation as resolved.
Show resolved Hide resolved
// if (customAttributes.error) {
// return <NoticeBox error title="Failed to load custom attributes" />
// }

Chisomchima marked this conversation as resolved.
Show resolved Hide resolved
if (!initialValuesWithAttributes || customAttributes.loading) {
return <LoadingSpinner />
Expand Down
47 changes: 47 additions & 0 deletions src/pages/indicatorTypes/Edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React from 'react'
import { useQuery } from 'react-query'
import { useParams } from 'react-router-dom'
import { DefaultNewFormContents } from '../../components/form/DefaultFormContents'
import { FormBase } from '../../components/form/FormBase'
import { SECTIONS_MAP, useOnSubmitEdit } from '../../lib'
import { useBoundResourceQueryFn } from '../../lib/query/useBoundQueryFn'
import { IndicatorType, PickWithFieldFilters } from '../../types/models'
import { validate } from './form'
import { IndicatorTypesFormFields } from './form/IndicatorTypesFormFields'

const fieldFilters = ['name', 'factor'] as const

export type IndicatorTypesFormValues = PickWithFieldFilters<
IndicatorType,
typeof fieldFilters
> & { id: string }

export const Component = () => {
const section = SECTIONS_MAP.indicatorType
const queryFn = useBoundResourceQueryFn()
const modelId = useParams().id as string

const query = {
resource: 'indicatorTypes',
id: modelId,
params: {
fields: fieldFilters.concat(),
},
}
const IndicatorTypeQuery = useQuery({
Chisomchima marked this conversation as resolved.
Show resolved Hide resolved
queryKey: [query],
queryFn: queryFn<IndicatorTypesFormValues>,
})
return (
<FormBase
onSubmit={useOnSubmitEdit({ section, modelId })}
section={section}
initialValues={IndicatorTypeQuery.data as IndicatorTypesFormValues}
Chisomchima marked this conversation as resolved.
Show resolved Hide resolved
validate={validate}
>
Chisomchima marked this conversation as resolved.
Show resolved Hide resolved
<DefaultNewFormContents section={section}>
<IndicatorTypesFormFields />
</DefaultNewFormContents>
</FormBase>
)
}
4 changes: 4 additions & 0 deletions src/pages/indicatorTypes/List.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import React from 'react'
import { DefaultSectionList } from '../DefaultSectionList'

export const Component = () => <DefaultSectionList />
23 changes: 23 additions & 0 deletions src/pages/indicatorTypes/New.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react'
import { DefaultNewFormContents } from '../../components/form/DefaultFormContents'
import { FormBase } from '../../components/form/FormBase'
import { SECTIONS_MAP, useOnSubmitNew } from '../../lib'
import { validate } from './form'
import { IndicatorTypesFormFields } from './form/IndicatorTypesFormFields'
import { initialValues } from './form/IndicatorTypesSchema'

const section = SECTIONS_MAP.indicatorType

export const Component = () => {
return (
<FormBase
initialValues={initialValues}
onSubmit={useOnSubmitNew({ section })}
validate={validate}
>
<DefaultNewFormContents section={section}>
<IndicatorTypesFormFields />
</DefaultNewFormContents>
</FormBase>
)
}
48 changes: 48 additions & 0 deletions src/pages/indicatorTypes/form/IndicatorTypesFormFields.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import i18n from '@dhis2/d2-i18n'
import { InputFieldFF } from '@dhis2/ui'
import React from 'react'
import { Field } from 'react-final-form'
import {
CustomAttributesSection,
StandardFormField,
StandardFormSection,
StandardFormSectionTitle,
StandardFormSectionDescription,
} from '../../../components'

export const IndicatorTypesFormFields = () => {
return (
<>
<StandardFormSection>
<StandardFormSectionTitle>
{i18n.t('Basic information')}
</StandardFormSectionTitle>
<StandardFormSectionDescription>
{i18n.t(
'Set up the basic information for this Indicator Type.'
)}
</StandardFormSectionDescription>

<StandardFormField>
<Field
Chisomchima marked this conversation as resolved.
Show resolved Hide resolved
name="name"
component={InputFieldFF}
label={i18n.t('Name')}
required
/>
</StandardFormField>

<StandardFormField>
<Field
name="factor"
type="number"
component={InputFieldFF}
label={i18n.t('Factor')}
required
/>
</StandardFormField>
</StandardFormSection>
<CustomAttributesSection />
</>
)
}
16 changes: 16 additions & 0 deletions src/pages/indicatorTypes/form/IndicatorTypesSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { z } from 'zod'
import { getDefaults, createFormValidate, modelFormSchemas } from '../../../lib'

const { identifiable } = modelFormSchemas

export const IndicatorSchema = identifiable.extend({
name: z.string().min(1, 'Name is required'),
Chisomchima marked this conversation as resolved.
Show resolved Hide resolved
factor: z
Chisomchima marked this conversation as resolved.
Show resolved Hide resolved
.string()
.min(1, 'Factor is required')
.regex(/^\d+(\.\d+)?$/, 'Factor must be a valid number'),
})

export const initialValues = getDefaults(IndicatorSchema)

export const validate = createFormValidate(IndicatorSchema)
1 change: 1 addition & 0 deletions src/pages/indicatorTypes/form/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { IndicatorSchema, validate } from './IndicatorTypesSchema'
2 changes: 1 addition & 1 deletion src/types/generated/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4776,7 +4776,7 @@ export type IndicatorType = {
created: string
createdBy: User
displayName: string
factor: number
factor: string
Chisomchima marked this conversation as resolved.
Show resolved Hide resolved
favorite: boolean
favorites: Array<string>
href: string
Expand Down
Loading