All files / src/context ParametersContext.jsx

100% Statements 26/26
100% Branches 8/8
100% Functions 8/8
100% Lines 26/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111    4x 4x         4x                                 9x   4x             2x           2x     1x               55x   55x                         71x 70x 4x   66x             76x 76x 4x   72x             67x   67x 4x     67x 2x     67x 2x     67x                
import { createContext, useContext, useReducer, useCallback } from 'react';
 
const ParametersContext = createContext(null);
const ParametersDispatchContext = createContext(null);
 
/**
 * Initial parameters state
 */
const INITIAL_PARAMETERS_STATE = {
  isLoaded: false,
  r_ij: 1.2,
  paramGeneral: null,
  onebody_parameters: null,
  twobody_parameters: null,
  threebody_parameters: null,
  fourbody_parameters: null,
  diagonalAtom: null,
  hydrogenbonds: null,
  bondOrderArrays: null,
};
 
/**
 * Parameters reducer
 */
function parametersReducer(state, action) {
  switch (action.type) {
    case 'SET_PARAMETERS':
      return {
        ...state,
        ...action.payload,
        isLoaded: true,
      };
 
    case 'SET_BOND_ORDER_ARRAYS':
      return {
        ...state,
        bondOrderArrays: action.payload,
      };
 
    case 'RESET_PARAMETERS':
      return INITIAL_PARAMETERS_STATE;
 
    default:
      return state;
  }
}
 
/**
 * Parameters Provider Component
 */
export function ParametersProvider({ children }) {
  const [state, dispatch] = useReducer(parametersReducer, INITIAL_PARAMETERS_STATE);
 
  return (
    <ParametersContext.Provider value={state}>
      <ParametersDispatchContext.Provider value={dispatch}>
        {children}
      </ParametersDispatchContext.Provider>
    </ParametersContext.Provider>
  );
}
 
/**
 * Custom hook to access parameters state
 */
export function useParameters() {
  const context = useContext(ParametersContext);
  if (context === null) {
    throw new Error('useParameters must be used within a ParametersProvider');
  }
  return context;
}
 
/**
 * Custom hook to access parameters dispatch
 */
export function useParametersDispatch() {
  const context = useContext(ParametersDispatchContext);
  if (context === null) {
    throw new Error('useParametersDispatch must be used within a ParametersProvider');
  }
  return context;
}
 
/**
 * Custom hook for parameter actions
 */
export function useParametersActions() {
  const dispatch = useParametersDispatch();
 
  const setParameters = useCallback((params) => {
    dispatch({ type: 'SET_PARAMETERS', payload: params });
  }, [dispatch]);
 
  const setBondOrderArrays = useCallback((arrays) => {
    dispatch({ type: 'SET_BOND_ORDER_ARRAYS', payload: arrays });
  }, [dispatch]);
 
  const resetParameters = useCallback(() => {
    dispatch({ type: 'RESET_PARAMETERS' });
  }, [dispatch]);
 
  return {
    setParameters,
    setBondOrderArrays,
    resetParameters,
  };
}
 
export default ParametersContext;