// #region [Imports] =================================================================================================== // Libraries import { useEffect, useState } from 'react'; import { useHistory } from 'react-router-dom'; import { bindActionCreators, Dispatch } from 'redux'; import { connect } from 'react-redux'; // Actions import { PageActions } from '../../store/actions/page'; // Types import { IStore } from '../../types/store'; import { ISingleNotice } from '../../types/notices'; // Helpers import { getPathPrefix } from '../../helpers/utils'; // SCSS import './index.scss'; // #endregion [Imports] // #region [Variables] ================================================================================================= declare var acfwAdminApp: any; const { setStorePage } = PageActions; // #endregion [Variables] // #region [Interfaces]================================================================================================= interface IActions { setStorePage: typeof setStorePage; } interface IProps { page: string; notices: ISingleNotice[]; actions: IActions; } // #endregion [Interfaces] // #region [Component] ================================================================================================= const CouponsNav = (props: IProps) => { const { page, notices, actions } = props; const [prevPage, setPrevPage] = useState(''); const history = useHistory(); const pathPrefix = getPathPrefix(); const { coupon_nav: { toplevel, dashboard, links }, app_pages, } = acfwAdminApp; /** * Handle menu click event. * * @param {string} id Page ID/Slug */ const handleMenuClick = (id: string) => { history.push(`${pathPrefix}admin.php?page=${id}`); actions.setStorePage({ data: id }); // Notify the main app's BrowserRouter of the URL change. // The sidebar nav and main app use separate BrowserRouter instances, // and history.push() (pushState) does not fire popstate, so the main // app's router won't see the new URL without this event. window.dispatchEvent(new PopStateEvent('popstate')); }; /** * Add page slug to the body's class list. */ useEffect(() => { document.body.classList.add(page); if (prevPage) document.body.classList.remove(prevPage); setPrevPage(page); }, [page]); return ( <>

{toplevel} {0 < notices.length && ( <>   {notices.length} )}
); }; const mapStateToProps = (store: IStore) => ({ page: store.page, notices: store.adminNotices, }); const mapDispatchToProps = (dispatch: Dispatch) => ({ actions: bindActionCreators({ setStorePage }, dispatch), }); export default connect(mapStateToProps, mapDispatchToProps)(CouponsNav); // #endregion [Component]