{"size": 1562, "ext": "js", "max_stars_repo_path": "client/src/contexts/CheckedItemContext.js", "max_stars_repo_name": "dooking/IssueTracker-28", "max_stars_repo_stars_event_min_datetime": "2020-10-26T07:09:19.000Z", "max_stars_repo_stars_event_max_datetime": "2021-09-23T14:44:37.000Z", "max_issues_repo_path": "client/src/contexts/CheckedItemContext.js", "max_issues_repo_name": "dooking/IssueTracker-28", "max_issues_repo_issues_event_min_datetime": "2020-10-27T02:33:04.000Z", "max_issues_repo_issues_event_max_datetime": "2020-11-11T14:07:37.000Z", "max_forks_repo_path": "client/src/contexts/CheckedItemContext.js", "max_forks_repo_name": "dooking/IssueTracker-28", "max_forks_repo_forks_event_min_datetime": "2020-11-06T07:04:16.000Z", "max_forks_repo_forks_event_max_datetime": "2021-05-13T19:42:00.000Z"}
{"max_stars_count": 12, "max_issues_count": 40, "max_forks_count": 7, "avg_line_length": 25.606557377, "max_line_length": 109, "alphanum_fraction": 0.6882202305}
import React, { useReducer, createContext, useContext } from 'react';

// CheckedItemContext에서 사용할 기본 상태
const initialState = {
  isAllChecked : false,
  checkedItems : new Set()
};

function checkedItemReducer(state, action) {
  switch (action.type) {
    case 'SELECT_ALL':
      return {
        ...state,
        isAllChecked : true,
      }
    case 'DESELECT_ALL':
      return {
          ...state,
          isAllChecked : false,
          checkedItems : new Set()
      }
    case 'CHECKED_UPDATE':
      return{
        ...state,
        checkedItems : action.data
    }
    default:
      return state;
  }
}

// state용 context와 dispatch용 context 따로 만들어주기
const CheckedItemStateContext = createContext(null);
const CheckedItemDispatchContext = createContext(null);

// 위에서 선언한 두가지 context들의 provider로 감싸주는 컴포넌트
export function CheckedItemProvider({ children }) {
  const [state, dispatch] = useReducer(checkedItemReducer, initialState);

  return (
    <CheckedItemStateContext.Provider value={state}>
      <CheckedItemDispatchContext.Provider value={dispatch}> {children}</CheckedItemDispatchContext.Provider>
    </CheckedItemStateContext.Provider>
  );
}

export function useCheckedItemState() {
  const state = useContext(CheckedItemStateContext);
  if (!state) {
    throw new Error('Cannot find CheckedItemProvider!');
  }
  return state;
}

export function useCheckedItemDispatch() {
  const dispatch = useContext(CheckedItemDispatchContext);
  if (!dispatch) {
    throw new Error('Cannot find CheckedItemProvider!');
  }
  return dispatch;
}