Skip to content

Commit

Permalink
refactor: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Nov 6, 2024
1 parent dc46f01 commit 6b68b6d
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 353 deletions.
34 changes: 32 additions & 2 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2024-11-04T20:16:04.917Z\n"
"PO-Revision-Date: 2024-11-04T20:16:04.918Z\n"
"POT-Creation-Date: 2024-11-06T18:10:35.150Z\n"
"PO-Revision-Date: 2024-11-06T18:10:35.150Z\n"

msgid "schemas"
msgstr "schemas"
Expand Down Expand Up @@ -411,6 +411,15 @@ msgstr "Translation updated successfully"
msgid "Save translations"
msgstr "Save translations"

msgid "Go back"
msgstr "Go back"

msgid "Next section"
msgstr "Next section"

msgid "Save and exit"
msgstr "Save and exit"

msgid "Can edit and capture"
msgstr "Can edit and capture"

Expand Down Expand Up @@ -1091,15 +1100,36 @@ msgstr ""
"included. PHU will still be available for the PHU level, but not included "
"in the aggregations to the levels above."

msgid "Set up the basic information for this data set."
msgstr "Set up the basic information for this data set."

msgid "Configure data elements"
msgstr "Configure data elements"

msgid "Choose what data is collected for this data set."
msgstr "Choose what data is collected for this data set."

msgid "Configure data entry periods"
msgstr "Configure data entry periods"

msgid "Choose for what time periods data can be entered for this data set"
msgstr "Choose for what time periods data can be entered for this data set"

msgid "Setup"
msgstr "Setup"

msgid "Data"
msgstr "Data"

msgid "Data Elements"
msgstr "Data Elements"

msgid "Periods"
msgstr "Periods"

msgid "Period type"
msgstr "Period type"

msgid "Organisation Units"
msgstr "Organisation Units"

Expand Down
1 change: 0 additions & 1 deletion src/lib/form/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ export { validate, createFormValidate } from './validate'
export { useOnSubmitEdit, useOnSubmitNew } from './useOnSubmit'
export { modelFormSchemas } from './modelFormSchemas'
export * from './sectionedForm'
export * from './sectionedForm/sectionedFormDescriptor'
14 changes: 0 additions & 14 deletions src/lib/form/sectionedForm/SectionedFormBase.tsx

This file was deleted.

81 changes: 75 additions & 6 deletions src/lib/form/sectionedForm/SectionedFormContext.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,89 @@
import React, { createContext, useState } from 'react'
import { createFormStore, FormProps, FormStore } from './formStore'
import { SectionDescriptor, SectionedFormDescriptor } from './formDescriptor'

Check failure on line 2 in src/lib/form/sectionedForm/SectionedFormContext.tsx

View workflow job for this annotation

GitHub Actions / build

Cannot find module './formDescriptor' or its corresponding type declarations.

Check failure on line 2 in src/lib/form/sectionedForm/SectionedFormContext.tsx

View workflow job for this annotation

GitHub Actions / lint

Unable to resolve path to module './formDescriptor'

export const SectionedFormContext = createContext<FormStore | null>(null)
/* Some of the types in this file may look complex.
However they are here to help type-safety and autocommpletion for consumers.
The only thing consumers need to do is pass the type of the formdescriptor to use the context.
useSectionedFormDescriptor<typeof FormDescriptor>()
This helps usage in specific form components.
*/

export const SectionedFormProvider = ({
type AllFieldNames<T extends SectionedFormDescriptor> =
T['sections'][number]['fields'][number]['name']

/* Helper to avoid returning undefined from a map when we know we have the value from the type.
And conversely - add undefined to TType if we dont have a specifc type for T*/
type EnforceIfInferrable<
T extends SectionedFormDescriptor,
TType
> = T extends SectionedFormDescriptor<infer U>
? unknown extends U
? TType | undefined
: TType
: never

function createContextValue<const T extends SectionedFormDescriptor>(
descriptor: T
) {
const fieldLabels = Object.fromEntries(
descriptor.sections.flatMap((section) =>

Check failure on line 30 in src/lib/form/sectionedForm/SectionedFormContext.tsx

View workflow job for this annotation

GitHub Actions / build

Parameter 'section' implicitly has an 'any' type.
section.fields.map((f) => [f.name, f.label] as const)

Check failure on line 31 in src/lib/form/sectionedForm/SectionedFormContext.tsx

View workflow job for this annotation

GitHub Actions / build

Parameter 'f' implicitly has an 'any' type.
)
) as Record<AllFieldNames<T>, EnforceIfInferrable<T, string>>

const sectionMap = Object.fromEntries(
descriptor.sections.map((s) => [s.name, s])

Check failure on line 36 in src/lib/form/sectionedForm/SectionedFormContext.tsx

View workflow job for this annotation

GitHub Actions / build

Parameter 's' implicitly has an 'any' type.
) as Record<
T['sections'][number]['name'],
EnforceIfInferrable<T, SectionDescriptor>
>

const sections: T['sections'] = descriptor.sections
return {
formName: descriptor.name,
formLabel: descriptor.label,
sections,
getSection: (name: T['sections'][number]['name']) => sectionMap[name],
getFieldLabel: (field: AllFieldNames<T>) => {
return fieldLabels[field]
},
}
}
type SectionFormContextValue<T extends SectionedFormDescriptor> = ReturnType<
typeof createContextValue<T>
>

export const SectionedFormContext = createContext<ReturnType<
typeof createContextValue
> | null>(null)

export const SectionedFormDescriptorProvider = <
T extends SectionedFormDescriptor
>({
children,
initialValue,
}: {
initialValue: Partial<FormProps>
initialValue: T
children: React.ReactNode
}) => {
const [store] = useState(() => createFormStore(initialValue))
const [contextValue] = useState(() => createContextValue(initialValue))

return (
<SectionedFormContext.Provider value={store}>
<SectionedFormContext.Provider value={contextValue}>
{children}
</SectionedFormContext.Provider>
)
}

export const useSectionedFormDescriptor = <
T extends SectionedFormDescriptor
>() => {
const context = React.useContext(SectionedFormContext)
if (!context) {
throw new Error(
'useSectionedFormDescriptor must be used within a SectionedFormDescriptorProvider'
)
}
return context as SectionFormContextValue<T>
}
90 changes: 0 additions & 90 deletions src/lib/form/sectionedForm/SectionedFormDescriptorProvider.tsx

This file was deleted.

21 changes: 0 additions & 21 deletions src/lib/form/sectionedForm/SectionedFormField.tsx

This file was deleted.

24 changes: 0 additions & 24 deletions src/lib/form/sectionedForm/SectionedFormSection.tsx

This file was deleted.

36 changes: 0 additions & 36 deletions src/lib/form/sectionedForm/SectionedFormSectionContext.tsx

This file was deleted.

Loading

0 comments on commit 6b68b6d

Please sign in to comment.