-
Notifications
You must be signed in to change notification settings - Fork 2
feat: [DHIS2-21175] Analytics table hooks form #865
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
Open
henrikmv
wants to merge
10
commits into
hv/feat/DHIS2-21176_AnalyticsTableHooksList
Choose a base branch
from
hv/feat/DHIS2-21175_AnalyticsTableHooksForm
base: hv/feat/DHIS2-21176_AnalyticsTableHooksList
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
90f3a01
feat: add analytics table hook form
henrikmv 7be9388
Merge branch 'hv/feat/DHIS2-21176_AnalyticsTableHooksList' into hv/fe…
henrikmv 33b12d0
feat: enhance analytics table hook form validation and state management
henrikmv 31c9ff4
feat: add new constants for analytics table hooks and enhance form va…
henrikmv af12ec7
feat: clean up
henrikmv da5a3aa
feat: add tests for analytics table hook form
henrikmv 0c10169
feat: add fields to fields folder
henrikmv 1723e3a
fix: format
henrikmv e4b308c
feat: remove unused advanced section from analytics table hook form
henrikmv a91d29e
feat: improved state management and blur handling for analytics and r…
henrikmv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,45 @@ | ||
| import { useQuery } from '@tanstack/react-query' | ||
| import React from 'react' | ||
| import { useParams } from 'react-router-dom' | ||
| import { DefaultEditFormContents, FormBase } from '../../components' | ||
| import { SECTIONS_MAP, useOnSubmitEdit } from '../../lib' | ||
| import { useBoundResourceQueryFn } from '../../lib/query/useBoundQueryFn' | ||
| import { AnalyticsTableHookFormFields } from './form/AnalyticsTableHookFormFields' | ||
| import { | ||
| AnalyticsTableHookFormValues, | ||
| validate, | ||
| } from './form/analyticsTableHookSchema' | ||
| import { fieldFilters } from './form/fieldFilters' | ||
|
|
||
| export const Component = () => { | ||
| const modelId = useParams().id as string | ||
| const section = SECTIONS_MAP.analyticsTableHook | ||
| const queryFn = useBoundResourceQueryFn() | ||
|
|
||
| const query = { | ||
| resource: 'analyticsTableHooks', | ||
| id: modelId, | ||
| params: { | ||
| fields: fieldFilters.concat(), | ||
| }, | ||
| } | ||
|
|
||
| const analyticsTableHookQuery = useQuery({ | ||
| queryKey: [query], | ||
| queryFn: queryFn<AnalyticsTableHookFormValues>, | ||
| }) | ||
| const initialValues = analyticsTableHookQuery.data | ||
|
|
||
| return ( | ||
| <FormBase | ||
| onSubmit={useOnSubmitEdit({ section, modelId })} | ||
| initialValues={initialValues} | ||
| validate={validate} | ||
| includeAttributes={false} | ||
| > | ||
| <DefaultEditFormContents section={section}> | ||
| <AnalyticsTableHookFormFields /> | ||
| </DefaultEditFormContents> | ||
| </FormBase> | ||
| ) | ||
| } |
This file contains hidden or 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,23 @@ | ||
| import React from 'react' | ||
| import { FormBase } from '../../components' | ||
| import { DefaultNewFormContents } from '../../components/form/DefaultFormContents' | ||
| import { SECTIONS_MAP, useOnSubmitNew } from '../../lib' | ||
| import { AnalyticsTableHookFormFields } from './form/AnalyticsTableHookFormFields' | ||
| import { initialValues, validate } from './form/analyticsTableHookSchema' | ||
|
|
||
| const section = SECTIONS_MAP.analyticsTableHook | ||
|
|
||
| export const Component = () => { | ||
| return ( | ||
| <FormBase | ||
| onSubmit={useOnSubmitNew({ section })} | ||
| initialValues={initialValues} | ||
| validate={validate} | ||
| includeAttributes={false} | ||
| > | ||
| <DefaultNewFormContents section={section}> | ||
| <AnalyticsTableHookFormFields /> | ||
| </DefaultNewFormContents> | ||
| </FormBase> | ||
| ) | ||
| } |
141 changes: 141 additions & 0 deletions
141
src/pages/analyticsTableHooks/form/AnalyticsTableHookFormFields.tsx
This file contains hidden or 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,141 @@ | ||
| import i18n from '@dhis2/d2-i18n' | ||
| import { RadioFieldFF, SingleSelectFieldFF, TextAreaFieldFF } from '@dhis2/ui' | ||
| import React from 'react' | ||
| import { Field, useField } from 'react-final-form' | ||
| import { | ||
| HorizontalFieldGroup, | ||
| NameField, | ||
| CodeField, | ||
| StandardFormField, | ||
| StandardFormSection, | ||
| StandardFormSectionDescription, | ||
| StandardFormSectionTitle, | ||
| } from '../../../components' | ||
| import { getConstantTranslation, SECTIONS_MAP, useSchema } from '../../../lib' | ||
| import { AnalyticsTableHook } from '../../../types/generated' | ||
|
|
||
| const section = SECTIONS_MAP.analyticsTableHook | ||
|
|
||
| const phaseOptions = [ | ||
| { | ||
| value: AnalyticsTableHook.phase.RESOURCE_TABLE_POPULATED, | ||
| label: i18n.t('Resource tables'), | ||
| }, | ||
| { | ||
| value: AnalyticsTableHook.phase.ANALYTICS_TABLE_POPULATED, | ||
| label: i18n.t('Analytics tables'), | ||
| }, | ||
| ] | ||
|
|
||
| export const AnalyticsTableHookFormFields = () => { | ||
| const schema = useSchema(section.name) | ||
| const { input: phaseInput } = useField<string | undefined>('phase', { | ||
| subscription: { value: true }, | ||
| }) | ||
| const selectedPhase = phaseInput.value | ||
|
|
||
| const resourceTableOptions = | ||
| schema?.properties.resourceTableType?.constants?.map((constant) => ({ | ||
| value: constant, | ||
| label: getConstantTranslation(constant), | ||
| })) ?? [] | ||
|
|
||
| const analyticsTableOptions = | ||
| schema?.properties.analyticsTableType?.constants?.map((constant) => ({ | ||
| value: constant, | ||
| label: getConstantTranslation(constant), | ||
| })) ?? [] | ||
|
|
||
| const showResourceTableField = | ||
| selectedPhase === AnalyticsTableHook.phase.RESOURCE_TABLE_POPULATED | ||
| const showAnalyticsTableField = | ||
| selectedPhase === AnalyticsTableHook.phase.ANALYTICS_TABLE_POPULATED | ||
|
|
||
| return ( | ||
| <> | ||
| <StandardFormSection> | ||
| <StandardFormSectionTitle> | ||
| {i18n.t('Basic information')} | ||
| </StandardFormSectionTitle> | ||
| <StandardFormSectionDescription> | ||
| {i18n.t( | ||
| 'Set up the basic information for this analytics table hook.' | ||
| )} | ||
| </StandardFormSectionDescription> | ||
| <StandardFormField> | ||
| <NameField schemaSection={section} /> | ||
| </StandardFormField> | ||
| <StandardFormField> | ||
| <CodeField schemaSection={section} /> | ||
| </StandardFormField> | ||
| <StandardFormField> | ||
| <HorizontalFieldGroup | ||
| label={i18n.t('Phase')} | ||
| required | ||
| dataTest="formfields-phase" | ||
| > | ||
| {phaseOptions.map((option) => ( | ||
| <Field<string | undefined> | ||
| key={option.value} | ||
| name="phase" | ||
| component={RadioFieldFF} | ||
| label={option.label} | ||
| type="radio" | ||
| value={option.value} | ||
| /> | ||
| ))} | ||
| </HorizontalFieldGroup> | ||
| </StandardFormField> | ||
|
|
||
| {showResourceTableField ? ( | ||
| <StandardFormField> | ||
| <Field | ||
| component={SingleSelectFieldFF} | ||
| name="resourceTableType" | ||
| label={i18n.t('Which resource table')} | ||
| options={resourceTableOptions} | ||
| required | ||
| clearable | ||
| inputWidth="400px" | ||
| dataTest="formfields-resourceTableType" | ||
| /> | ||
| </StandardFormField> | ||
| ) : null} | ||
|
|
||
| {showAnalyticsTableField ? ( | ||
| <StandardFormField> | ||
| <Field | ||
| component={SingleSelectFieldFF} | ||
| name="analyticsTableType" | ||
| label={i18n.t('Which analytics table')} | ||
| options={analyticsTableOptions} | ||
| required | ||
| clearable | ||
| inputWidth="400px" | ||
| dataTest="formfields-analyticsTableType" | ||
| /> | ||
| </StandardFormField> | ||
| ) : null} | ||
| </StandardFormSection> | ||
|
|
||
| <StandardFormSection> | ||
| <StandardFormSectionTitle> | ||
| {i18n.t('Advanced')} | ||
| </StandardFormSectionTitle> | ||
| <StandardFormSectionDescription> | ||
| {i18n.t('Configure the SQL that should run for this hook.')} | ||
| </StandardFormSectionDescription> | ||
| <StandardFormField> | ||
| <Field | ||
| component={TextAreaFieldFF} | ||
| name="sql" | ||
| label={i18n.t('SQL')} | ||
| required | ||
| inputWidth="400px" | ||
| dataTest="formfields-sql" | ||
| /> | ||
| </StandardFormField> | ||
| </StandardFormSection> | ||
| </> | ||
| ) | ||
| } |
68 changes: 58 additions & 10 deletions
68
src/pages/analyticsTableHooks/form/analyticsTableHookSchema.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -1,20 +1,68 @@ | ||
| import { z } from 'zod' | ||
| import { modelFormSchemas } from '../../../lib' | ||
| import { AnalyticsTableHook } from '../../../types/generated' | ||
| import { | ||
| createFormValidate, | ||
| getDefaultsOld, | ||
| modelFormSchemas, | ||
| } from '../../../lib' | ||
| import { | ||
| AnalyticsTableHook, | ||
| PickWithFieldFilters, | ||
| } from '../../../types/generated' | ||
| import { fieldFilters } from './fieldFilters' | ||
|
|
||
| const { withDefaultListColumns } = modelFormSchemas | ||
| const { withDefaultListColumns, identifiable } = modelFormSchemas | ||
|
|
||
| const analyticsTableHookBaseSchema = z.object({ | ||
| name: z.string(), | ||
| code: z.string().optional(), | ||
| sql: z.string().optional(), | ||
| analyticsTableType: z.nativeEnum(AnalyticsTableHook.analyticsTableType), | ||
| name: z.string().trim().min(1), | ||
| code: z.string().trim().optional(), | ||
| sql: z.string().trim().min(1), | ||
| analyticsTableType: z | ||
| .nativeEnum(AnalyticsTableHook.analyticsTableType) | ||
| .optional(), | ||
| phase: z.nativeEnum(AnalyticsTableHook.phase), | ||
| resourceTableType: z | ||
| .nativeEnum(AnalyticsTableHook.resourceTableType) | ||
| .optional(), | ||
| }) | ||
|
|
||
| export const analyticsTableHookListSchema = analyticsTableHookBaseSchema.merge( | ||
| withDefaultListColumns | ||
| ) | ||
| export const analyticsTableHookFormSchema = identifiable | ||
| .merge(analyticsTableHookBaseSchema) | ||
| .superRefine((value, context) => { | ||
| if ( | ||
| value.phase === AnalyticsTableHook.phase.RESOURCE_TABLE_POPULATED && | ||
| !value.resourceTableType | ||
| ) { | ||
| context.addIssue({ | ||
| code: z.ZodIssueCode.custom, | ||
| path: ['resourceTableType'], | ||
| message: 'Required', | ||
| }) | ||
| } | ||
| if ( | ||
| value.phase === | ||
| AnalyticsTableHook.phase.ANALYTICS_TABLE_POPULATED && | ||
| !value.analyticsTableType | ||
| ) { | ||
| context.addIssue({ | ||
| code: z.ZodIssueCode.custom, | ||
| path: ['analyticsTableType'], | ||
| message: 'Required', | ||
| }) | ||
henrikmv marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }) | ||
|
|
||
| export const analyticsTableHookListSchema = analyticsTableHookBaseSchema | ||
| .partial({ | ||
| sql: true, | ||
| analyticsTableType: true, | ||
| resourceTableType: true, | ||
| }) | ||
| .merge(withDefaultListColumns) | ||
|
|
||
| export const initialValues = getDefaultsOld(analyticsTableHookFormSchema) | ||
| export const validate = createFormValidate(analyticsTableHookFormSchema) | ||
|
|
||
| export type AnalyticsTableHookFormValues = PickWithFieldFilters< | ||
| AnalyticsTableHook, | ||
| typeof fieldFilters | ||
| > | ||
This file contains hidden or 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,11 @@ | ||
| import { DEFAULT_FIELD_FILTERS } from '../../../lib' | ||
|
|
||
| export const fieldFilters = [ | ||
| ...DEFAULT_FIELD_FILTERS, | ||
| 'name', | ||
| 'code', | ||
| 'phase', | ||
| 'resourceTableType', | ||
| 'analyticsTableType', | ||
| 'sql', | ||
| ] as const |
This file contains hidden or 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 |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| export { analyticsTableHookListSchema } from './analyticsTableHookSchema' | ||
| export { analyticsTableHookFormSchema } from './analyticsTableHookSchema' |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.