Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
brobro10000 committed Jun 26, 2024
1 parent c670a1c commit 579c610
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/components/EnterpriseApp/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class EnterpriseApp extends React.Component {
enablePortalLearnerCreditManagementScreen={enablePortalLearnerCreditManagementScreen}
>
<BrandStyles enterpriseBranding={enterpriseBranding} />
<div className="enterprise-app" style={{ minHeight: this.context.minHeight }}>
<div className="enterprise-app fill-vertical-space" style={{ minHeight: this.context.minHeight }}>
<MediaQuery minWidth={breakpoints.large.minWidth}>
{matchesMediaQ => (
<>
Expand Down
17 changes: 14 additions & 3 deletions src/components/Footer/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import { configuration } from '../../config';
import Img from '../Img';
import messages from './messages';
import './Footer.scss';
import useGlobalContext from '../GlobalProvider/useGlobalContext';
import { FOOTER_HEIGHT } from '../../data/constants/global';
import { GlobalContext } from '../GlobalProvider';
import { setFooterHeight } from '../../data/actions/global';

class Footer extends React.Component {
// eslint-disable-next-line react/static-property-placement
static contextType = GlobalContext;

ref = createRef();

constructor(props) {
Expand All @@ -22,16 +25,24 @@ class Footer extends React.Component {
}

componentDidMount() {
console.log(this.ref.current.offsetHeight);
const { footerHeight, dispatch } = this.context;
if (this.ref.current?.offsetHeight && footerHeight < this.ref.current?.offsetHeight) {
dispatch(setFooterHeight(this.ref.current.offsetHeight));
}
}

componentDidUpdate(prevProps) {
const { enterpriseLogo } = this.props;
const { footerHeight, dispatch } = this.context;

if (enterpriseLogo && enterpriseLogo !== prevProps.enterpriseLogo) {
this.setState({ // eslint-disable-line react/no-did-update-set-state
enterpriseLogoNotFound: false,
});
}
if (this.ref.current?.offsetHeight && footerHeight < this.ref.current?.offsetHeight) {
dispatch(setFooterHeight(this.ref.current.offsetHeight));
}
}

renderEnterpriseLogo() {
Expand Down
26 changes: 9 additions & 17 deletions src/components/GlobalProvider/index.jsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import { createContext } from 'react';
import { createContext, useMemo, useReducer } from 'react';
import PropTypes from 'prop-types';
import useGlobalContext from './useGlobalContext';
import { globalReducer, initialState } from '../../data/reducers/global';

export const GlobalContext = createContext();

const GlobalContextProvider = ({
refValue,
actionType,
children,
}) => {
const globalContext = useGlobalContext(
{
refValue,
actionType,
},
);
const [globalState, dispatch] = useReducer(globalReducer, initialState);
const globalContext = useMemo(() => ({
...globalState,
// Offsets by an additional 1 rem to avoid rendering the scrollbar unnecessarily
minHeight: `calc(100vh - ${globalState.headerHeight + globalState.footerHeight + 16}px)`,
dispatch,
}), [globalState]);
return (
<GlobalContext.Provider value={globalContext}>
{children}
Expand All @@ -23,14 +22,7 @@ const GlobalContextProvider = ({
};

GlobalContextProvider.propTypes = {
refValue: PropTypes.string,
actionType: PropTypes.string,
children: PropTypes.node.isRequired,
};

GlobalContextProvider.defaultProps = {
refValue: null,
actionType: null,
};

export default GlobalContextProvider;
21 changes: 2 additions & 19 deletions src/components/GlobalProvider/useGlobalContext.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,10 @@
import { useEffect, useMemo, useReducer } from 'react';
import { useMemo, useReducer } from 'react';

import { globalReducer, initialState } from '../../data/reducers/global';
import { FOOTER_HEIGHT, HEADER_HEIGHT } from '../../data/constants/global';
import { footerHeight, headerHeight } from '../../data/actions/global';

function useGlobalContext({
actionType,
refValue,
}) {
function useGlobalContext() {
const [globalState, dispatch] = useReducer(globalReducer, initialState);

useEffect(() => {
switch (actionType) {
case HEADER_HEIGHT:
dispatch(headerHeight(refValue));
break;
case FOOTER_HEIGHT:
dispatch(footerHeight(refValue));
break;
default:
}
}, [actionType, refValue]);

return useMemo(() => ({
...globalState,
minHeight: `calc(100vh - ${globalState.headerHeight + globalState.footerHeight}px)`,
Expand Down
12 changes: 7 additions & 5 deletions src/components/Header/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import Img from '../Img';
import { configuration } from '../../config';

import './Header.scss';
import { headerHeight } from '../../data/actions/global';
import { setHeaderHeight } from '../../data/actions/global';
import { GlobalContext } from '../GlobalProvider';

export const Logo = ({ enterpriseLogo, enterpriseName }) => {
Expand Down Expand Up @@ -87,12 +87,14 @@ const Header = ({
}) => {
const user = getAuthenticatedUser();
const ref = useRef();
const { dispatch } = useContext(GlobalContext);
const { headerHeight, dispatch } = useContext(GlobalContext);

useEffect(() => {
if (ref.current?.offsetHeight && !global.headerHeight) {
dispatch(headerHeight(ref.current?.offsetHeight));
if (ref.current?.offsetHeight && headerHeight < ref.current?.offsetHeight) {
dispatch(setHeaderHeight(ref.current?.offsetHeight));
}
}, [dispatch, ref.current?.offsetHeight]);
}, [dispatch, headerHeight, ref.current?.offsetHeight]);

return (
<header ref={ref} className="container-fluid border-bottom">
<Navbar aria-label="header" className="px-0 py-1 justify-content-between">
Expand Down
11 changes: 4 additions & 7 deletions src/data/actions/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,17 @@ import {
FOOTER_HEIGHT,
} from '../constants/global';

const headerHeight = (refValue = 0) => ({
const setHeaderHeight = (refValue = 0) => ({
type: HEADER_HEIGHT,
payload: { headerHeight: refValue },
});

const footerHeight = (refValue = 0) => ({
const setFooterHeight = (refValue = 0) => ({
type: FOOTER_HEIGHT,
payload: { footerHeight: refValue },
});

const getGlobalHeightFromState = (state) => state;

export {
headerHeight,
footerHeight,
getGlobalHeightFromState,
setHeaderHeight,
setFooterHeight,
};
4 changes: 2 additions & 2 deletions src/data/reducers/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import {
} from '../constants/global';

export const initialState = {
headerHeight: null,
footerHeight: null,
headerHeight: 0,
footerHeight: 0,
};

export const globalReducer = (state = initialState, action) => {
Expand Down
4 changes: 0 additions & 4 deletions src/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ form {
padding-right: 15px;
}

.offset-min-height {
min-height: calc(100vh - 185px);
}

.toast-container {
z-index: $zindex-popover;
}
Expand Down

0 comments on commit 579c610

Please sign in to comment.