Skip to content

Commit

Permalink
useMutationsHandler works fire !!
Browse files Browse the repository at this point in the history
  • Loading branch information
hervedombya committed Oct 19, 2023
1 parent 152b63c commit afd96c2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 125 deletions.
36 changes: 23 additions & 13 deletions src/lib/components/toast/useMutationsHandler.test.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
import { act, renderHook } from '@testing-library/react-hooks';
import { MutationConfig, useMutationsHandler } from './useMutationsHandler';
import { useToast } from './ToastProvider';
import { useMutationsHandler } from './useMutationsHandler';

jest.mock('./ToastProvider', () => ({
useToast: jest.fn(),
}));

const mockUseToast = useToast as jest.MockedFunction<typeof useToast>;

const mutationResultMock = {
context: undefined,
data: [],
failureCount: 0,
error: null,
mutate: jest.fn(),
mutateAsync: jest.fn(),
reset: jest.fn(),
variables: undefined,
};

describe('useMutationsHandler', () => {
const mainMutation = {
mutation: {
context: undefined,
data: [],
failureCount: 0,
error: null,
isError: false,
isIdle: false,
isLoading: false,
isPaused: false,
isSuccess: true,
mutate: jest.fn(),
mutateAsync: jest.fn(),
reset: jest.fn(),
status: 'success',
variables: undefined,
...mutationResultMock,
},
name: 'mutation1',
} as MutationConfig;
};

const dependantMutations = [
{
Expand All @@ -36,6 +40,8 @@ describe('useMutationsHandler', () => {
isSuccess: true,
isError: false,
isIdle: false,
status: 'success',
...mutationResultMock,
},
name: 'mutation2',
},
Expand All @@ -45,11 +51,12 @@ describe('useMutationsHandler', () => {
isSuccess: false,
isError: false,
isIdle: true,
status: 'idle',
...mutationResultMock,
},
name: 'mutation3',
isPrimary: false,
},
] as MutationConfig[];
];

const messageDescriptionBuilder = jest.fn(() => 'message');

Expand Down Expand Up @@ -115,11 +122,14 @@ describe('useMutationsHandler', () => {
mutation: {
isLoading: false,
isSuccess: false,
isIdle: false,
isError: true,
stauts: 'error',
...mutationResultMock,
},
name: 'mutation4',
},
] as MutationConfig[];
];

