mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-02-27 02:03:44 -05:00
<!-- Please follow this checklist and put an x in each of the boxes, like this: [x]. It will ensure that our team takes your pull request seriously. --> - [x] I have read [freeCodeCamp's contribution guidelines](https://github.com/freeCodeCamp/freeCodeCamp/blob/master/CONTRIBUTING.md). - [x] My pull request has a descriptive title (not a vague title like `Update index.md`) - [x] My pull request targets the `master` branch of freeCodeCamp.
96 lines
2.2 KiB
JavaScript
96 lines
2.2 KiB
JavaScript
import React, { PureComponent, createContext } from 'react';
|
|
import PropTypes from 'prop-types';
|
|
|
|
const noop = () => {};
|
|
|
|
const initialState = { displaySideNav: false, expandedState: {} };
|
|
|
|
const { Provider, Consumer } = createContext({
|
|
...initialState,
|
|
toggleDisplaySideNav: () => {
|
|
console.warn('no method from provider');
|
|
},
|
|
toggleExpandedState: () => {
|
|
console.warn('no method from provider');
|
|
}
|
|
});
|
|
|
|
const propTypes = {
|
|
children: PropTypes.any
|
|
};
|
|
|
|
class NavigationContextProvider extends PureComponent {
|
|
constructor(...props) {
|
|
super(...props);
|
|
|
|
this.state = { ...initialState };
|
|
|
|
this.toggleSideNav = this.toggleSideNav.bind(this);
|
|
this.toggleExpandedState = this.toggleExpandedState.bind(this);
|
|
}
|
|
|
|
componentDidMount() {
|
|
if (typeof window !== 'undefined') {
|
|
const pathMap = window.location.pathname
|
|
.slice(1)
|
|
.split('/')
|
|
.slice(0, -1)
|
|
.reduce((map, current, i, pathArray) => {
|
|
const path =
|
|
i !== 0 ? map[pathArray[i - 1]] + `/${current}` : `/${current}`;
|
|
return {
|
|
...map,
|
|
[current]: path
|
|
};
|
|
}, {});
|
|
|
|
return Object.keys(pathMap)
|
|
.map(key => pathMap[key])
|
|
.forEach(path => {
|
|
this.toggleExpandedState(path);
|
|
});
|
|
}
|
|
return null;
|
|
}
|
|
|
|
toggleExpandedState(path) {
|
|
return this.setState(state => ({
|
|
...state,
|
|
expandedState: {
|
|
...state.expandedState,
|
|
[path]: !state.expandedState[path]
|
|
}
|
|
}));
|
|
}
|
|
|
|
toggleSideNav() {
|
|
return this.setState(state => ({
|
|
...state,
|
|
displaySideNav: !this.state.displaySideNav
|
|
}));
|
|
}
|
|
|
|
render() {
|
|
const { children } = this.props;
|
|
const { displaySideNav, expandedState } = this.state;
|
|
return (
|
|
<Provider
|
|
value={{
|
|
displaySideNav,
|
|
expandedState,
|
|
toggleDisplaySideNav: noop,
|
|
toggleExpandedState: this.toggleExpandedState
|
|
}}
|
|
>
|
|
{children}
|
|
</Provider>
|
|
);
|
|
}
|
|
}
|
|
|
|
NavigationContextProvider.displayName = 'NavigationContextProvider';
|
|
NavigationContextProvider.propTypes = propTypes;
|
|
|
|
export const NavigationContext = Consumer;
|
|
export default NavigationContextProvider;
|