From 9b88f9a0927f5e2a1fe4cb6745c93c0d8375dfaa Mon Sep 17 00:00:00 2001 From: Mikkel Jakobsen Date: Sun, 18 Aug 2024 00:00:05 +0200 Subject: [PATCH] Remove deprecated user functionality This has been used in a previous version of the apps but are not in use anymore. --- src/core/store.ts | 2 -- src/core/user.js | 65 ------------------------------------------ src/core/user.slice.js | 44 ---------------------------- 3 files changed, 111 deletions(-) delete mode 100644 src/core/user.js delete mode 100644 src/core/user.slice.js diff --git a/src/core/store.ts b/src/core/store.ts index 2380a58b9a..4871895e57 100644 --- a/src/core/store.ts +++ b/src/core/store.ts @@ -10,7 +10,6 @@ import { import { persistReducer, persistStore } from "redux-persist"; import storage from "redux-persist/lib/storage/session"; import textReducer from "./text.slice"; -import userReducer from "./user.slice"; import modalReducer from "./modal.slice"; import urlReducer from "./url.slice"; import filterReducer from "./filter.slice"; @@ -37,7 +36,6 @@ export const store = configureStore({ reducer: persistReducer( persistConfig, combineReducers({ - user: userReducer, text: textReducer, modal: modalReducer, url: urlReducer, diff --git a/src/core/user.js b/src/core/user.js deleted file mode 100644 index 898819b937..0000000000 --- a/src/core/user.js +++ /dev/null @@ -1,65 +0,0 @@ -/** - * A simple collection of functionality in regards to the current user. - * - * @class User - */ -import { hasToken } from "./token"; -import { store, persistor } from "./store"; -import { updateStatus, attemptAuthentication } from "./user.slice"; - -const selectStatus = (state) => state.user.status; - -class User { - /** - * Used to keep track if we started attempting in this page view. - * - * @static - */ - static #attemptingThisRequest = false; - - /** - * Returns whether a user is authenticated of not. - * - * @static - * @returns {boolean} - * @memberof User - */ - static isAuthenticated() { - store.dispatch( - updateStatus({ - hasToken: hasToken("user"), - doFail: !User.#attemptingThisRequest - }) - ); - return selectStatus(store.getState()) === "authenticated"; - } - - /** - * Redirect to login. - * - * @param {string} loginUrl the URL to redirect to. - */ - static authenticate(loginUrl) { - // Switch state to attempting and flush state to session storage - // before redirecting. - store.dispatch(attemptAuthentication()).then(() => persistor.flush()); - User.#attemptingThisRequest = true; - window.location.href = loginUrl; - } - - /** - * Whether authentication failed. - * - * Will return true if we just tried authenticating and it failed. - * - * @returns {boolean} - */ - static authenticationFailed() { - // isAuthenticated() will ensure state is up to date. - return ( - !this.isAuthenticated() && selectStatus(store.getState()) === "failed" - ); - } -} - -export default User; diff --git a/src/core/user.slice.js b/src/core/user.slice.js deleted file mode 100644 index f6649d3524..0000000000 --- a/src/core/user.slice.js +++ /dev/null @@ -1,44 +0,0 @@ -import { createSlice, createAsyncThunk } from "@reduxjs/toolkit"; - -// This thunk doesn't actually do anything but resolve straight away, -// but this allows us to chain a `.then()` on the action on the caller -// side. -export const attemptAuthentication = createAsyncThunk( - "user/attemptAuthentication", - () => Promise.resolve() -); - -const userSlice = createSlice({ - name: "user", - initialState: { status: "unauthenticated" }, - reducers: { - updateStatus(state, action) { - if (state.status === "unauthenticated" || state.status === "attempting") { - if (action.payload.hasToken) { - state.status = "authenticated"; - } else if (action.payload.doFail && state.status === "attempting") { - state.status = "failed"; - } - } - }, - setStatusAuthenticated(state) { - state.status = "authenticated"; - }, - setStatusUnauthenticated(state) { - state.status = "unauthenticated"; - } - }, - extraReducers: { - [attemptAuthentication.pending]: (state) => { - state.status = "attempting"; - } - } -}); - -export const { - updateStatus, - setStatusAuthenticated, - setStatusUnauthenticated -} = userSlice.actions; - -export default userSlice.reducer;