github.com/minio/console@v1.4.1/web-app/src/screens/Console/consoleSlice.ts (about) 1 // This file is part of MinIO Console Server 2 // Copyright (c) 2022 MinIO, Inc. 3 // 4 // This program is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Affero General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // This program is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Affero General Public License for more details. 13 // 14 // You should have received a copy of the GNU Affero General Public License 15 // along with this program. If not, see <http://www.gnu.org/licenses/>. 16 17 import { createSlice, PayloadAction } from "@reduxjs/toolkit"; 18 import { SessionResponse } from "../../api/consoleApi"; 19 import { AppState } from "../../store"; 20 import { fetchSession } from "../../screens/LoginPage/sessionThunk"; 21 import { SessionCallStates } from "./consoleSlice.types"; 22 23 export interface ConsoleState { 24 session: SessionResponse; 25 sessionLoadingState: SessionCallStates; 26 } 27 28 const initialState: ConsoleState = { 29 session: {}, 30 sessionLoadingState: SessionCallStates.Initial, 31 }; 32 33 export const consoleSlice = createSlice({ 34 name: "console", 35 initialState, 36 reducers: { 37 setSessionLoadingState: ( 38 state, 39 action: PayloadAction<SessionCallStates>, 40 ) => { 41 state.sessionLoadingState = action.payload; 42 }, 43 saveSessionResponse: (state, action: PayloadAction<SessionResponse>) => { 44 state.session = action.payload; 45 }, 46 resetSession: (state) => { 47 state.session = initialState.session; 48 }, 49 }, 50 extraReducers: (builder) => { 51 builder 52 .addCase(fetchSession.pending, (state, action) => { 53 state.sessionLoadingState = SessionCallStates.Loading; 54 }) 55 .addCase(fetchSession.fulfilled, (state, action) => { 56 state.sessionLoadingState = SessionCallStates.Done; 57 }); 58 }, 59 }); 60 61 export const { saveSessionResponse, resetSession, setSessionLoadingState } = 62 consoleSlice.actions; 63 export const selSession = (state: AppState) => state.console.session; 64 export const selFeatures = (state: AppState) => 65 state.console.session ? state.console.session.features : []; 66 67 export default consoleSlice.reducer;