Skip to content

Commit

Permalink
Used useEvent hook for createPopup functions (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexShukel authored Nov 11, 2022
1 parent c7e71ae commit 46cdfff
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 63 deletions.
15 changes: 15 additions & 0 deletions src/hooks/useEvent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { useCallback, useRef } from 'react';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const useEvent = <F extends (...args: any[]) => any>(func?: F): F => {
const ref = useRef<F | undefined>(func);

ref.current = func;

const callback = useCallback(
(...args: Parameters<F>) => ref.current?.(...args),
[]
);

return callback as F;
};
6 changes: 3 additions & 3 deletions src/hooks/usePopup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ComponentType, useCallback, useRef } from 'react';

import { useEvent } from './useEvent';
import { usePopupsContext } from './usePopupsContext';
import { PopupGroup } from '../components/PopupGroup';
import { DefaultPopup } from '../types/DefaultPopup';
Expand All @@ -24,7 +25,7 @@ export const usePopup = <P, K extends keyof P>(
groupId: group.groupId,
});

const open = useCallback<OptionalParamFunction<Omit<P, K>, void>>(
const open = useEvent<OptionalParamFunction<Omit<P, K>, void>>(
(omittedProps?: Omit<P, K>) => {
const defaultClose = () => {
unmount(popupIdentifier.current);
Expand All @@ -38,8 +39,7 @@ export const usePopup = <P, K extends keyof P>(
);

mount(popup);
},
[PopupComponent, mount, props, unmount]
}
);

const close = useCallback(() => {
Expand Down
62 changes: 30 additions & 32 deletions src/hooks/usePopupsFactory.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ComponentType, useCallback } from 'react';
import { ComponentType } from 'react';

import { useEvent } from './useEvent';
import { usePopupsContext } from './usePopupsContext';
import { PopupGroup } from '../components/PopupGroup';
import { DefaultPopup } from '../types/DefaultPopup';
Expand All @@ -19,37 +20,34 @@ export const usePopupsFactory = <P, K extends keyof P>(
): UsePopupsFactoryBag<P, K> => {
const { mount, unmount } = usePopupsContext();

const create = useCallback(
(omittedProps?: Omit<P, K>) => {
const id = uuid();

const popupIdentifier: PopupIdentifier = {
id,
groupId: group.groupId,
};

const defaultClose = () => {
unmount(popupIdentifier);
};

const popup = new DefaultPopup(
PopupComponent,
{
...props,
...omittedProps,
} as P,
popupIdentifier,
defaultClose
);

mount<P>(popup);

return () => {
unmount(popupIdentifier);
};
},
[group.groupId, PopupComponent, props, mount, unmount]
);
const create = useEvent((omittedProps?: Omit<P, K>) => {
const id = uuid();

const popupIdentifier: PopupIdentifier = {
id,
groupId: group.groupId,
};

const defaultClose = () => {
unmount(popupIdentifier);
};

const popup = new DefaultPopup(
PopupComponent,
{
...props,
...omittedProps,
} as P,
popupIdentifier,
defaultClose
);

mount<P>(popup);

return () => {
unmount(popupIdentifier);
};
});

return create;
};
54 changes: 26 additions & 28 deletions src/hooks/useResponsePopup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ComponentType, useCallback, useRef } from 'react';

import { useEvent } from './useEvent';
import { usePopupsContext } from './usePopupsContext';
import { PopupGroup } from '../components/PopupGroup';
import { OptionalParamFunction } from '../types/OptionalParamFunction';
Expand Down Expand Up @@ -29,34 +30,31 @@ export const useResponsePopup = <P, K extends keyof P, R>(
unmount(popupIdentifierRef.current);
}, [unmount]);

const waitResponse = useCallback(
(omittedProps?: Omit<P, K>) => {
let popup: ResponsePopup<P, R> | null = null;

const promise = new Promise<R>((resolve, reject) => {
popup = new ResponsePopup(
PopupComponent,
{
...props,
...omittedProps,
} as P,
popupIdentifierRef.current,
defaultClose,
resolve,
reject
);

mount(popup);
});

promise.finally(() => {
popup!.isSettled = true;
});

return promise;
},
[PopupComponent, defaultClose, mount, props]
);
const waitResponse = useEvent((omittedProps?: Omit<P, K>) => {
let popup: ResponsePopup<P, R> | null = null;

const promise = new Promise<R>((resolve, reject) => {
popup = new ResponsePopup(
PopupComponent,
{
...props,
...omittedProps,
} as P,
popupIdentifierRef.current,
defaultClose,
resolve,
reject
);

mount(popup);
});

promise.finally(() => {
popup!.isSettled = true;
});

return promise;
});

return waitResponse;
};

0 comments on commit 46cdfff

Please sign in to comment.