From 134cd927ec4bba15cf8cde3c633bcb60236d742d Mon Sep 17 00:00:00 2001 From: Paul Kaplan Date: Thu, 13 May 2021 13:20:26 -0400 Subject: [PATCH] Add classroomId to studio reducer --- src/redux/session.js | 1 + src/redux/studio.js | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/redux/session.js b/src/redux/session.js index a9886ebfe..36ce89927 100644 --- a/src/redux/session.js +++ b/src/redux/session.js @@ -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); diff --git a/src/redux/studio.js b/src/redux/studio.js index 29cfb39b1..2cbb80394 100644 --- a/src/redux/studio.js +++ b/src/redux/studio.js @@ -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 };