@@ -39,29 +39,8 @@ export class Game extends React.Component<GameProps, {}> {
39
39
*/
40
40
public async componentDidMount ( ) {
41
41
const story : any = ( await import ( './game.js' ) ) . default
42
- let storyOpts : Array < string | boolean >
43
42
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 ( )
65
44
story ( this . div , this . canvas , this . props . name , ...storyOpts )
66
45
this . props . handleSaveCanvas ( this . canvas )
67
46
} else {
@@ -78,6 +57,33 @@ export class Game extends React.Component<GameProps, {}> {
78
57
</ div >
79
58
)
80
59
}
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
+ }
81
87
}
82
88
83
89
export default Game
0 commit comments