Skip to content

Commit fdab2d7

Browse files
committed
feat(core): failsave should now be correct
1 parent 12bdd8d commit fdab2d7

File tree

5 files changed

+28
-21
lines changed

5 files changed

+28
-21
lines changed

actions/auth.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
'use strict';
22

3-
export function login (context) {
3+
export function login (context, payload) {
44
context.dispatch('START_LOGIN');
5-
6-
const data = {
7-
wpm: 10,
8-
accuracy: 80,
9-
textId: 1
10-
};
11-
12-
window.location.href = `/api/auth?data=${JSON.stringify(data)}`;
5+
window.location.href = `/api/auth${ payload ? `data=${JSON.stringify(payload)}` : '' }`;
136
}
147

158
export function checkSession (context, payload, done) {

actions/game.js

+2
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ export function loadText (context, textId, done) {
5959
}
6060

6161
export function submitStats (context, payload, done) {
62+
if (!payload.isLogged) { return context.dispatch('OVERRIDE_FAILSAVE', payload); }
63+
6264
superagent.post(`${context.api._getUrl()}/users/me/game`)
6365
.set('Authorization', `Bearer ${context.api._getToken()}`)
6466
.send(payload)

components/game/index.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import React from 'react';
55
import connectToStores from 'fluxible-addons-react/connectToStores';
66

77
// stores
8-
import GameStore from '../../stores/GameStore';
8+
import { AuthStore, GameStore } from '../../stores';
99

1010
// components
1111
import SourceCode from './SourceCode';
@@ -39,6 +39,7 @@ class Game extends React.Component {
3939
}
4040

4141
componentDidUpdate () {
42+
4243
if (this.props.isFinished) {
4344

4445
// clear tick at the end of the test
@@ -60,7 +61,6 @@ class Game extends React.Component {
6061
this._hasRestartListener = false;
6162
}
6263

63-
6464
}
6565

6666
componentWillUnmount () {
@@ -98,7 +98,8 @@ class Game extends React.Component {
9898
if (this.props.currentWordIndex === this.props.text.words.length - 1) {
9999
this.props.context.executeAction(submitStats, {
100100
textId: this.props.textId,
101-
...this.props.stats
101+
...this.props.stats,
102+
isLogged: this.props.isLogged
102103
});
103104
}
104105
}
@@ -170,8 +171,9 @@ class Game extends React.Component {
170171
}
171172
}
172173

173-
export default connectToStores(Game, [GameStore], context => {
174+
export default connectToStores(Game, [AuthStore, GameStore], context => {
174175
const gameStore = context.getStore(GameStore);
176+
const authStore = context.getStore(AuthStore);
175177
return {
176178
stats: gameStore.getStats(),
177179
text: gameStore.getText(),
@@ -180,6 +182,7 @@ export default connectToStores(Game, [GameStore], context => {
180182
typedWord: gameStore.typedWord(),
181183
isPlaying: gameStore.isPlaying(),
182184
isFinished: gameStore.isFinished(),
183-
isFocused: gameStore.isFocused()
185+
isFocused: gameStore.isFocused(),
186+
isLogged: authStore.isLogged()
184187
};
185188
});

components/ui/GithubButton.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
import React from 'react';
44
import { connectToStores } from 'fluxible-addons-react';
55

6-
import { AuthStore } from '../../stores';
6+
import { AuthStore, GameStore } from '../../stores';
77
import { login, logout } from '../../actions/auth';
88

99
class GithubButton extends React.Component {
1010

1111
handleClick () {
12-
const { isLogged, isLogging } = this.props;
12+
const { isLogged, isLogging, failsave } = this.props;
1313
if (isLogging) { return; }
1414
if (!isLogged) {
15-
this.props.context.executeAction(login);
15+
this.props.context.executeAction(login, failsave);
1616
} else {
1717
this.props.context.executeAction(logout);
1818
}
@@ -45,12 +45,13 @@ class GithubButton extends React.Component {
4545

4646
export default connectToStores(
4747
GithubButton,
48-
[AuthStore],
48+
[AuthStore, GameStore],
4949
context => {
5050
const authStore = context.getStore(AuthStore);
5151
return {
5252
isLogged: authStore.isLogged(),
53-
isLogging: authStore.isLogging()
53+
isLogging: authStore.isLogging(),
54+
failsave: context.getStore(GameStore).getFailsave()
5455
};
5556
}
5657
);

stores/GameStore.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ class GameStore extends BaseStore {
2121
_isPlaying: false,
2222
_isFinished: false,
2323
_isFocused: false,
24-
_startDate: null
24+
_startDate: null,
25+
_failsave: null
2526
});
2627
}
2728

@@ -77,6 +78,7 @@ class GameStore extends BaseStore {
7778
if (!this._startDate) { return 0; }
7879
return moment().diff(this._startDate);
7980
}
81+
getFailsave () { return this._failsave; }
8082
typedLetters () { return this._typedLetters; }
8183
isFetching () { return this._isFetching; }
8284
isPlaying () { return this._isPlaying; }
@@ -178,6 +180,11 @@ class GameStore extends BaseStore {
178180
this.emitChange();
179181
}
180182

183+
handleFailsave (failsave) {
184+
this._failsave = failsave;
185+
this.emitChange();
186+
}
187+
181188
}
182189

183190
GameStore.storeName = 'GameStore';
@@ -192,7 +199,8 @@ GameStore.handlers = {
192199
INPUT_SET_FOCUS: 'handleSetFocus',
193200
BEGIN_TEST: 'handleBeginTest',
194201
UPDATE_WORD: 'handleUpdateWord',
195-
TYPE_WORD: 'handleTypeWord'
202+
TYPE_WORD: 'handleTypeWord',
203+
OVERRIDE_FAILSAVE: 'handleFailsave'
196204
};
197205

198206
export default GameStore;

0 commit comments

Comments
 (0)