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

[ui-core]: add onSuccess and onError callback for useLoadData hook #3849

Merged
merged 4 commits into from
Sep 17, 2024
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
65 changes: 58 additions & 7 deletions desktop/core/src/desktop/js/utils/hooks/useLoadData.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('useLoadData', () => {
expect(result.current.error).toBeUndefined();
expect(result.current.loading).toBe(true);

waitFor(() => {
await waitFor(() => {
expect(mockGet).toHaveBeenCalledWith(mockUrl, mockOptions.params, expect.any(Object));
expect(result.current.data).toEqual(mockData);
expect(result.current.error).toBeUndefined();
Expand All @@ -84,7 +84,7 @@ describe('useLoadData', () => {
expect(result.current.error).toBeUndefined();
expect(result.current.loading).toBe(true);

waitFor(() => {
await waitFor(() => {
expect(mockGet).toHaveBeenCalledWith(mockUrl, undefined, expect.any(Object));
expect(result.current.data).toBeUndefined();
expect(result.current.error).toEqual(mockError);
Expand All @@ -108,7 +108,7 @@ describe('useLoadData', () => {
expect(result.current.error).toBeUndefined();
expect(result.current.loading).toBe(true);

waitFor(() => {
await waitFor(() => {
expect(mockGet).toHaveBeenCalledWith(mockUrl, undefined, expect.any(Object));
expect(result.current.data).toEqual(mockData);
expect(result.current.error).toBeUndefined();
Expand All @@ -121,7 +121,7 @@ describe('useLoadData', () => {
result.current.reloadData();
});

waitFor(() => {
await waitFor(() => {
expect(mockGet).toHaveBeenCalledTimes(2);
expect(mockGet).toHaveBeenCalledWith(mockUrl, undefined, expect.any(Object));
expect(result.current.data).toEqual({ ...mockData, product: 'Hue 2' });
Expand All @@ -137,7 +137,7 @@ describe('useLoadData', () => {
expect(result.current.error).toBeUndefined();
expect(result.current.loading).toBe(true);

waitFor(() => {
await waitFor(() => {
expect(mockGet).toHaveBeenCalledWith(mockUrl, undefined, expect.any(Object));
expect(result.current.data).toEqual(mockData);
expect(result.current.error).toBeUndefined();
Expand All @@ -157,7 +157,7 @@ describe('useLoadData', () => {
expect(result.current.error).toBeUndefined();
expect(result.current.loading).toBe(true);

waitFor(() => {
await waitFor(() => {
expect(mockGet).toHaveBeenCalledWith(mockUrl, mockOptions.params, expect.any(Object));
expect(result.current.data).toEqual(mockData);
expect(result.current.error).toBeUndefined();
Expand All @@ -172,11 +172,62 @@ describe('useLoadData', () => {

rerender({ url: mockUrl, options: newOptions });

waitFor(() => {
await waitFor(() => {
expect(mockGet).toHaveBeenCalledWith(mockUrl, newOptions.params, expect.any(Object));
expect(result.current.data).toEqual(newMockData);
expect(result.current.error).toBeUndefined();
expect(result.current.loading).toBe(false);
});
});

it('should call onSuccess callback', async () => {
const mockOnSuccess = jest.fn();
const mockOnError = jest.fn();
const { result } = renderHook(() =>
useLoadData(mockUrl, {
onSuccess: mockOnSuccess,
onError: mockOnError
})
);

expect(result.current.data).toBeUndefined();
expect(result.current.error).toBeUndefined();
expect(result.current.loading).toBe(true);

await waitFor(() => {
expect(mockGet).toHaveBeenCalledWith(mockUrl, undefined, expect.any(Object));
expect(result.current.data).toEqual(mockData);
expect(result.current.error).toBeUndefined();
expect(result.current.loading).toBe(false);
expect(mockOnSuccess).toHaveBeenCalledWith(mockData);
expect(mockOnError).not.toHaveBeenCalled();
});
});

it('should call onError callback', async () => {
const mockError = new Error('Fetch error');
mockGet.mockRejectedValue(mockError);

const mockOnSuccess = jest.fn();
const mockOnError = jest.fn();
const { result } = renderHook(() =>
useLoadData(mockUrl, {
onSuccess: mockOnSuccess,
onError: mockOnError
})
);

expect(result.current.data).toBeUndefined();
expect(result.current.error).toBeUndefined();
expect(result.current.loading).toBe(true);

await waitFor(() => {
expect(mockGet).toHaveBeenCalledWith(mockUrl, undefined, expect.any(Object));
expect(result.current.data).toBeUndefined();
expect(result.current.error).toEqual(mockError);
expect(result.current.loading).toBe(false);
expect(mockOnSuccess).not.toHaveBeenCalled();
expect(mockOnError).toHaveBeenCalledWith(mockError);
});
});
});
23 changes: 17 additions & 6 deletions desktop/core/src/desktop/js/utils/hooks/useLoadData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,27 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { ApiFetchOptions, get } from '../../api/utils';

export type IOptions<T, U> = {
export interface Options<T, U> {
urlPrefix?: string;
params?: U;
fetchOptions?: ApiFetchOptions<T>;
skip?: boolean;
};
onSuccess?: (data: T) => void;
onError?: (error: Error) => void;
}

type IUseLoadData<T> = {
interface UseLoadDataProps<T> {
data?: T;
loading: boolean;
error?: Error;
reloadData: () => void;
};
}

const useLoadData = <T, U = unknown>(url?: string, options?: IOptions<T, U>): IUseLoadData<T> => {
const [localOptions, setLocalOptions] = useState<IOptions<T, U> | undefined>(options);
const useLoadData = <T, U = unknown>(
url?: string,
options?: Options<T, U>
): UseLoadDataProps<T> => {
const [localOptions, setLocalOptions] = useState<Options<T, U> | undefined>(options);
const [data, setData] = useState<T | undefined>();
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<Error | undefined>();
Expand Down Expand Up @@ -61,8 +66,14 @@ const useLoadData = <T, U = unknown>(url?: string, options?: IOptions<T, U>): IU
const fetchUrl = localOptions?.urlPrefix ? `${localOptions.urlPrefix}${url}` : url;
const response = await get<T, U>(fetchUrl, localOptions?.params, fetchOptions);
setData(response);
if (localOptions?.onSuccess) {
localOptions.onSuccess(response);
}
} catch (error) {
setError(error instanceof Error ? error : new Error(error));
if (localOptions?.onError) {
localOptions.onError(error);
}
} finally {
setLoading(false);
}
Expand Down