From 0c00a22463f8f411bd062d18b354511b2881175c Mon Sep 17 00:00:00 2001 From: Matthew Stratford Date: Mon, 31 May 2021 22:03:33 +0100 Subject: [PATCH] Re-add myradio session --- src/session/state.ts | 111 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/session/state.ts diff --git a/src/session/state.ts b/src/session/state.ts new file mode 100644 index 0000000..9c66576 --- /dev/null +++ b/src/session/state.ts @@ -0,0 +1,111 @@ +import { createSlice, PayloadAction } from "@reduxjs/toolkit"; +import { AppThunk } from "../store"; +import { + User, + getCurrentApiUser, + Timeslot, + getCurrentApiTimeslot, + doesCurrentUserHavePermission, +} from "../api"; + +import * as Sentry from "@sentry/react"; + +const BROADCAST_PERMISSION_ID = 340; + +interface sessionState { + currentUser: User | null; + currentTimeslot: Timeslot | null; + userCanBroadcast: boolean; + userLoading: boolean; + userLoadError: string | null; + timeslotLoading: boolean; + timeslotLoadError: string | null; +} + +const sessionState = createSlice({ + name: "Session", + initialState: { + currentUser: null, + currentTimeslot: null, + userCanBroadcast: false, + userLoading: false, + userLoadError: null, + timeslotLoading: false, + timeslotLoadError: null, + } as sessionState, + reducers: { + setCurrentUser( + state, + action: PayloadAction<{ user: User | null; canBroadcast: boolean }> + ) { + state.userLoading = false; + state.userLoadError = null; + state.currentUser = action.payload.user; + state.userCanBroadcast = action.payload.canBroadcast; + }, + getUserStarting(state) { + state.userLoadError = null; + state.userLoading = true; + }, + getUserError(state, action: PayloadAction) { + state.userLoading = false; + state.userLoadError = action.payload; + }, + getTimeslotStarting(state) { + state.timeslotLoadError = null; + state.timeslotLoading = true; + }, + getTimeslotSuccess(state, action: PayloadAction) { + console.log("Getting timeslot succeeded."); + state.timeslotLoading = false; + state.timeslotLoadError = null; + state.currentTimeslot = action.payload; + }, + getTimeslotError(state, action: PayloadAction) { + state.timeslotLoading = false; + state.timeslotLoadError = action.payload; + }, + getState(state) { + return state; + }, + }, +}); + +export default sessionState.reducer; + +export const { setCurrentUser, getUserError } = sessionState.actions; + +export const getCurrentUser = (): AppThunk => async (dispatch, getState) => { + return getState().session.currentUser; +}; + +export const getUser = (): AppThunk => async (dispatch) => { + dispatch(sessionState.actions.getUserStarting()); + try { + const [user, canBroadcast] = await Promise.all([ + getCurrentApiUser(), + doesCurrentUserHavePermission(BROADCAST_PERMISSION_ID), + ]); + Sentry.setUser({ + id: user.memberid.toString(10), + email: user.public_email, + username: user.fname + " " + user.sname, + }); + dispatch(sessionState.actions.setCurrentUser({ user, canBroadcast })); + } catch (e) { + console.log("Failed to get user. " + e.toString()); + dispatch(sessionState.actions.getUserError(e.toString())); + } +}; + +export const getTimeslot = (): AppThunk => async (dispatch) => { + dispatch(sessionState.actions.getTimeslotStarting()); + try { + const timeslot = await getCurrentApiTimeslot(); + Sentry.setTag("timeslot_id", timeslot.timeslot_id); + dispatch(sessionState.actions.getTimeslotSuccess(timeslot)); + } catch (e) { + console.log("Failed to get selected timeslot. " + e.toString()); + dispatch(sessionState.actions.getTimeslotError(e.toString())); + } +};