Skip to content

Commit

Permalink
Fix type for errors
Browse files Browse the repository at this point in the history
  • Loading branch information
pasyukevich committed Feb 6, 2024
1 parent 762a628 commit 5beeb4c
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 11 deletions.
6 changes: 2 additions & 4 deletions src/components/AddressForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import {View} from 'react-native';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ErrorUtils from '@libs/ErrorUtils';
import type * as Localize from '@libs/Localize';
import Navigation from '@libs/Navigation/Navigation';
import * as ValidationUtils from '@libs/ValidationUtils';
import CONST from '@src/CONST';
import type {Country} from '@src/CONST';
import type ONYXKEYS from '@src/ONYXKEYS';
import type {Errors} from '@src/types/onyx/OnyxCommon';
import AddressSearch from './AddressSearch';
import CountrySelector from './CountrySelector';
import FormProvider from './Form/FormProvider';
Expand Down Expand Up @@ -89,8 +87,8 @@ function AddressForm({
* @returns - An object containing the errors for each inputID
*/

const validator = useCallback((values: OnyxFormValuesFields<typeof ONYXKEYS.FORMS.GET_PHYSICAL_CARD_FORM>): Errors => {
const errors: Errors = {};
const validator = useCallback((values: OnyxFormValuesFields<typeof ONYXKEYS.FORMS.GET_PHYSICAL_CARD_FORM>): ErrorUtils.ErrorsList => {
const errors: ErrorUtils.ErrorsList = {};
const requiredFields = ['addressLine1', 'city', 'country', 'state'] as const;

// Check "State" dropdown is a valid state if selected Country is USA
Expand Down
6 changes: 3 additions & 3 deletions src/components/Form/FormProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {ForwardedRef, MutableRefObject, ReactNode} from 'react';
import React, {createRef, forwardRef, useCallback, useImperativeHandle, useMemo, useRef, useState} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import type {ErrorsList} from '@libs/ErrorUtils';
import * as ValidationUtils from '@libs/ValidationUtils';
import Visibility from '@libs/Visibility';
import * as FormActions from '@userActions/FormActions';
Expand All @@ -11,7 +12,6 @@ import type {OnyxFormKey} from '@src/ONYXKEYS';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Form, Network} from '@src/types/onyx';
import type {FormValueType} from '@src/types/onyx/Form';
import type {Errors} from '@src/types/onyx/OnyxCommon';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import FormContext from './FormContext';
import FormWrapper from './FormWrapper';
Expand Down Expand Up @@ -54,7 +54,7 @@ type FormProviderProps<TFormID extends OnyxFormKey = OnyxFormKey> = FormProvider
children: ((props: {inputValues: OnyxFormValues<TFormID>}) => ReactNode) | ReactNode;

/** Callback to validate the form */
validate?: (values: OnyxFormValuesFields<TFormID>) => Errors;
validate?: (values: OnyxFormValuesFields<TFormID>) => ErrorsList;

/** Should validate function be called when input loose focus */
shouldValidateOnBlur?: boolean;
Expand Down Expand Up @@ -86,7 +86,7 @@ function FormProvider(
const inputRefs = useRef<InputRefs>({});
const touchedInputs = useRef<Record<string, boolean>>({});
const [inputValues, setInputValues] = useState<Form>(() => ({...draftValues}));
const [errors, setErrors] = useState<Errors>({});
const [errors, setErrors] = useState<ErrorsList>({});
const hasServerError = useMemo(() => !!formState && !isEmptyObject(formState?.errors), [formState]);

const onValidate = useCallback(
Expand Down
3 changes: 1 addition & 2 deletions src/components/Form/FormWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import ScrollViewWithContext from '@components/ScrollViewWithContext';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ErrorUtils from '@libs/ErrorUtils';
import type {Form} from '@src/types/onyx';
import type {Errors} from '@src/types/onyx/OnyxCommon';
import type ChildrenProps from '@src/types/utils/ChildrenProps';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type {FormProps, InputRefs} from './types';
Expand All @@ -29,7 +28,7 @@ type FormWrapperProps = ChildrenProps &
submitButtonStyles?: StyleProp<ViewStyle>;

/** Server side errors keyed by microtime */
errors: Errors;
errors: ErrorUtils.ErrorsList;

/** Assuming refs are React refs */
inputRefs: RefObject<InputRefs>;
Expand Down
4 changes: 3 additions & 1 deletion src/libs/ErrorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function getEarliestErrorField<TOnyxData extends OnyxDataWithErrorFields>(onyxDa
return {[key]: errorsForField[key]};
}

type ErrorsList = Record<string, string | [string, {isTranslated: boolean}]>;
type ErrorsList = Record<string, Localize.MaybePhraseKey>;

/**
* Method used to generate error message for given inputID
Expand Down Expand Up @@ -141,3 +141,5 @@ export {
addErrorMessage,
getLatestErrorMessageField,
};

export type {ErrorsList};
2 changes: 1 addition & 1 deletion src/types/onyx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import type Currency from './Currency';
import type CustomStatusDraft from './CustomStatusDraft';
import type Download from './Download';
import type {
AddressForm,
AddDebitCardForm,
AddressForm,
CloseAccountForm,
DateOfBirthForm,
DisplayNameForm,
Expand Down

0 comments on commit 5beeb4c

Please sign in to comment.