11import React from 'react'
22import { Route , IndexRoute } from 'react-router'
33import clientSettings from '../client/settings'
4- import { fetchProduct , fetchPage , setCategory , receiveSitemap } from './actions'
4+ import { fetchProduct , fetchProducts , fetchPage , setCategory , receiveSitemap } from './actions'
55
66import api from 'cezerin-client' ;
77api . initAjax ( clientSettings . ajaxBaseUrl ) ;
@@ -15,22 +15,44 @@ import CheckoutContainer from './containers/checkout'
1515import CheckoutSuccessContainer from './containers/checkoutSuccess'
1616import NotFoundContainer from './containers/notfound'
1717
18- function checkSitemap ( nextState , cb ) {
18+ const getSitemap = ( path , state , dispatch ) => {
19+ const currentPageFromState = state . app . currentPage ;
20+ // loaded on first request (server side)
21+ const currentPageAlreadyInState = currentPageFromState && currentPageFromState . path === path ;
22+ if ( currentPageAlreadyInState ) {
23+ return Promise . resolve ( { currentPage : currentPageFromState , currentPageAlreadyInState} )
24+ } else {
25+ return api . ajax . sitemap . retrieve ( { path : path } ) . then ( sitemapResponse => {
26+ const currentPageFromRequest = sitemapResponse . json ;
27+ if ( currentPageFromRequest ) {
28+ dispatch ( receiveSitemap ( currentPageFromRequest ) )
29+ }
30+ return ( { currentPage : currentPageFromRequest , currentPageAlreadyInState} ) ;
31+ } )
32+ }
33+ }
34+
35+ function getComponent ( nextState , cb ) {
1936 const { dispatch, getState} = this . store ;
2037 const state = getState ( ) ;
2138
22- api . ajax . sitemap . retrieve ( { path : nextState . location . pathname , enabled : true } ) . then ( sitemapResponse => {
23- if ( sitemapResponse . json ) {
24- dispatch ( receiveSitemap ( sitemapResponse . json ) )
25- if ( sitemapResponse . json . type === 'product-category' ) {
26- dispatch ( setCategory ( sitemapResponse . json . resource ) )
39+ getSitemap ( nextState . location . pathname , state , dispatch ) . then ( ( { currentPage, currentPageAlreadyInState} ) => {
40+ if ( currentPage ) {
41+ if ( currentPage . type === 'product-category' ) {
42+ dispatch ( setCategory ( currentPage . resource ) )
43+ if ( ! currentPageAlreadyInState ) {
44+ dispatch ( fetchProducts ( ) ) ;
45+ }
2746 cb ( null , props => < CategoryContainer { ...props } /> ) ;
28-
29- } else if ( sitemapResponse . json . type === 'product' ) {
30- dispatch ( fetchProduct ( sitemapResponse . json . resource ) )
47+ } else if ( currentPage . type === 'product' ) {
48+ if ( ! currentPageAlreadyInState ) {
49+ dispatch ( fetchProduct ( currentPage . resource ) )
50+ }
3151 cb ( null , props => < ProductContainer { ...props } /> ) ;
32- } else if ( sitemapResponse . json . type === 'page' ) {
33- dispatch ( fetchPage ( sitemapResponse . json . resource ) )
52+ } else if ( currentPage . type === 'page' ) {
53+ if ( ! currentPageAlreadyInState ) {
54+ dispatch ( fetchPage ( currentPage . resource ) )
55+ }
3456 if ( nextState . location . pathname == '/' ) {
3557 cb ( null , IndexContainer ) ;
3658 } else if ( nextState . location . pathname == '/checkout' ) {
@@ -51,9 +73,9 @@ function checkSitemap(nextState, cb) {
5173
5274export default ( store ) => (
5375 < Route path = '/' component = { SharedContainer } >
54- < IndexRoute getComponent = { checkSitemap } store = { store } />
55- < Route path = "/:slug" getComponent = { checkSitemap } store = { store } />
56- < Route path = "/:categorySlug/:productSlug" getComponent = { checkSitemap } store = { store } />
76+ < IndexRoute getComponent = { getComponent } store = { store } />
77+ < Route path = "/:slug" getComponent = { getComponent } store = { store } />
78+ < Route path = "/:categorySlug/:productSlug" getComponent = { getComponent } store = { store } />
5779 < Route path = "*" component = { NotFoundContainer } status = { 404 } />
5880 </ Route >
5981)
0 commit comments