From 87608bb7205e64078f72f710289c81bd170f95c4 Mon Sep 17 00:00:00 2001 From: Dennis Kigen Date: Fri, 16 Feb 2024 23:28:42 +0300 Subject: [PATCH] (feat) Add a global SWR configuration (#929) --- .../src/openmrsComponentDecorator.tsx | 39 ++++++++++++------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/framework/esm-react-utils/src/openmrsComponentDecorator.tsx b/packages/framework/esm-react-utils/src/openmrsComponentDecorator.tsx index f533a4eba..80b7c613c 100644 --- a/packages/framework/esm-react-utils/src/openmrsComponentDecorator.tsx +++ b/packages/framework/esm-react-utils/src/openmrsComponentDecorator.tsx @@ -1,8 +1,9 @@ -import React, { type ComponentType, Suspense } from 'react'; +import React, { type ComponentType, type ErrorInfo, Suspense } from 'react'; import { I18nextProvider } from 'react-i18next'; +import { SWRConfig } from 'swr'; import type {} from '@openmrs/esm-globals'; -import type { ComponentConfig, ExtensionData } from './ComponentContext'; -import { ComponentContext } from './ComponentContext'; +import { openmrsFetch } from '@openmrs/esm-api'; +import { ComponentContext, type ComponentConfig, type ExtensionData } from './ComponentContext'; const defaultOpts = { strictMode: true, @@ -10,6 +11,16 @@ const defaultOpts = { disableTranslations: false, }; +// Read more about the available config options here: https://swr.vercel.app/docs/api#configuration +const defaultSwrConfig = { + // max number of retries after requests have failed + errorRetryCount: 3, + // default fetcher function + fetcher: openmrsFetch, + // only revalidate once every 30 seconds + focusThrottleInterval: 30000, +}; + export interface ComponentDecoratorOptions { moduleName: string; featureName: string; @@ -23,7 +34,7 @@ export interface OpenmrsReactComponentProps { export interface OpenmrsReactComponentState { caughtError: any; - caughtErrorInfo: any; + caughtErrorInfo: ErrorInfo | null; config: ComponentConfig; } @@ -57,7 +68,7 @@ export function openmrsComponentDecorator(userOpts: ComponentDecoratorOptions }; } - componentDidCatch(err: any, info: any) { + componentDidCatch(err: any, info: ErrorInfo) { if (info && info.componentStack) { err.extra = Object.assign(err.extra || {}, { componentStack: info.componentStack, @@ -83,15 +94,17 @@ export function openmrsComponentDecorator(userOpts: ComponentDecoratorOptions } else { const content = ( - - {opts.disableTranslations ? ( - - ) : ( - + + + {opts.disableTranslations ? ( - - )} - + ) : ( + + + + )} + + );