{"size": 813, "ext": "js", "max_stars_repo_path": "src/common/hooks/usePermission.js", "max_stars_repo_name": "davesag/social-media-collector", "max_stars_repo_stars_event_min_datetime": "2021-04-23T03:57:25.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-20T16:52:23.000Z", "max_issues_repo_path": "src/common/hooks/usePermission.js", "max_issues_repo_name": "listato/social-media-collector", "max_issues_repo_issues_event_min_datetime": "2020-06-12T00:14:22.000Z", "max_issues_repo_issues_event_max_datetime": "2021-09-08T20:01:55.000Z", "max_forks_repo_path": "src/common/hooks/usePermission.js", "max_forks_repo_name": "listato/social-media-collector", "max_forks_repo_forks_event_min_datetime": "2020-10-27T19:25:52.000Z", "max_forks_repo_forks_event_max_datetime": "2021-01-11T02:15:34.000Z"}
{"max_stars_count": 55, "max_issues_count": 12, "max_forks_count": 6, "avg_line_length": 26.2258064516, "max_line_length": 76, "alphanum_fraction": 0.6728167282}
import { useCallback, useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import actions from 'common/state/permission/actions';

const usePermission = (storageKey, defaultValue) => {
  const checked = useSelector(
    ({ permission: { [storageKey]: { value } = { value: null } } }) => value
  );

  const dispatch = useDispatch();

  const savePermission = useCallback(
    evt => dispatch(actions.savePermission(storageKey, evt.target.checked)),
    [dispatch]
  );

  const loadPermission = useCallback(
    () => dispatch(actions.loadPermission(storageKey, defaultValue)),
    [dispatch]
  );

  useEffect(() => {
    /* istanbul ignore else */
    if (checked === null) loadPermission();
  }, [checked]);

  return { checked, savePermission };
};

export default usePermission;
