0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-04-01 02:41:39 -05:00

Added site data initilization through APIs

refs https://github.com/TryGhost/members.js/issues/6

Use member identity and public site data admin API to fetch site data which was previously being passed down from the theme. This change allows us to be directly fetch all relevant site data except adminUrl and use it to initialize the flows.

Also adds a basic loading behavior till we finish fetching site and member logged-in data from API.
This commit is contained in:
Rish 2020-04-17 14:53:10 +05:30
parent 0c0e1069ae
commit 066ec7fcae

View file

@ -13,23 +13,50 @@ export default class ParentContainer extends React.Component {
console.log('Initialized script with data', props.data);
// Setup Members API with site/admin URLs
const {siteUrl, adminUrl} = props.data.site;
const {siteUrl, adminUrl} = props.data;
this.MembersAPI = setupMembersApi({siteUrl, adminUrl});
// Setup custom trigger button handling
this.customTriggerButton = document.querySelector('[data-members-trigger-button]');
this.setupCustomTriggerButton(this.customTriggerButton);
const page = this.isMemberLoggedIn() ? 'signedin' : 'signin';
// Initialize site and members data
this.initData();
const page = 'loading';
this.state = {
page,
showPopup: false,
action: null
action: {
name: 'loading'
}
};
}
isMemberLoggedIn() {
return !!this.props.data.member;
async initData() {
try {
const memberIdentity = await this.MembersAPI.getMemberIdentity();
const {site} = await this.MembersAPI.getSiteData();
const isMemberLoggedIn = !!memberIdentity;
this.setState({
isMemberLoggedIn,
site,
page: isMemberLoggedIn ? 'signedin' : 'signup'
});
} catch (e) {
this.setState({
action: {
name: 'loadingFailed'
}
});
}
}
getData() {
const member = this.props.data.member;
const site = this.state.site;
return {site, member};
}
switchPage(page) {
@ -126,7 +153,7 @@ export default class ParentContainer extends React.Component {
if (this.state.showPopup) {
return (
<PopupMenuComponent
data={this.props.data}
data={this.getData()}
action={this.state.action}
page={this.state.page}
switchPage={page => this.switchPage(page)}
@ -144,7 +171,7 @@ export default class ParentContainer extends React.Component {
name={this.props.name}
onToggle= {e => this.onTriggerToggle()}
isPopupOpen={this.state.showPopup}
data={this.props.data}
data={this.getData()}
/>
);
}