Skip to content

Commit

Permalink
Project refactoring (#64)
Browse files Browse the repository at this point in the history
* Removed isClosing prop

* Fixed test
  • Loading branch information
AlexShukel authored Jun 20, 2022
1 parent 9798ac5 commit 52006e0
Show file tree
Hide file tree
Showing 16 changed files with 23 additions and 117 deletions.
13 changes: 4 additions & 9 deletions src/hooks/usePopupsBag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { PopupGroup } from '../components/PopupGroup';
import { Popup } from '../types/Popup';
import { PopupIdentifier } from '../types/PopupIdentifier';
import { PopupsBag } from '../types/PopupsBag';
import { callIfPromiseReturned } from '../utils/callIfPromiseReturned';
import { popupsReducer } from '../utils/popupsReducer';

export const usePopupsBag = (): PopupsBag => {
Expand Down Expand Up @@ -32,11 +31,11 @@ export const usePopupsBag = (): PopupsBag => {
dispatch({ type: 'unmount', payload: { popupIdentifier } });
}, []);

const mount = useCallback(<P>(popup: Popup<P>) => {
const mount = useCallback(<P = {}>(popup: Popup<P>) => {
dispatch({
type: 'mount',
payload: {
popup: popup as Popup<unknown>,
popup: popup as unknown as Popup<object>,
},
});

Expand All @@ -46,13 +45,9 @@ export const usePopupsBag = (): PopupsBag => {
const close = useCallback(
(popupIdentifier: PopupIdentifier) => {
const popup = getPopup(popupIdentifier);
if (popup.close) {
callIfPromiseReturned(popup.close, () =>
unmount(popupIdentifier)
);
}
popup.close();
},
[unmount, getPopup]
[getPopup]
);

return {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/usePopupsByGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { usePopupsContext } from './usePopupsContext';
import { PopupGroup } from '../components/PopupGroup';
import { Popup } from '../types/Popup';

export const usePopupsByGroup = (group: PopupGroup): Popup<unknown>[] => {
export const usePopupsByGroup = (group: PopupGroup): Popup<object>[] => {
const { getPopupsByGroup } = usePopupsContext();

const popups = getPopupsByGroup(group);
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useResponseHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const useResponseHandler = (close: () => void): ResponseHandler => {
} = usePopupsContext();
const popupIdentifier = usePopupIdentifier();

const popupRef = useRef<ResponsePopup<unknown, unknown> | null>(null);
const popupRef = useRef<ResponsePopup<object, unknown> | null>(null);

const resolve = useCallback(
(value: unknown) => {
Expand Down
4 changes: 2 additions & 2 deletions src/types/DefaultPopup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export class DefaultPopup<P> extends Popup<P> {
}

export const isDefaultPopup = (
popup: Popup<unknown>
): popup is DefaultPopup<unknown> => uniqueKey in popup;
popup: Popup<object>
): popup is DefaultPopup<object> => uniqueKey in popup;
9 changes: 2 additions & 7 deletions src/types/Popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ import { ComponentType } from 'react';
import { PopupIdentifier } from './PopupIdentifier';
import { CLOSE_NOT_IMPLEMENTED } from '../constants';

export abstract class Popup<P> {
public isClosing = false;

export abstract class Popup<P = {}> {
constructor(
public PopupComponent: ComponentType<P>,
public props: P,
Expand All @@ -19,9 +17,6 @@ export abstract class Popup<P> {
public setCloseHandler: (close: () => void | Promise<void>) => void = (
close
) => {
this.close = () => {
this.isClosing = true;
return close();
};
this.close = close;
};
}
4 changes: 2 additions & 2 deletions src/types/PopupsBag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export type PopupsBag = {
mount: <P>(popup: Popup<P>) => PopupIdentifier;
unmount: (popupIdentifier: PopupIdentifier) => void;

getPopupsByGroup: (group: PopupGroup) => Array<Popup<unknown>>;
getPopup: (popupIdentifier: PopupIdentifier) => Popup<unknown>;
getPopupsByGroup: (group: PopupGroup) => Array<Popup<object>>;
getPopup: (popupIdentifier: PopupIdentifier) => Popup<object>;

close: (popupIdentifier: PopupIdentifier) => void;
};
2 changes: 1 addition & 1 deletion src/types/PopupsRegistry.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import { Popup } from './Popup';

export type PopupsRegistry = Record<symbol, Record<number, Popup<unknown>>>;
export type PopupsRegistry = Record<symbol, Record<number, Popup<object>>>;
4 changes: 2 additions & 2 deletions src/types/ResponsePopup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ export class ResponsePopup<P, R> extends Popup<P> {
}

export const isResponsePopup = (
popup: Popup<unknown>
): popup is ResponsePopup<unknown, unknown> => isSettledKey in popup;
popup: Popup<object>
): popup is ResponsePopup<object, unknown> => isSettledKey in popup;
12 changes: 0 additions & 12 deletions src/utils/callIfPromiseReturned.ts

This file was deleted.

5 changes: 0 additions & 5 deletions src/utils/isPromise.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/utils/popupsReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { PopupsRegistry } from '../types/PopupsRegistry';
type MountAction = {
type: 'mount';
payload: {
popup: Popup<unknown>;
popup: Popup<object>;
};
};

Expand Down
2 changes: 1 addition & 1 deletion src/utils/renderPopups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { ReactElement } from 'react';
import { PopupIdentifierProvider } from './PopupIdentifierContext';
import { Popup } from '../types/Popup';

export const renderPopups = (popups: Popup<unknown>[]): ReactElement[] =>
export const renderPopups = (popups: Popup<object>[]): ReactElement[] =>
popups.map(({ PopupComponent, props, popupIdentifier }) => (
<PopupIdentifierProvider
key={popupIdentifier.id}
Expand Down
4 changes: 2 additions & 2 deletions test/hooks/useResponsePopup.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { act, renderHook, screen } from '@testing-library/react';

import { group, TestHookWrapper } from './TestHookWrapper';
import { useResponsePopup } from '../../src/hooks/useResponsePopup';
import { isPromise } from '../../src/utils/isPromise';

const ConfirmPopup: React.FC = jest.fn(() => <div>Confirm popup</div>);

Expand All @@ -20,7 +19,8 @@ describe('useResponsePopup', () => {
);

act(() => {
expect(isPromise(result.current() as unknown as void)).toBeTruthy();
// It must return promise
expect(result.current()['then']).toBeDefined();
});
});

Expand Down
43 changes: 0 additions & 43 deletions test/utils/callIfPromiseReturned.test.ts

This file was deleted.

24 changes: 0 additions & 24 deletions test/utils/isPromise.test.ts

This file was deleted.

8 changes: 4 additions & 4 deletions test/utils/popupsReducer.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useReducer } from 'react';
import { act, renderHook } from '@testing-library/react';

import { createPopupGroup, PopupGroup } from '../../src/components/PopupGroup';
import { createPopupGroup } from '../../src/components/PopupGroup';
import { DefaultPopup } from '../../src/types/DefaultPopup';
import { Popup } from '../../src/types/Popup';
import { PopupIdentifier } from '../../src/types/PopupIdentifier';
Expand Down Expand Up @@ -48,9 +48,9 @@ describe('State reducer of popups', () => {
useReducer(popupsReducer, {
popups: {
[group.groupId]: {
0: {} as Popup<unknown>,
1: {} as Popup<unknown>,
2: {} as Popup<unknown>,
0: {} as Popup<object>,
1: {} as Popup<object>,
2: {} as Popup<object>,
},
},
})
Expand Down

0 comments on commit 52006e0

Please sign in to comment.