diff --git a/src/components/Base/Base.tsx b/src/components/Base/Base.tsx index 4eb4ec0e8..0180fa376 100644 --- a/src/components/Base/Base.tsx +++ b/src/components/Base/Base.tsx @@ -27,7 +27,7 @@ export interface BaseComponentInterface< TResourceKey extends keyof Resources = keyof Resources, > extends CommonComponentInterface { FallbackComponent?: BaseBoundariesProps['FallbackComponent'] - LoaderComponent?: BaseBoundariesProps['LoaderComponent'] + LoaderComponent?: LoadingIndicatorContextProps['LoadingIndicator'] onEvent: OnEventType } @@ -74,54 +74,111 @@ export const BaseComponent = - {children} + + } + > + {children} + ) } -interface BaseLayoutProps { +export interface BaseLayoutProps { children?: ReactNode - error: SDKError | null + error?: SDKError | SDKError[] | null + isLoading?: boolean } -export const BaseLayout = ({ children, error }: BaseLayoutProps) => { +function SingleErrorContent({ error }: { error: SDKError }) { const Components = useComponentContext() const { t } = useTranslation() - const hasFieldErrors = error !== null && error.fieldErrors.length > 0 + const hasFieldErrors = error.fieldErrors.length > 0 return ( - - {error && ( - - {hasFieldErrors && ( - fe.message) - .map(fe => ( - {fe.message} - ))} - /> - )} - {!hasFieldErrors && error.category === 'validation_error' && ( - - {error.raw && - typeof error.raw === 'object' && - 'pretty' in error.raw && - typeof (error.raw as { pretty: unknown }).pretty === 'function' - ? (error.raw as { pretty: () => string }).pretty() - : error.message} - - )} - {!hasFieldErrors && error.category !== 'validation_error' && ( - {error.message || t('errors.unknownError')} - )} - + + {hasFieldErrors && ( + fieldError.message) + .map(fieldError => ( + {fieldError.message} + ))} + /> )} + {!hasFieldErrors && error.category === 'validation_error' && ( + + {error.raw && + typeof error.raw === 'object' && + 'pretty' in error.raw && + typeof (error.raw as { pretty: unknown }).pretty === 'function' + ? (error.raw as { pretty: () => string }).pretty() + : error.message} + + )} + {!hasFieldErrors && error.category !== 'validation_error' && ( + {error.message || t('errors.unknownError')} + )} + + ) +} + +function MultipleErrorsContent({ errors }: { errors: SDKError[] }) { + const Components = useComponentContext() + const { t } = useTranslation() + + return ( + + error.message || error.fieldErrors.length > 0) + .map((error, index) => { + const visibleFieldErrors = error.fieldErrors.filter(fieldError => fieldError.message) + + if (visibleFieldErrors.length === 0) { + return {error.message || t('errors.unknownError')} + } + + return ( + + {error.message || t('errors.unknownError')} + ( + {fieldError.message} + ))} + /> + + ) + })} + /> + + ) +} + +export const BaseLayout = ({ children, error, isLoading }: BaseLayoutProps) => { + const { LoadingIndicator } = useLoadingIndicator() + + const errors = Array.isArray(error) ? error : error ? [error] : [] + const hasErrors = errors.length > 0 + + if (isLoading && !hasErrors) { + return + } + + const [firstError] = errors + + return ( + + {errors.length > 1 && } + {errors.length === 1 && firstError && } {children} ) @@ -166,22 +223,14 @@ const LoaderWithMetrics = ({ export interface BaseBoundariesProps { children?: ReactNode FallbackComponent?: (props: FallbackProps) => JSX.Element - LoaderComponent?: LoadingIndicatorContextProps['LoadingIndicator'] onErrorBoundaryError?: (error: unknown, info: ErrorInfo) => void - componentName?: string } export const BaseBoundaries = ({ children, FallbackComponent = InternalError, - LoaderComponent: LoadingIndicatorFromProps, onErrorBoundaryError, - componentName, }: BaseBoundariesProps) => { - const { LoadingIndicator: LoadingIndicatorFromContext } = useLoadingIndicator() - const LoaderComponent = LoadingIndicatorFromProps ?? LoadingIndicatorFromContext - const { observability } = useObservability() - return ( {({ reset: resetQueries }) => ( @@ -190,17 +239,7 @@ export const BaseBoundaries = ({ onReset={resetQueries} onError={onErrorBoundaryError} > - - } - > - {children} - + {children} )} diff --git a/src/components/Base/index.ts b/src/components/Base/index.ts index 9301ef812..4248e3830 100644 --- a/src/components/Base/index.ts +++ b/src/components/Base/index.ts @@ -1,7 +1,10 @@ export { BaseComponent, BaseBoundaries, + BaseLayout, type BaseComponentInterface, + type BaseLayoutProps, + type BaseBoundariesProps, type CommonComponentInterface, } from './Base' export { createCompoundContext } from './createCompoundContext' diff --git a/src/components/InformationRequests/InformationRequests.tsx b/src/components/InformationRequests/InformationRequests.tsx index dd764553f..4cd87ce5d 100644 --- a/src/components/InformationRequests/InformationRequests.tsx +++ b/src/components/InformationRequests/InformationRequests.tsx @@ -139,8 +139,10 @@ export function InformationRequestsFlow({ onClose={handleCloseModal} footer={ Footer && ( - }> -