Refactored app initialization to remove admin url dependency

closes https://github.com/TryGhost/members.js/issues/25

- Remove dependency on admin url for initialization as member endpoints now exist on site directly
- Auto initializes the script on load as need not wait on any external dependency
- API fetch cleanup in parent container
- Updates tests
This commit is contained in:
Rish 2020-05-01 17:06:58 +05:30
parent e291e7075b
commit 2aef0d57f8
3 changed files with 9 additions and 36 deletions

View File

@ -4,7 +4,7 @@ import App from './App';
test('renders App', () => {
const {container} = render(
<App data={{adminUrl: 'https://youradminurl.com'}} />
<App />
);
// dashboard component should be rendered on root route

View File

@ -4,12 +4,8 @@ import setupGhostApi from '../utils/api';
import {ParentContext} from './ParentContext';
const React = require('react');
const PropTypes = require('prop-types');
export default class ParentContainer extends React.Component {
static propTypes = {
data: PropTypes.object.isRequired
};
constructor(props) {
super(props);
@ -47,9 +43,10 @@ export default class ParentContainer extends React.Component {
};
}
async fetchApiData(adminUrl) {
// Fetch site and member session data with Ghost Apis
async fetchData() {
try {
this.GhostApi = setupGhostApi({adminUrl});
this.GhostApi = setupGhostApi();
const {site, member} = await this.GhostApi.init();
const stripeParam = this.getStripeUrlParam();
const {page, showPopup = false} = this.getDefaultPage({member, stripeParam});
@ -63,26 +60,10 @@ export default class ParentContainer extends React.Component {
});
} catch (e) {
/* eslint-disable no-console */
console.error(`[Members.js] Failed to fetch site data, please make sure your admin url - ${adminUrl} - is correct.`);
console.error(`[Members.js] Failed to initialize`);
/* eslint-enable no-console */
this.setState({
action: 'init:failed:incorrectAdminUrl',
initStatus: 'failed'
});
}
}
// Fetch site and member session data with Ghost Apis
fetchData() {
const {adminUrl} = this.props.data;
if (adminUrl) {
this.fetchApiData(adminUrl);
} else {
/* eslint-disable no-console */
console.error(`[Members.js] Failed to initialize, pass a valid admin url.`);
/* eslint-enable no-console */
this.setState({
action: 'init:failed:missingAdminUrl',
action: 'init:failed',
initStatus: 'failed'
});
}

View File

@ -17,23 +17,15 @@ function handleTokenUrl() {
}
}
function init(data) {
function init() {
addRootDiv();
handleTokenUrl();
ReactDOM.render(
<React.StrictMode>
<App data={data} />
<App />
</React.StrictMode>,
document.getElementById('ghost-membersjs-root')
);
}
window.GhostMembers = {
init: init
};
// This will automatically load for local if an .env.development.local file is present
if (process.env.NODE_ENV === 'development') {
const adminUrl = process.env.REACT_APP_ADMIN_URL;
init({adminUrl});
}
init();