Skip to content

Commit

Permalink
Set a default storyOpt for null stories (#298)
Browse files Browse the repository at this point in the history
* Set a default storyOpt for null stories

* Bump version (v1.0.1 -> v1.0.2)
  • Loading branch information
ning-y authored Aug 14, 2018
1 parent 3cc51d4 commit abb9394
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
50 changes: 28 additions & 22 deletions src/components/academy/game/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,8 @@ export class Game extends React.Component<GameProps, {}> {
*/
public async componentDidMount() {
const story: any = (await import('./game.js')).default
let storyOpts: Array<string | boolean>
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 {
Expand All @@ -78,6 +57,33 @@ export class Game extends React.Component<GameProps, {}> {
</div>
)
}

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

0 comments on commit abb9394

Please sign in to comment.