|
| 1 | +import React from 'react' |
| 2 | +import { Provider } from 'mobx-react' |
| 3 | +import R from 'ramda' |
| 4 | + |
| 5 | +import GAWraper from '../components/GAWraper' |
| 6 | +import initRootStore from '../stores/init' |
| 7 | +import ThemeWrapper from '../containers/ThemeWrapper' |
| 8 | +import MultiLanguage from '../containers/MultiLanguage' |
| 9 | +import Sidebar from '../containers/Sidebar' |
| 10 | +import Preview from '../containers/Preview' |
| 11 | +import Doraemon from '../containers/Doraemon' |
| 12 | +import Route from '../containers/Route' |
| 13 | +import BodyLayout from '../containers/BodyLayout' |
| 14 | +import Header from '../containers/Header' |
| 15 | +import CommunityBanner from '../containers/CommunityBanner' |
| 16 | +import CommunityContent from '../containers/CommunityContent' |
| 17 | + |
| 18 | +import { |
| 19 | + makeGQClient, |
| 20 | + getMainPath, |
| 21 | + getSubPath, |
| 22 | + BStore, |
| 23 | + ssrPagedSchema, |
| 24 | + ssrPagedContents, |
| 25 | +} from '../utils' |
| 26 | +import Footer from '../components/Footer' |
| 27 | + |
| 28 | +// try to fix safari bug |
| 29 | +// see https://github.com/yahoo/react-intl/issues/422 |
| 30 | +global.Intl = require('intl') |
| 31 | + |
| 32 | +/* |
| 33 | + this function is only needed in dev-mode, it's a bug |
| 34 | + in production, the query for sub-path will go direactly to sub-route file |
| 35 | + */ |
| 36 | +async function fetchData(props) { |
| 37 | + const token = BStore.cookie.from_req(props.req, 'jwtToken') |
| 38 | + const gqClient = makeGQClient(token) |
| 39 | + |
| 40 | + const subpath = getSubPath(props) |
| 41 | + console.log('subpath --> ', subpath) |
| 42 | + |
| 43 | + // const sessionState = gqClient.request(P.sessionState) |
| 44 | + const pagedContents = gqClient.request(ssrPagedSchema(subpath), { |
| 45 | + filter: { page: 1, size: 30 }, |
| 46 | + }) |
| 47 | + |
| 48 | + return { |
| 49 | + ...(await pagedContents), |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/* filter: mergeRouteQuery(query), */ |
| 54 | +export default class Index extends React.Component { |
| 55 | + static async getInitialProps(props) { |
| 56 | + console.log('## community ## index page ..') |
| 57 | + |
| 58 | + const subPath = getSubPath(props) |
| 59 | + const mainPath = getMainPath(props) |
| 60 | + |
| 61 | + let resp |
| 62 | + try { |
| 63 | + resp = await fetchData(props) |
| 64 | + } catch ({ response: { errors } }) { |
| 65 | + /* |
| 66 | + if (ssrAmbulance.hasLoginError(errors)) { |
| 67 | + resp = await fetchData(props, { realname: false }) |
| 68 | + } else { |
| 69 | + return { statusCode: 404, target: subPath } |
| 70 | + } |
| 71 | + */ |
| 72 | + return { statusCode: 404, target: subPath } |
| 73 | + } |
| 74 | + |
| 75 | + // const { pagedCommunities } = resp |
| 76 | + const pagedContents = ssrPagedContents(mainPath, subPath, resp) |
| 77 | + |
| 78 | + return R.merge({ route: { mainPath, subPath } }, pagedContents) |
| 79 | + } |
| 80 | + |
| 81 | + constructor(props) { |
| 82 | + super(props) |
| 83 | + this.store = initRootStore({ ...props }) |
| 84 | + } |
| 85 | + |
| 86 | + render() { |
| 87 | + return ( |
| 88 | + <Provider store={this.store}> |
| 89 | + <GAWraper> |
| 90 | + <ThemeWrapper> |
| 91 | + <Route /> |
| 92 | + <MultiLanguage> |
| 93 | + <Sidebar /> |
| 94 | + <Preview /> |
| 95 | + <Doraemon /> |
| 96 | + <BodyLayout> |
| 97 | + <Header /> |
| 98 | + <CommunityBanner /> |
| 99 | + <CommunityContent /> |
| 100 | + <Footer /> |
| 101 | + </BodyLayout> |
| 102 | + </MultiLanguage> |
| 103 | + </ThemeWrapper> |
| 104 | + </GAWraper> |
| 105 | + </Provider> |
| 106 | + ) |
| 107 | + } |
| 108 | +} |
0 commit comments