From abb939481b5723acb23c73a67a9b820bfde2b16c Mon Sep 17 00:00:00 2001 From: ningOTI Date: Wed, 15 Aug 2018 02:54:44 +0800 Subject: [PATCH] Set a default storyOpt for null stories (#298) * Set a default storyOpt for null stories * Bump version (v1.0.1 -> v1.0.2) --- package.json | 2 +- src/components/academy/game/index.tsx | 50 +++++++++++++++------------ 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 8f7d60684e..aab0fceb49 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "private": true, "name": "cadet-frontend", - "version": "1.0.1", + "version": "1.0.2", "scripts-info": { "format": "Format source code", "start": "Start the Webpack development server", diff --git a/src/components/academy/game/index.tsx b/src/components/academy/game/index.tsx index 717eb71821..345a6b84f2 100644 --- a/src/components/academy/game/index.tsx +++ b/src/components/academy/game/index.tsx @@ -39,29 +39,8 @@ export class Game extends React.Component { */ public async componentDidMount() { const story: any = (await import('./game.js')).default - let storyOpts: Array if (this.props.canvas === undefined) { - // First time rendering the Game component - if (this.props.story) { - storyOpts = [this.props.story.story, !this.props.story.playStory] - } else { - // session.story is undefined if creating store from localStorage - const state = store.getState() - const tokens = { - accessToken: state.session.accessToken!, - refreshToken: state.session.refreshToken! - } - const user: any = await getUser(tokens) - if (user) { - storyOpts = [user.story.story, !user.story.playStory] - store.dispatch(setUser(user)) - } else { - // if user is null, actions.logOut is called anyways; nonetheless we - // set storyOpts, otherwise typescript complains about using storyOpts - // before assignment in story/4 below - storyOpts = ['mission-1', true] - } - } + const storyOpts = await this.getStoryOpts() story(this.div, this.canvas, this.props.name, ...storyOpts) this.props.handleSaveCanvas(this.canvas) } else { @@ -78,6 +57,33 @@ export class Game extends React.Component { ) } + + private async getStoryOpts() { + if (this.props.story) { + // no missions, no story from backend, just play intro + return this.props.story.story + ? [this.props.story.story, !this.props.story.playStory] + : ['mission-1', true] + } else { + // this.props.story is null if creating 'fresh' store from localStorage + const state = store.getState() + const tokens = { + accessToken: state.session.accessToken!, + refreshToken: state.session.refreshToken! + } + const user: any = await getUser(tokens) + if (user) { + store.dispatch(setUser(user)) + // no missions, no story from backend, just play intro + return user.story.story ? [user.story.story, !user.story.playStory] : ['mission-1', true] + } else { + // if user is null, actions.logOut is called anyways; nonetheless we + // return a storyOpts, otherwise typescript complains about using storyOpts + // before assignment in story/4 below + return ['mission-1', true] + } + } + } } export default Game