const { waitFor } = renderHook(() =>
useMutationsHandler({
Expand Down
148 changes: 36 additions & 112 deletions src/lib/components/toast/useMutationsHandler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { ReactNode, useCallback, useEffect } from 'react';
import {
MutationFunction,
UseMutationOptions,
UseMutationResult,
} from 'react-query';
import { UseMutationOptions, UseMutationResult } from 'react-query';
import { useToast } from './ToastProvider';

export type MutationConfig<T> = {
Expand All @@ -13,86 +9,55 @@ export type MutationConfig<T> = {

declare type MAXIMUM_DEPTH = 20;

declare type GetResults<T> = T extends {
mutationFnData: any;
error?: infer TError;
data: infer TData;
}
? UseMutationResult<TData, TError>
: T extends {
mutationFnData: infer TMutationFnData;
error?: infer TError;
}
? UseMutationResult<TMutationFnData, TError>
: T extends {
data: infer TData;
error?: infer TError;
}
? UseMutationResult<TData, TError>
: T extends [any, infer TError, infer TData]
? UseMutationResult<TData, TError>
: T extends [infer TMutationFnData, infer TError]
? UseMutationResult<TMutationFnData, TError>
: T extends [infer TMutationFnData]
? UseMutationResult<TMutationFnData>
: T extends {
mutationFn?: MutationFunction<any>;
select: (data: any) => infer TData;
}
? UseMutationResult<TData>
: T extends {
mutationFn?: MutationFunction<infer TMutationFnData>;
}
? UseMutationResult<TMutationFnData>
: UseMutationResult;
declare type GetDescriptionBuilder<T> = T extends UseMutationResult<
infer TData,
infer TError
declare type GetResults<T> = T extends MutationConfig<
MinimalMutationResult<infer TData, infer TError>
>
? MutationConfig<MinimalMutationResult<TData, TError>>
: T extends MinimalMutationResult<infer TData, infer TError>
? MutationConfig<MinimalMutationResult<TData, TError>>
: MutationConfig<MinimalMutationResult<unknown, unknown>>;

declare type GetDescriptionBuilder<T> = T extends MutationConfig<
MinimalMutationResult<infer TData, infer TError>
>
? DescriptionBuilder<TData, TError>
: T extends MutationConfig<UseMutationOptions<infer TData, infer TError>>
? DescriptionBuilder<TData, TError>
: T extends MinimalMutationResult<infer TData, infer TError>
? DescriptionBuilder<TData, TError>
: DescriptionBuilder;
: never;

/**
* MutationResults reducer recursively maps type param to results
*/

declare type MutationsResults<
T extends any[],
Result extends any[] = [],
Depth extends ReadonlyArray<number> = [],
> = Depth['length'] extends MAXIMUM_DEPTH
? UseMutationResult[]
? MutationConfig<MinimalMutationResult<unknown, unknown>>[]
: T extends []
? []
: T extends [infer Head]
? [...Result, GetResults<Head>]
: T extends [infer Head, ...infer Tail]
? MutationsResults<[...Tail], [...Result, GetResults<Head>], [...Depth, 1]>
: T extends UseMutationOptions<
infer TMutationFnData,
infer TError,
infer TData
>[]
? MutationConfig<
UseMutationResult<unknown extends TData ? TMutationFnData : TData, TError>
>[]
: MutationConfig<UseMutationResult>[];
: T extends MutationConfig<MinimalMutationResult<infer TData, infer TError>>[]
? MutationConfig<MinimalMutationResult<TData, TError>>[]
: MutationConfig<MinimalMutationResult<unknown, unknown>>[];

enum DescriptionBuilderStatus {
Success = 'success',
Error = 'error',
}

type DescriptionBuilder<Data = unknown, Error = unknown> =
| {
error?: Error;
status: DescriptionBuilderStatus;
name: string;
}
| {
data?: Data;
status: DescriptionBuilderStatus;
name: string;
};
type DescriptionBuilder<Data = unknown, Error = unknown> = {
error?: Error;
data?: Data;
status: DescriptionBuilderStatus;
name: string;
};

declare type DescriptionBuilders<
T extends any[],
Expand Down Expand Up @@ -121,23 +86,9 @@ declare type DescriptionBuilders<
>[]
: DescriptionBuilder[];

type Props<
A = unknown,
B = unknown,
C = unknown,
D = unknown,
MainMutationType extends UseMutationResult<A, B, C, D> = UseMutationResult<
A,
B,
C,
D
>,
T extends any[] = any[],
> = {
type Props<MainMutationType, T extends any[]> = {
mainMutation: MutationConfig<MainMutationType>;
dependantMutations?: readonly [
...MutationsResults<MutationConfig<MainMutationType>[]>
];
dependantMutations?: readonly [...MutationsResults<T>];
messageDescriptionBuilder: (
mutations: T extends []
? [GetDescriptionBuilder<MainMutationType>]
Expand All @@ -147,36 +98,21 @@ type Props<
onMainMutationSuccess?: () => void;
};

// type MutationsHandlerProps = {
// mainMutation: MutationConfig<unknown, unknown, unknown>;
// dependantMutations?: MutationConfig<unknown, unknown, unknown>[];
// messageDescriptionBuilder: (
// successMutations: DescriptionBuilder<unknown, unknown>[],
// errorMutations: DescriptionBuilder<unknown, unknown>[],
// ) => ReactNode;
// toastStyles?: React.CSSProperties;
// onMainMutationSuccess?: () => void;
// };
type MinimalMutationResult<TData, TError> = Pick<
UseMutationResult<TData, TError, unknown, unknown>,
'isError' | 'isIdle' | 'isSuccess' | 'isLoading' | 'error' | 'data'
>;

export const useMutationsHandler = <
A = unknown,
B = unknown,
C = unknown,
D = unknown,
MainMutationType extends UseMutationResult<A, B, C, D> = UseMutationResult<
A,
B,
C,
D
>,
T extends any[] | [] = [],
MainMutationType extends MinimalMutationResult<unknown, unknown>,
T extends any[] | [],
>({
mainMutation,
dependantMutations,
messageDescriptionBuilder,
toastStyles,
onMainMutationSuccess,
}: Props<A, B, C, D, MainMutationType, T>) => {
}: Props<MainMutationType, T>) => {
const { showToast } = useToast();
const mutations = [
mainMutation,
Expand All @@ -196,18 +132,6 @@ export const useMutationsHandler = <
(_, index) => results[index].isError,
);

const successDescriptionBuilder = successMutations.map((m) => ({
data: m.mutation?.data,
status: DescriptionBuilderStatus.Success,
name: m.name,
}));

const errorDescriptionBuilder = errorMutations.map((m) => ({
error: m.mutation?.error,
status: DescriptionBuilderStatus.Error,
name: m.name,
}));

const mainMutationDesc: GetDescriptionBuilder<MainMutationType> = {
data: mainMutation.mutation?.data,
status: DescriptionBuilderStatus.Success,
Expand Down

0 comments on commit afd96c2

Please sign in to comment.