department-of-veterans-affairs/vets-website

View on GitHub
src/applications/edu-benefits/hooks/useSetToggleParam.js

Summary

Maintainability
A
25 mins
Test Coverage
import { useEffect } from 'react';

export const useSetToggleParam = toggleValue => {
  useEffect(
    () => {
      const urlParams = new URLSearchParams(window.location.search);
      if (toggleValue != null && toggleValue !== 'undefined') {
        urlParams.set('toggle', toggleValue);
        const newUrl = `${window.location.origin}${
          window.location.pathname
        }?${urlParams.toString()}`;
        if (window.location.href !== newUrl) {
          window.location.href = newUrl;
        }
      }
    },
    [toggleValue],
  );
};