Add classroomId to studio reducer

This commit is contained in:
Paul Kaplan 2021-05-13 13:20:26 -04:00
parent fd6215d581
commit 134cd927ec
2 changed files with 16 additions and 2 deletions

View file

@ -127,6 +127,7 @@ module.exports.selectUsername = state => get(state, ['session', 'session', 'user
module.exports.selectToken = state => get(state, ['session', 'session', 'user', 'token'], null);
module.exports.selectIsAdmin = state => get(state, ['session', 'session', 'permissions', 'admin'], false);
module.exports.selectIsSocial = state => get(state, ['session', 'session', 'permissions', 'social'], false);
module.exports.selectIsEducator = state => get(state, ['session', 'session', 'permissions', 'educator'], false);
// NB logged out user id as NaN so that it can never be used in equality testing since NaN !== NaN
module.exports.selectUserId = state => get(state, ['session', 'session', 'user', 'id'], NaN);

View file

@ -3,7 +3,7 @@ const keyMirror = require('keymirror');
const api = require('../lib/api');
const log = require('../lib/log');
const {selectUsername, selectToken} = require('./session');
const {selectUsername, selectToken, selectIsEducator} = require('./session');
const Status = keyMirror({
FETCHED: null,
@ -22,6 +22,9 @@ const getInitialState = () => ({
followers: 0,
owner: null,
// BEWARE: classroomId is only loaded if the user is an educator
classroomId: null,
rolesStatus: Status.NOT_FETCHED,
manager: false,
curator: false,
@ -95,6 +98,7 @@ const selectStudioLoadFailed = state => state.studio.infoStatus === Status.ERROR
const selectIsFetchingInfo = state => state.studio.infoStatus === Status.FETCHING;
const selectIsFollowing = state => state.studio.following;
const selectIsFetchingRoles = state => state.studio.rolesStatus === Status.FETCHING;
const selectClassroomId = state => state.studio.classroomId;
// Thunks
const getInfo = () => ((dispatch, getState) => {
@ -138,6 +142,14 @@ const getRoles = () => ((dispatch, getState) => {
invited: body.invited
}));
});
// Since the user is now loaded, it's a good time to check if the studio is part of a classroom
if (selectIsEducator(state)) {
api({uri: `/studios/${studioId}/classroom`}, (err, body, res) => {
// No error states for inability/problems loading classroom, just swallow them
if (!err && res.statusCode === 200 && body) dispatch(setInfo({classroomId: body.id}));
});
}
});
module.exports = {
@ -161,5 +173,6 @@ module.exports = {
selectStudioLoadFailed,
selectIsFetchingInfo,
selectIsFetchingRoles,
selectIsFollowing
selectIsFollowing,
selectClassroomId
};