Skip to content

Commit 8845d5c

Browse files
committed
feat: keep language when creating new user
settings with language setting available when disconnected
1 parent cff9f83 commit 8845d5c

File tree

8 files changed

+67
-46
lines changed

8 files changed

+67
-46
lines changed

public/app/config/config.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const PRODUCT_NAME = 'Graasp';
4343
const TMP_FOLDER = 'tmp';
4444
const DEFAULT_LANG = 'en';
4545
const DEFAULT_DEVELOPER_MODE = false;
46-
const TEACHER_USER_MODE = 'teacher';
46+
const TEACHER_USER_MODE = 'student';
4747
const DEFAULT_USER_MODE = TEACHER_USER_MODE;
4848
const DEFAULT_GEOLOCATION_ENABLED = false;
4949
const VISUAL_SYNC_MODE = 'visual';
@@ -55,10 +55,10 @@ const DEFAULT_AUTHENTICATION = false;
5555
const DEFAULT_ACTION_ACCESSIBILITY = false;
5656
const DEFAULT_ACTIONS_AS_ENABLED = true;
5757

58-
const DEFAULT_USER = {
58+
const DEFAULT_USER = (lang = DEFAULT_LANG) => ({
5959
geolocation: null,
6060
settings: {
61-
lang: DEFAULT_LANG,
61+
lang,
6262
developerMode: DEFAULT_DEVELOPER_MODE,
6363
geolocationEnabled: DEFAULT_GEOLOCATION_ENABLED,
6464
syncMode: DEFAULT_SYNC_MODE,
@@ -68,7 +68,7 @@ const DEFAULT_USER = {
6868
},
6969
favoriteSpaces: [],
7070
recentSpaces: [],
71-
};
71+
});
7272

7373
const ANONYMOUS_USERNAME = 'Anonymous';
7474

public/app/listeners/signIn.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,48 @@
11
const ObjectId = require('bson-objectid');
2-
const _ = require('lodash');
32
const { USERS_COLLECTION } = require('../db');
43
const { SIGN_IN_CHANNEL } = require('../config/channels');
54
const {
65
DEFAULT_USER,
76
ANONYMOUS_USERNAME,
87
AUTHENTICATED,
8+
DEFAULT_LANG,
99
} = require('../config/config');
1010

11-
const createNewUser = (username, createdAt, anonymous = false) => {
11+
const createNewUser = (
12+
username,
13+
createdAt,
14+
lang = DEFAULT_LANG,
15+
anonymous = false
16+
) => {
1217
const id = ObjectId().str;
1318

1419
return {
1520
id,
1621
username,
1722
createdAt,
1823
anonymous,
19-
..._.cloneDeep(DEFAULT_USER),
24+
...DEFAULT_USER(lang),
2025
};
2126
};
2227

2328
const signIn = (mainWindow, db) => async (
2429
event,
25-
{ username, anonymous = false }
30+
{ username, lang = DEFAULT_LANG, anonymous = false }
2631
) => {
2732
const users = db.get(USERS_COLLECTION);
2833

2934
const now = new Date();
3035

3136
let user;
3237
if (anonymous) {
33-
user = createNewUser(ANONYMOUS_USERNAME, now, anonymous);
38+
user = createNewUser(ANONYMOUS_USERNAME, now, lang, anonymous);
3439
users.push(user).write();
3540
} else {
3641
// check in db if username exists
3742
user = users.find({ username }).value();
3843

3944
if (!user) {
40-
user = createNewUser(username, now);
45+
user = createNewUser(username, now, lang);
4146
users.push(user).write();
4247
}
4348
}

src/App.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,7 @@ export class App extends Component {
194194
path={LOAD_SELECTION_SPACE_PATH}
195195
component={Authorization()(LoadSelectionScreen)}
196196
/>
197-
<Route
198-
exact
199-
path={SETTINGS_PATH}
200-
component={Authorization()(Settings)}
201-
/>
197+
<Route exact path={SETTINGS_PATH} component={Settings} />
202198
<Route
203199
exact
204200
path={SYNC_SPACE_PATH}

src/__snapshots__/App.test.js.snap

+1-9
Original file line numberDiff line numberDiff line change
@@ -388,15 +388,7 @@ exports[`<App /> renders correctly 1`] = `
388388
path="/load-space/selection"
389389
/>
390390
<Route
391-
component={
392-
Object {
393-
"$$typeof": Symbol(react.memo),
394-
"WrappedComponent": [Function],
395-
"compare": null,
396-
"displayName": "Connect(ComposedComponent)",
397-
"type": [Function],
398-
}
399-
}
391+
component={[Function]}
400392
exact={true}
401393
path="/settings"
402394
/>

src/actions/authentication.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ const flagSigningIn = createFlag(FLAG_SIGNING_IN);
2727
const flagSigningOut = createFlag(FLAG_SIGNING_OUT);
2828
const flagGettingAuthenticated = createFlag(FLAG_GETTING_AUTHENTICATED);
2929

30-
const signIn = async ({ username, anonymous }) => async dispatch => {
30+
const signIn = async ({ username, lang, anonymous }) => async dispatch => {
3131
try {
3232
dispatch(flagSigningIn(true));
33-
window.ipcRenderer.send(SIGN_IN_CHANNEL, { username, anonymous });
33+
window.ipcRenderer.send(SIGN_IN_CHANNEL, { username, lang, anonymous });
3434
window.ipcRenderer.once(SIGN_IN_CHANNEL, async (event, user) => {
3535
if (user === ERROR_GENERAL) {
3636
toastr.error(ERROR_MESSAGE_HEADER, ERROR_SIGNING_IN);

src/components/Settings.js

+36-14
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import SyncAdvancedSwitch from './space/sync/SyncAdvancedSwitch';
1717
import StudentModeSwitch from './common/StudentModeSwitch';
1818
import ActionEnabledSwitch from './common/ActionEnabledSwitch';
1919
import ActionAccessibilitySwitch from './common/ActionAccessibilitySwitch';
20-
import { USER_MODES } from '../config/constants';
20+
import { USER_MODES, DEFAULT_USER_MODE } from '../config/constants';
2121

2222
const styles = theme => ({
2323
...Styles(theme),
@@ -43,10 +43,31 @@ export class Settings extends Component {
4343
changeLanguage: PropTypes.func.isRequired,
4444
}).isRequired,
4545
userMode: PropTypes.oneOf(Object.values(USER_MODES)).isRequired,
46+
authenticated: PropTypes.bool.isRequired,
47+
};
48+
49+
renderDeveloperModeSwitch = () => {
50+
const { userMode } = this.props;
51+
return userMode === USER_MODES.TEACHER ? <DeveloperSwitch /> : null;
52+
};
53+
54+
renderActionSettings = () => {
55+
const { t } = this.props;
56+
return (
57+
<>
58+
<Typography variant="h5" color="inherit" className="mt-2">
59+
{t('Actions')}
60+
</Typography>
61+
<FormGroup>
62+
<ActionEnabledSwitch />
63+
<ActionAccessibilitySwitch />
64+
</FormGroup>
65+
</>
66+
);
4667
};
4768

4869
render() {
49-
const { classes, t, userMode } = this.props;
70+
const { classes, t, authenticated } = this.props;
5071

5172
return (
5273
<Main id={SETTINGS_MAIN_ID}>
@@ -56,27 +77,28 @@ export class Settings extends Component {
5677
</Typography>
5778
<FormGroup>
5879
<LanguageSelect />
59-
<GeolocationControl />
60-
<SyncAdvancedSwitch />
61-
<StudentModeSwitch />
62-
{userMode === USER_MODES.TEACHER ? <DeveloperSwitch /> : null}
80+
{authenticated && (
81+
<>
82+
<GeolocationControl />
83+
<SyncAdvancedSwitch />
84+
<StudentModeSwitch />
85+
{this.renderDeveloperModeSwitch()}
86+
</>
87+
)}
6388
</FormGroup>
6489
<Divider variant="middle" classes={{ root: classes.divider }} />
65-
<Typography variant="h5" color="inherit" className="mt-2">
66-
{t('Actions')}
67-
</Typography>
68-
<FormGroup>
69-
<ActionEnabledSwitch />
70-
<ActionAccessibilitySwitch />
71-
</FormGroup>
90+
91+
{authenticated && this.renderActionSettings()}
7292
</div>
7393
</Main>
7494
);
7595
}
7696
}
7797

7898
const mapStateToProps = ({ authentication }) => ({
79-
userMode: authentication.getIn(['user', 'settings', 'userMode']),
99+
authenticated: authentication.getIn(['authenticated']),
100+
userMode:
101+
authentication.getIn(['user', 'settings', 'userMode']) || DEFAULT_USER_MODE,
80102
});
81103

82104
const ConnectedComponent = connect(mapStateToProps, null)(Settings);

src/components/Settings.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const createSettingsProps = () => {
3333
changeLanguage: jest.fn(),
3434
},
3535
userMode: USER_MODES.TEACHER,
36+
authenticated: true,
3637
};
3738
};
3839

src/components/signin/SignInScreen.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import PropTypes from 'prop-types';
1212
import logo from '../../logo.svg';
1313
import Styles from '../../Styles';
1414
import { signIn } from '../../actions/authentication';
15-
import { AUTHENTICATED, ACTION_VERBS } from '../../config/constants';
15+
import {
16+
AUTHENTICATED,
17+
ACTION_VERBS,
18+
DEFAULT_LANGUAGE,
19+
} from '../../config/constants';
1620
import { HOME_PATH } from '../../config/paths';
1721
import Main from '../common/Main';
1822
import {
@@ -96,6 +100,7 @@ class SignInScreen extends Component {
96100
authenticated: PropTypes.bool.isRequired,
97101
actionsEnabled: PropTypes.bool.isRequired,
98102
user: PropTypes.instanceOf(Map),
103+
lang: PropTypes.string.isRequired,
99104
};
100105

101106
static defaultProps = {
@@ -136,15 +141,15 @@ class SignInScreen extends Component {
136141

137142
handleSignIn = () => {
138143
const { username } = this.state;
139-
const { dispatchSignIn, actionsEnabled } = this.props;
144+
const { dispatchSignIn, actionsEnabled, lang } = this.props;
140145
if (username.length) {
141-
dispatchSignIn({ username, actionsEnabled });
146+
dispatchSignIn({ username, actionsEnabled, lang });
142147
}
143148
};
144149

145150
handleAnonymousSignIn = () => {
146-
const { dispatchSignIn } = this.props;
147-
dispatchSignIn({ anonymous: true });
151+
const { dispatchSignIn, lang } = this.props;
152+
dispatchSignIn({ anonymous: true, lang });
148153
};
149154

150155
handleUsername = event => {
@@ -216,7 +221,7 @@ class SignInScreen extends Component {
216221
}
217222

218223
const mapStateToProps = ({ authentication }) => ({
219-
user: authentication.getIn(['user']),
224+
lang: authentication.getIn(['user', 'settings', 'lang']) || DEFAULT_LANGUAGE,
220225
authenticated: authentication.getIn(['authenticated']) === AUTHENTICATED,
221226
actionsEnabled: authentication.getIn(['user', 'settings', 'actionsEnabled']),
222227
});

0 commit comments

Comments
 (0)