generated from EyeSeeTea/dhis2-app-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge feature/full-create-edit-event-form and solve conflicts
- Loading branch information
Showing
15 changed files
with
552 additions
and
500 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
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,200 @@ | ||
import { Maybe } from "../../../utils/ts-utils"; | ||
import { validateFieldRequired, validateFieldRequiredWithNotApplicable } from "./validations"; | ||
import { UserOption } from "../user-selector/UserSelector"; | ||
import { Option } from "../utils/option"; | ||
import { ValidationError, ValidationErrorKey } from "../../../domain/entities/ValidationError"; | ||
import { FormSectionState } from "./FormSectionsState"; | ||
|
||
export type FieldType = "text" | "boolean" | "select" | "radio" | "date" | "user"; | ||
|
||
type FormFieldStateBase<T> = { | ||
id: string; | ||
label?: string; | ||
placeholder?: string; | ||
helperText?: string; | ||
errors: string[]; | ||
required?: boolean; | ||
showIsRequired?: boolean; | ||
disabled?: boolean; | ||
isVisible?: boolean; | ||
hasNotApplicable?: boolean; | ||
notApplicableFieldId?: string; | ||
width?: string; | ||
maxWidth?: string; | ||
value: T; | ||
type: FieldType; | ||
}; | ||
|
||
export type FormTextFieldState = FormFieldStateBase<string> & { | ||
type: "text"; | ||
multiline?: boolean; | ||
}; | ||
|
||
export type FormBooleanFieldState = FormFieldStateBase<boolean> & { | ||
type: "boolean"; | ||
}; | ||
|
||
export type FormMultipleOptionsFieldState = FormFieldStateBase<string[]> & { | ||
type: "select"; | ||
options: Option[]; | ||
multiple: true; | ||
}; | ||
|
||
export type FormOptionsFieldState = FormFieldStateBase<string> & { | ||
type: "select" | "radio"; | ||
options: Option[]; | ||
multiple: false; | ||
}; | ||
|
||
export type FormDateFieldState = FormFieldStateBase<Date | null> & { | ||
type: "date"; | ||
}; | ||
|
||
export type FormAvatarFieldState = FormFieldStateBase<Maybe<string>> & { | ||
type: "user"; | ||
options: UserOption[]; | ||
}; | ||
|
||
export type FormFieldState = | ||
| FormTextFieldState | ||
| FormOptionsFieldState | ||
| FormMultipleOptionsFieldState | ||
| FormBooleanFieldState | ||
| FormDateFieldState | ||
| FormAvatarFieldState; | ||
|
||
// HELPERS: | ||
|
||
export function getAllFieldsFromSections(formSections: FormSectionState[]): FormFieldState[] { | ||
return formSections.reduce( | ||
(acc: FormFieldState[], section: FormSectionState): FormFieldState[] => { | ||
if (section.subsections) { | ||
return [...acc, ...getAllFieldsFromSections(section.subsections)]; | ||
} else { | ||
return [...acc, ...section.fields]; | ||
} | ||
}, | ||
[] | ||
); | ||
} | ||
|
||
export function getStringFieldValue(id: string, allFields: FormFieldState[]): string { | ||
return ( | ||
getFieldValueById<FormTextFieldState | FormOptionsFieldState | FormAvatarFieldState>( | ||
id, | ||
allFields | ||
) || "" | ||
); | ||
} | ||
|
||
export function getDateFieldValue(id: string, allFields: FormFieldState[]): Date | null { | ||
return getFieldValueById<FormDateFieldState>(id, allFields) || null; | ||
} | ||
|
||
export function getBooleanFieldValue(id: string, allFields: FormFieldState[]): boolean { | ||
return !!getFieldValueById<FormBooleanFieldState>(id, allFields); | ||
} | ||
|
||
export function getMultipleOptionsFieldValue(id: string, allFields: FormFieldState[]): string[] { | ||
return getFieldValueById<FormMultipleOptionsFieldState>(id, allFields) || []; | ||
} | ||
|
||
export function getFieldValueById<F extends FormFieldState>( | ||
id: string, | ||
fields: FormFieldState[] | ||
): F["value"] | undefined { | ||
const field = fields.find(field => field.id === id); | ||
if (field) { | ||
return getFieldValue<F>(field); | ||
} | ||
} | ||
|
||
export function getFieldIdFromIdsDictionary<T extends Record<string, string>>( | ||
key: keyof T, | ||
fieldIdsDictionary: T | ||
): string { | ||
return fieldIdsDictionary[key] as string; | ||
} | ||
|
||
export function getEmptyValueForField<F extends FormFieldState>(field: FormFieldState): F["value"] { | ||
switch (field.type) { | ||
case "text": | ||
return ""; | ||
case "boolean": | ||
return false; | ||
case "select": | ||
return field.multiple ? [] : ""; | ||
case "radio": | ||
return ""; | ||
case "date": | ||
return null; | ||
case "user": | ||
return undefined; | ||
} | ||
} | ||
|
||
export function getFieldValue<F extends FormFieldState>(field: FormFieldState): F["value"] { | ||
return field.value; | ||
} | ||
|
||
export function isFieldInSection(section: FormSectionState, field: FormFieldState): boolean { | ||
return section.fields.some(f => f.id === field.id); | ||
} | ||
|
||
// UPDATES: | ||
|
||
export function updateFields( | ||
formFields: FormFieldState[], | ||
updatedField: FormFieldState, | ||
fieldValidationErrors?: ValidationError[] | ||
): FormFieldState[] { | ||
return formFields.map(field => { | ||
if (field.id === updatedField.id) { | ||
return { | ||
...updatedField, | ||
errors: | ||
fieldValidationErrors?.find(error => error.property === updatedField.id) | ||
?.errors || [], | ||
}; | ||
} else { | ||
return field; | ||
} | ||
}); | ||
} | ||
|
||
export function updateFieldState<F extends FormFieldState>( | ||
fieldToUpdate: F, | ||
newValue: F["value"] | ||
): F { | ||
return { ...fieldToUpdate, value: newValue }; | ||
} | ||
|
||
// VALIDATIONS: | ||
|
||
export function validateField( | ||
field: FormFieldState, | ||
allFields: FormFieldState[] = [] | ||
): ValidationError | undefined { | ||
if (field.notApplicableFieldId || !field.isVisible) return; | ||
|
||
const errors: ValidationErrorKey[] = [ | ||
...(field.required && !field.hasNotApplicable | ||
? validateFieldRequired(field.value, field.type) | ||
: []), | ||
...(field.hasNotApplicable | ||
? validateFieldRequiredWithNotApplicable( | ||
field.value, | ||
field.type, | ||
!!allFields.find(f => f.notApplicableFieldId === field.id)?.value | ||
) | ||
: []), | ||
]; | ||
|
||
return errors.length > 0 | ||
? { | ||
property: field.id, | ||
errors: errors, | ||
value: field.value, | ||
} | ||
: undefined; | ||
} |
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
Oops, something went wrong.