Skip to content
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: add onError property #24

Merged
merged 3 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/components/ChartKit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import './ChartKit.scss';

const b = block('chartkit');

export const ChartKit = React.forwardRef<ChartKitRef | undefined, ChartKitProps<ChartkitType>>(
const ChartKitComponent = React.forwardRef<ChartKitRef | undefined, ChartKitProps<ChartkitType>>(
(props, ref) => {
const widgetRef = React.useRef<ChartKitWidgetRef>();
const {id = getRandomCKId(), type, data, onLoad, ...restProps} = props;
const {id = getRandomCKId(), type, data, onLoad, onError, ...restProps} = props;
const lang = settings.get('lang');
const plugins = settings.get('plugins');
const plugin = plugins.find((iteratedPlugin) => iteratedPlugin.type === type);
Expand All @@ -39,7 +39,7 @@ export const ChartKit = React.forwardRef<ChartKitRef | undefined, ChartKitProps<
);

return (
<ErrorBoundary>
<ErrorBoundary onError={onError}>
<React.Suspense fallback={<Loader />}>
<div className={b()}>
<ChartComponent
Expand All @@ -56,3 +56,5 @@ export const ChartKit = React.forwardRef<ChartKitRef | undefined, ChartKitProps<
);
},
);

export const ChartKit = React.memo(ChartKitComponent);
15 changes: 14 additions & 1 deletion src/components/ErrorBoundary/ErrorBoundary.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import React from 'react';
import type {ChartKitOnError} from '../../types';
import {ErrorView} from '../ErrorView/ErrorView';

type Props = {
onError?: ChartKitOnError;
};

type State = {
error?: Error;
};

export class ErrorBoundary extends React.Component<{}, State> {
export class ErrorBoundary extends React.Component<Props, State> {
static getDerivedStateFromError(error: Error) {
return {error};
}
Expand All @@ -14,6 +19,14 @@ export class ErrorBoundary extends React.Component<{}, State> {
error: undefined,
};

componentDidUpdate() {
const {error} = this.state;

if (error) {
this.props.onError?.({error});
}
}

render() {
if (this.state.error) {
return <ErrorView />;
Expand Down
13 changes: 9 additions & 4 deletions src/plugins/yagr/renderer/YagrWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import moment from 'moment';
import debounce from 'lodash/debounce';
import {useThemeValue} from '@yandex-cloud/uikit';
import YagrComponent from 'yagr/dist/react';
import YagrComponent, {YagrChartProps} from 'yagr/dist/react';
import {
YagrConfig,
TooltipRow,
Expand Down Expand Up @@ -233,6 +233,13 @@ const YagrWidget = React.forwardRef<ChartKitWidgetRef | undefined, YagrWidgetPro
.join(', ') || id
: id;

const handleChartLoading: NonNullable<YagrChartProps['onChartLoad']> = React.useCallback(
(chart, {renderTime}) => {
onLoad?.({...data, widget: chart, widgetRendering: renderTime});
},
[onLoad, data],
);

const onWindowResize = React.useCallback(() => {
if (yagrRef.current?.chart) {
const chart = yagrRef.current.chart;
Expand Down Expand Up @@ -268,9 +275,7 @@ const YagrWidget = React.forwardRef<ChartKitWidgetRef | undefined, YagrWidgetPro
ref={yagrRef}
id={id}
config={config}
onChartLoad={(chart, {renderTime}) =>
onLoad && onLoad({...props.data, widget: chart, widgetRendering: renderTime})
}
onChartLoad={handleChartLoading}
debug={{filename: debugFileName}}
/>
);
Expand Down
3 changes: 3 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@ export type ChartKitOnLoadData<T extends ChartkitType> = {
widgetRendering?: number;
};

export type ChartKitOnError = (data: {error: any}) => void;

export type ChartKitProps<T extends ChartkitType> = {
type: T;
data: ChartkitWidget[T]['data'];
id?: string;
onLoad?: (data?: ChartKitOnLoadData<T>) => void;
onError?: ChartKitOnError;
} & {[key in keyof Omit<ChartkitWidget[T], 'data' | 'widget'>]: ChartkitWidget[T][key]};

export type ChartKitPlugin = {
Expand Down