From f17f156150c888d3e71b5b52c6f0ab3124aaf9ac Mon Sep 17 00:00:00 2001 From: Benjy Cui Date: Mon, 9 May 2016 11:34:45 +0800 Subject: [PATCH] site: fix 404 --- site/component/Header/index.jsx | 33 +++++++++++++++++++++------------ site/entry/index.jsx | 6 ++++++ site/entry/utils.js | 23 +++++++++++++++++++---- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/site/component/Header/index.jsx b/site/component/Header/index.jsx index 21fd3ed748..10f8e6a98d 100644 --- a/site/component/Header/index.jsx +++ b/site/component/Header/index.jsx @@ -17,15 +17,7 @@ export default class Header extends React.Component { constructor(props) { super(props); - this.state = { - menuVisible: false, - menuMode: 'horizontal', - isFirstFrame: true, - }; - } - - componentDidMount() { - window.addEventListener('scroll', debounce(() => { + this.onScroll = debounce(() => { const scrollTop = document.body.scrollTop || document.documentElement.scrollTop; const clientHeight = document.documentElement.clientHeight; if (scrollTop >= clientHeight) { @@ -33,13 +25,25 @@ export default class Header extends React.Component { } else { this.setState({ isFirstFrame: true }); } - }, 100)); + }, 100); - document.addEventListener('click', () => { + this.onDocumentClick = () => { this.setState({ menuVisible: false, }); - }); + }; + + this.state = { + menuVisible: false, + menuMode: 'horizontal', + isFirstFrame: true, + }; + } + + componentDidMount() { + window.addEventListener('scroll', this.onScroll); + + document.addEventListener('click', this.onDocumentClick); enquire.register('only screen and (min-width: 320px) and (max-width: 767px)', { match: () => { @@ -51,6 +55,11 @@ export default class Header extends React.Component { }); } + componentWillUnmount() { + window.removeEventListener('scroll', this.onScroll); + document.removeEventListener('click', this.onDocumentClick); + } + handleMenuIconClick = (e) => { e.stopPropagation(); e.nativeEvent.stopImmediatePropagation(); diff --git a/site/entry/index.jsx b/site/entry/index.jsx index 0700acc55c..8315962584 100644 --- a/site/entry/index.jsx +++ b/site/entry/index.jsx @@ -52,32 +52,38 @@ ReactDOM.render( {utils.generateIndex(reactComponents)} {redirects} {utils.generateIndex(reactComponents)} {utils.generateIndex(practice)} {utils.generateIndex(pattern)} {utils.generateIndex(spec)} {utils.generateIndex(resource)} diff --git a/site/entry/utils.js b/site/entry/utils.js index a6bbb466b3..dabfdcb433 100644 --- a/site/entry/utils.js +++ b/site/entry/utils.js @@ -61,15 +61,30 @@ export function generateIndex(data) { const pathToFile = {}; Object.keys(redirects).forEach((key) => pathToFile[redirects[key]] = key); pathToFile['docs/react/changelog'] = './CHANGELOG'; // TODO + +function getDoc(data, props) { + const trimedPathname = props.location.pathname.replace(/^\//, ''); + const processedPathname = pathToFile[trimedPathname] || trimedPathname; + const doc = data[`${processedPathname}.md`] || + data[`${processedPathname}/index.md`]; + return doc; +} + export function getChildrenWrapper(data) { return function childrenWrapper(props) { - const trimedPathname = props.location.pathname.replace(/^\//, ''); - const processedPathname = pathToFile[trimedPathname] || trimedPathname; - const doc = data[`${processedPathname}.md`] || - data[`${processedPathname}/index.md`]; + const doc = getDoc(data, props); const hasDemos = demosList[doc.meta.fileName]; return !hasDemos ?
: ; }; } + +export function getEnterHandler(data) { + return function handleEnter(nextState, replace) { + const doc = getDoc(data, nextState); + if (!doc) { + replace('/404'); + } + }; +}