-
Notifications
You must be signed in to change notification settings - Fork 25
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: focus ict errored field #478
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,13 +9,19 @@ import { TransitionView } from "@swan-io/lake/src/components/TransitionView"; | |
import { animations, colors } from "@swan-io/lake/src/constants/design"; | ||
import { useDebounce } from "@swan-io/lake/src/hooks/useDebounce"; | ||
import { useUrqlQuery } from "@swan-io/lake/src/hooks/useUrqlQuery"; | ||
import { isNotNullishOrEmpty, isNullishOrEmpty } from "@swan-io/lake/src/utils/nullish"; | ||
import { | ||
isNotNullish, | ||
isNotNullishOrEmpty, | ||
isNullish, | ||
isNullishOrEmpty, | ||
} from "@swan-io/lake/src/utils/nullish"; | ||
import { useEffect, useMemo, useRef, useState } from "react"; | ||
import { ActivityIndicator, View } from "react-native"; | ||
import { hasDefinedKeys, useForm } from "react-ux-form"; | ||
|
||
import { AsyncData, Result } from "@swan-io/boxed"; | ||
import { LakeAlert } from "@swan-io/lake/src/components/LakeAlert"; | ||
import { LakeText } from "@swan-io/lake/src/components/LakeText"; | ||
import { noop } from "@swan-io/lake/src/utils/function"; | ||
import { StyleSheet } from "react-native"; | ||
import { P, match } from "ts-pattern"; | ||
|
@@ -50,7 +56,7 @@ export type Beneficiary = { | |
type Props = { | ||
initialBeneficiary?: Beneficiary; | ||
amount: Amount; | ||
errors?: string[]; | ||
errors?: string[][]; | ||
onPressPrevious: () => void; | ||
onSave: (details: Beneficiary) => void; | ||
}; | ||
|
@@ -81,25 +87,32 @@ export const TransferInternationalWizardBeneficiary = ({ | |
[locale.language, dynamicFields], | ||
); | ||
|
||
const { Field, submitForm, FieldsListener, listenFields, setFieldValue, getFieldState } = | ||
useForm<{ | ||
name: string; | ||
route: string; | ||
results: ResultItem[]; | ||
}>({ | ||
name: { | ||
initialValue: initialBeneficiary?.name ?? "", | ||
validate: validateRequired, | ||
}, | ||
route: { | ||
initialValue: initialBeneficiary?.route ?? "", | ||
validate: () => undefined, | ||
}, | ||
results: { | ||
initialValue: initialBeneficiary?.results ?? [], | ||
validate: () => undefined, | ||
}, | ||
}); | ||
const { | ||
Field, | ||
submitForm, | ||
FieldsListener, | ||
listenFields, | ||
setFieldValue, | ||
getFieldState, | ||
setFieldError, | ||
} = useForm<{ | ||
name: string; | ||
route: string; | ||
results: ResultItem[]; | ||
}>({ | ||
name: { | ||
initialValue: initialBeneficiary?.name ?? "", | ||
validate: validateRequired, | ||
}, | ||
route: { | ||
initialValue: initialBeneficiary?.route ?? "", | ||
validate: () => undefined, | ||
}, | ||
results: { | ||
initialValue: initialBeneficiary?.results ?? [], | ||
validate: () => undefined, | ||
}, | ||
}); | ||
|
||
useEffect(() => { | ||
match(data) | ||
|
@@ -147,16 +160,37 @@ export const TransferInternationalWizardBeneficiary = ({ | |
listenFields(["results"], () => refresh()); | ||
}, [listenFields, refresh]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't forget to remove the listeners to avoid leaks / updating unmounted components states: useEffect(() => {
const removeRouteListener = listenFields(["route"], ({ route: { value } }) => setRoute(value));
const removeResultsListener = listenFields(["results"], () => refresh());
return () => {
removeRouteListener();
removeResultsListener();
};
}, [listenFields, refresh]); |
||
|
||
useEffect(() => { | ||
if (isNotNullish(errors) && errors.length) { | ||
for (const error of errors) { | ||
if (isNotNullishOrEmpty(error[0])) { | ||
dynamicFormApiRef.current?.setDynamicFieldError(error[0], error[1]); | ||
} | ||
} | ||
} | ||
}, [setFieldError, errors, dynamicFormApiRef.current?.setDynamicFieldError]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given the update above, you can safely do: const setDynamicFieldError = dynamicFormApiRef.current?.setDynamicFieldError;
useEffect(() => {
for (const [name, error] of errors) {
setDynamicFieldError?.(name, error);
}
}, [errors, setDynamicFieldError]); |
||
|
||
const orphanErrors = useMemo(() => errors?.filter(([key]) => isNullish(key)), [errors])?.map( | ||
([_, message]) => message, | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be removed entirely as there's no need to check if first tuple item is nullish. |
||
|
||
return ( | ||
<View> | ||
<Tile> | ||
{errors?.map((message, i) => ( | ||
<View key={`validation-alert-${i}`}> | ||
<LakeAlert variant="error" title={message} /> | ||
<Space height={12} /> | ||
</View> | ||
))} | ||
|
||
<Tile | ||
footer={ | ||
isNotNullish(orphanErrors) && orphanErrors.length ? ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid non-boolean values in conditions ( For example: {orphanErrors.length && <p>Hello</p>} // will print the text "0" is array is empty |
||
<LakeAlert | ||
anchored={true} | ||
variant="error" | ||
title={t("transfer.new.internationalTransfer.errors.title")} | ||
> | ||
{orphanErrors?.map((message, i) => ( | ||
<LakeText key={`validation-alert-${i}`}>{message}</LakeText> | ||
))} | ||
</LakeAlert> | ||
) : null | ||
} | ||
> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be replaced with: errors.length > 0 ? (
<LakeAlert
anchored={true}
variant="error"
title={t("transfer.new.internationalTransfer.errors.title")}
>
{errors.map(([_, message], i) => (
<LakeText key={`validation-alert-${i}`}>{message}</LakeText>
))}
</LakeAlert>
) : null (no extra array) |
||
<LakeLabel | ||
label={t("transfer.new.internationalTransfer.beneficiary.name")} | ||
render={id => ( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I highly recommend using an array of tuples (with fixed size: 2). You can also default to a fixed instance of an empty array to avoid useless conditions in your components: