Skip to content

Commit

Permalink
Revert "add action key to useModal hook (#15)" (#16)
Browse files Browse the repository at this point in the history
This reverts commit 80da4b0.
  • Loading branch information
carlosthe19916 authored Dec 14, 2021
1 parent 80da4b0 commit b0457db
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 23 deletions.
10 changes: 3 additions & 7 deletions src/hooks/useModal/useModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ describe('useModal', () => {

// Open modal
const { open } = result.current;
act(() => open('edit'));
act(() => open());
expect(result.current.isOpen).toEqual(true);
expect(result.current.data).toBeUndefined();
expect(result.current.actionKey).toEqual('edit');
});

it('onOpen: with data', () => {
Expand All @@ -20,11 +19,10 @@ describe('useModal', () => {

// Open modal
const { open } = result.current;
act(() => open('edit', ENTITY));
act(() => open(ENTITY));

expect(result.current.isOpen).toEqual(true);
expect(result.current.data).toEqual(ENTITY);
expect(result.current.actionKey).toEqual('edit');
});

it('Close modal with data', () => {
Expand All @@ -34,17 +32,15 @@ describe('useModal', () => {
const { open, close } = result.current;

// Open modal
act(() => open('edit', ENTITY));
act(() => open(ENTITY));

expect(result.current.isOpen).toEqual(true);
expect(result.current.data).toEqual(ENTITY);
expect(result.current.actionKey).toEqual('edit');

// Close modal
act(() => close());

expect(result.current.isOpen).toEqual(false);
expect(result.current.data).toBeUndefined();
expect(result.current.actionKey).toBeUndefined();
});
});
21 changes: 5 additions & 16 deletions src/hooks/useModal/useModal.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import { useCallback, useReducer } from 'react';
import { ActionType, createAction, getType } from 'typesafe-actions';

interface IOpenAction {
data: any;
actionKey: string;
}

const openModal = createAction('useModal/action/openModalWithData')<IOpenAction>();
const openModal = createAction('useModal/action/openModalWithData')<any>();
const closeModal = createAction('useModal/action/startClose')();

// State
type State = Readonly<{
data: any;
isOpen: boolean;
actionKey?: string;
}>;

const defaultState: State = {
data: undefined,
isOpen: false,
actionKey: undefined,
};

// Reducer
Expand All @@ -31,16 +24,14 @@ const reducer = (state: State, action: Action): State => {
case getType(openModal):
return {
...state,
data: action.payload.data,
data: action.payload,
isOpen: true,
actionKey: action.payload.actionKey,
};
case getType(closeModal):
return {
...state,
data: undefined,
isOpen: false,
actionKey: undefined,
};
default:
return state;
Expand All @@ -52,8 +43,7 @@ const reducer = (state: State, action: Action): State => {
interface HookState<T> {
data?: T;
isOpen: boolean;
actionKey?: string;
open: (actionKey: string, data?: T) => void;
open: (data?: T) => void;
close: () => void;
}

Expand All @@ -62,8 +52,8 @@ export const useModal = <T>(): HookState<T> => {
...defaultState,
});

const openHandler = useCallback((actionKey: string, entity?: T) => {
dispatch(openModal({ actionKey, data: entity }));
const openHandler = useCallback((entity?: T) => {
dispatch(openModal(entity));
}, []);

const closeHandler = useCallback(() => {
Expand All @@ -73,7 +63,6 @@ export const useModal = <T>(): HookState<T> => {
return {
data: state.data,
isOpen: state.isOpen,
actionKey: state.actionKey,
open: openHandler,
close: closeHandler,
};
Expand Down

0 comments on commit b0457db

Please sign in to comment.