Skip to content

Commit

Permalink
refactor: removed duplicated method "update"
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhom committed Oct 17, 2021
1 parent 8292379 commit c312e4c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 40 deletions.
18 changes: 4 additions & 14 deletions src/hooks/usePortal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,9 @@ export const usePortal = (hostName: string = 'root') => {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const addPortal = useCallback((name: string, node: ReactNode) => {
const addUpdatePortal = useCallback((name: string, node: ReactNode) => {
dispatch({
type: ACTIONS.ADD_PORTAL,
hostName,
portalName: name,
node,
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const updatePortal = useCallback((name: string, node: ReactNode) => {
dispatch({
type: ACTIONS.UPDATE_PORTAL,
type: ACTIONS.ADD_UPDATE_PORTAL,
hostName,
portalName: name,
node,
Expand All @@ -61,8 +51,8 @@ export const usePortal = (hostName: string = 'root') => {
return {
registerHost,
deregisterHost,
addPortal,
updatePortal,
addPortal: addUpdatePortal,
updatePortal: addUpdatePortal,
removePortal,
};
};
3 changes: 1 addition & 2 deletions src/state/constants.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
enum ACTIONS {
REGISTER_HOST,
DEREGISTER_HOST,
ADD_PORTAL,
UPDATE_PORTAL,
ADD_UPDATE_PORTAL,
REMOVE_PORTAL,
}

Expand Down
19 changes: 11 additions & 8 deletions src/state/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { ACTIONS } from './constants';
import { print } from '../utilities/logger';
import type { PortalType } from '../types';
import type { ActionTypes, AddPortalAction } from './types';
import type {
ActionTypes,
AddUpdatePortalAction,
RemovePortalAction,
} from './types';

const registerHost = (
state: Record<string, Array<PortalType>>,
Expand All @@ -21,7 +25,7 @@ const deregisterHost = (
return state;
};

const addOrUpdatePortal = (
const addUpdatePortal = (
state: Record<string, Array<PortalType>>,
hostName: string,
portalName: string,
Expand Down Expand Up @@ -76,19 +80,18 @@ export const reducer = (
return registerHost(clonedState, action.hostName);
case ACTIONS.DEREGISTER_HOST:
return deregisterHost(clonedState, action.hostName);
case ACTIONS.ADD_PORTAL:
case ACTIONS.UPDATE_PORTAL:
return addOrUpdatePortal(
case ACTIONS.ADD_UPDATE_PORTAL:
return addUpdatePortal(
clonedState,
action.hostName,
(action as AddPortalAction).portalName,
(action as AddPortalAction).node
(action as AddUpdatePortalAction).portalName,
(action as AddUpdatePortalAction).node
);
case ACTIONS.REMOVE_PORTAL:
return removePortal(
clonedState,
action.hostName,
(action as AddPortalAction).portalName
(action as RemovePortalAction).portalName
);
default:
return state;
Expand Down
12 changes: 2 additions & 10 deletions src/state/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import type { ReactNode } from 'react';
import type { ACTIONS } from './constants';

export interface AddPortalAction {
type: ACTIONS;
hostName: string;
portalName: string;
node: ReactNode;
}

export interface UpdatePortalAction {
export interface AddUpdatePortalAction {
type: ACTIONS;
hostName: string;
portalName: string;
Expand All @@ -32,8 +25,7 @@ export interface UnregisterHostAction {
}

export type ActionTypes =
| AddPortalAction
| UpdatePortalAction
| AddUpdatePortalAction
| RemovePortalAction
| RegisterHostAction
| UnregisterHostAction;
6 changes: 0 additions & 6 deletions src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import type { ReactNode } from 'react';

export interface PortalMethods {
mount: (name: string, node: ReactNode) => void;
update: (name: string, node: ReactNode) => void;
unmount: (name: string) => void;
}

export interface PortalType {
name: string;
node: ReactNode;
Expand Down

0 comments on commit c312e4c

Please sign in to comment.