loaderSlice.js 719 Bytes
Newer Older
quynhquang400@gmail.com's avatar
quynhquang400@gmail.com committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/* eslint-disable prettier/prettier */
import {createSlice} from '@reduxjs/toolkit';

const initLoaderState = {
  isLoading: false,
  loadersCount: 0,
};

const loader = createSlice({
  name: 'loader',
  initialState: initLoaderState,
  reducers: {
    startLoading: state => {
      state.isLoading = true;
      state.loadersCount = state.loadersCount + 1;
    },
    finishLoading: state => {
      const newCount = state.loadersCount - 1;
      const stillWaitingOtherComponent = newCount !== 0;
      state.isLoading = stillWaitingOtherComponent;
      state.loadersCount = newCount;
    },
  },
});

const {actions, reducer} = loader;
export const {startLoading, finishLoading} = actions;
export default reducer;