Last active
December 1, 2019 01:54
-
-
Save erzr/60fa7ccb42002b4196507485edc9d33d to your computer and use it in GitHub Desktop.
ClientSidePlaceholder for Sitecore JSS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { withSitecoreContext, dataApi, Placeholder } from '@sitecore-jss/sitecore-jss-react'; | |
import { dataFetcher } from './dataFetcher'; | |
import config from './temp/config'; | |
class ClientSidePlaceholder extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
elements: null | |
}; | |
} | |
fetchPlaceholder(placeholderName, itemLanguage, itemId) { | |
return dataApi.fetchPlaceholderData(placeholderName, itemId, { | |
layoutServiceConfig: { host: config.sitecoreApiHost }, | |
querystringParams: { sc_lang: itemLanguage, sc_apikey: config.sitecoreApiKey }, | |
fetcher: dataFetcher, | |
}); | |
} | |
componentDidMount() { | |
const shouldFetch = this.shouldFetchPlaceholder(); | |
if (shouldFetch) { | |
const placeholderName = this.props.name; | |
const { route } = this.props.sitecoreContext; | |
const { itemLanguage, itemId } = route; | |
this.fetchPlaceholder(placeholderName, itemLanguage, itemId) | |
.then(result => { | |
this.setState({ | |
elements: result.elements | |
}); | |
}) | |
} | |
} | |
render() { | |
const rendering = { | |
...this.props.rendering | |
}; | |
rendering.placeholders[this.props.name] = this.state.elements ? this.state.elements : | |
this.props.hideInitialContents ? [] : rendering.placeholders[this.props.name]; | |
const placeholderProps = { | |
...this.props, | |
rendering | |
} | |
return <Placeholder {...placeholderProps} /> | |
} | |
isClientside() { | |
return typeof window !== 'undefined'; | |
} | |
isDisconnectedMode() { | |
const disconnectedMode = this.props.sitecoreContext.site.name === 'JssDisconnectedLayoutService'; | |
return disconnectedMode; | |
} | |
isPageEditing() { | |
const isEditing = this.props.sitecoreContext.pageEditing; | |
return isEditing; | |
} | |
shouldFetchPlaceholder() { | |
return this.isClientside() && !this.isDisconnectedMode() && !this.isPageEditing(); | |
} | |
} | |
export default withSitecoreContext()(ClientSidePlaceholder); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment