Skip to content

Commit

Permalink
(feat) Add a global SWR configuration (#929)
Browse files Browse the repository at this point in the history
  • Loading branch information
denniskigen authored Feb 16, 2024
1 parent f2a3823 commit 87608bb
Showing 1 changed file with 26 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
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,
throwErrorsToConsole: true,
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;
Expand All @@ -23,7 +34,7 @@ export interface OpenmrsReactComponentProps {

export interface OpenmrsReactComponentState {
caughtError: any;
caughtErrorInfo: any;
caughtErrorInfo: ErrorInfo | null;
config: ComponentConfig;
}

Expand Down Expand Up @@ -57,7 +68,7 @@ export function openmrsComponentDecorator<T>(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,
Expand All @@ -83,15 +94,17 @@ export function openmrsComponentDecorator<T>(userOpts: ComponentDecoratorOptions
} else {
const content = (
<Suspense fallback={null}>
<ComponentContext.Provider value={this.state.config}>
{opts.disableTranslations ? (
<Comp {...this.props} />
) : (
<I18nextProvider i18n={window.i18next} defaultNS={opts.moduleName}>
<SWRConfig value={defaultSwrConfig}>
<ComponentContext.Provider value={this.state.config}>
{opts.disableTranslations ? (
<Comp {...this.props} />
</I18nextProvider>
)}
</ComponentContext.Provider>
) : (
<I18nextProvider i18n={window.i18next} defaultNS={opts.moduleName}>
<Comp {...this.props} />
</I18nextProvider>
)}
</ComponentContext.Provider>
</SWRConfig>
</Suspense>
);

Expand Down

0 comments on commit 87608bb

Please sign in to comment.