Skip to content

Commit abb9394

Browse files
authored
Set a default storyOpt for null stories (#298)
* Set a default storyOpt for null stories * Bump version (v1.0.1 -> v1.0.2)
1 parent 3cc51d4 commit abb9394

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "cadet-frontend",
4-
"version": "1.0.1",
4+
"version": "1.0.2",
55
"scripts-info": {
66
"format": "Format source code",
77
"start": "Start the Webpack development server",

src/components/academy/game/index.tsx

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,29 +39,8 @@ export class Game extends React.Component<GameProps, {}> {
3939
*/
4040
public async componentDidMount() {
4141
const story: any = (await import('./game.js')).default
42-
let storyOpts: Array<string | boolean>
4342
if (this.props.canvas === undefined) {
44-
// First time rendering the Game component
45-
if (this.props.story) {
46-
storyOpts = [this.props.story.story, !this.props.story.playStory]
47-
} else {
48-
// session.story is undefined if creating store from localStorage
49-
const state = store.getState()
50-
const tokens = {
51-
accessToken: state.session.accessToken!,
52-
refreshToken: state.session.refreshToken!
53-
}
54-
const user: any = await getUser(tokens)
55-
if (user) {
56-
storyOpts = [user.story.story, !user.story.playStory]
57-
store.dispatch(setUser(user))
58-
} else {
59-
// if user is null, actions.logOut is called anyways; nonetheless we
60-
// set storyOpts, otherwise typescript complains about using storyOpts
61-
// before assignment in story/4 below
62-
storyOpts = ['mission-1', true]
63-
}
64-
}
43+
const storyOpts = await this.getStoryOpts()
6544
story(this.div, this.canvas, this.props.name, ...storyOpts)
6645
this.props.handleSaveCanvas(this.canvas)
6746
} else {
@@ -78,6 +57,33 @@ export class Game extends React.Component<GameProps, {}> {
7857
</div>
7958
)
8059
}
60+
61+
private async getStoryOpts() {
62+
if (this.props.story) {
63+
// no missions, no story from backend, just play intro
64+
return this.props.story.story
65+
? [this.props.story.story, !this.props.story.playStory]
66+
: ['mission-1', true]
67+
} else {
68+
// this.props.story is null if creating 'fresh' store from localStorage
69+
const state = store.getState()
70+
const tokens = {
71+
accessToken: state.session.accessToken!,
72+
refreshToken: state.session.refreshToken!
73+
}
74+
const user: any = await getUser(tokens)
75+
if (user) {
76+
store.dispatch(setUser(user))
77+
// no missions, no story from backend, just play intro
78+
return user.story.story ? [user.story.story, !user.story.playStory] : ['mission-1', true]
79+
} else {
80+
// if user is null, actions.logOut is called anyways; nonetheless we
81+
// return a storyOpts, otherwise typescript complains about using storyOpts
82+
// before assignment in story/4 below
83+
return ['mission-1', true]
84+
}
85+
}
86+
}
8187
}
8288

8389
export default Game

0 commit comments

Comments
 (0)