github.com/minio/console@v1.4.1/web-app/src/screens/Console/Dashboard/dashboardSlice.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 { zoomState } from "./types";
    19  import { IDashboardPanel } from "./Prometheus/types";
    20  import { getUsageAsync } from "./dashboardThunks";
    21  import { AdminInfoResponse } from "api/consoleApi";
    22  
    23  export interface DashboardState {
    24    zoom: zoomState;
    25    usage: AdminInfoResponse | null;
    26    status: "idle" | "loading" | "failed";
    27    widgetLoadVersion: number;
    28  }
    29  
    30  const initialState: DashboardState = {
    31    status: "idle",
    32    zoom: {
    33      openZoom: false,
    34      widgetRender: null,
    35    },
    36    usage: null,
    37    widgetLoadVersion: 0,
    38  };
    39  export const dashboardSlice = createSlice({
    40    name: "dashboard",
    41    initialState,
    42    reducers: {
    43      openZoomPage: (state, action: PayloadAction<IDashboardPanel>) => {
    44        state.zoom.openZoom = true;
    45        state.zoom.widgetRender = action.payload;
    46      },
    47      closeZoomPage: (state) => {
    48        state.zoom.openZoom = false;
    49        state.zoom.widgetRender = null;
    50      },
    51      reloadWidgets: (state) => {
    52        state.widgetLoadVersion++;
    53      },
    54    },
    55    extraReducers: (builder) => {
    56      builder
    57        .addCase(getUsageAsync.pending, (state) => {
    58          state.status = "loading";
    59        })
    60        .addCase(getUsageAsync.rejected, (state) => {
    61          state.status = "failed";
    62        })
    63        .addCase(getUsageAsync.fulfilled, (state, action) => {
    64          state.status = "idle";
    65          state.usage = action.payload;
    66        });
    67    },
    68  });
    69  export const { openZoomPage, closeZoomPage, reloadWidgets } =
    70    dashboardSlice.actions;
    71  
    72  export default dashboardSlice.reducer;