homeSlice.js 2.55 KB
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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
/* eslint-disable prettier/prettier */
import {createAsyncThunk, createSlice} from '@reduxjs/toolkit';

import serviceRequest from '../../app/serviceRequest';

import homeAPI from '../../api/homeAPI';
import Utils from '../../utils';

const initialHome = {
  quotationList: [],
  news: [],
  birthdayListInMonth: [],
};

export const getQuotation = createAsyncThunk(
  'home/getQuotation',
  async (data, thunkAPI) => {
    return serviceRequest({
      dispatch: thunkAPI.dispatch,
      serviceMethod: homeAPI.requestGetQuotation,
      payload: data,
      options: {
        skipLoader: false,
      },
    });
  },
);
export const getRadomQuotation = createAsyncThunk(
  'home/getRadomQuotation',
  async (data, thunkAPI) => {
    return serviceRequest({
      dispatch: thunkAPI.dispatch,
      serviceMethod: homeAPI.requestGetRandomQuotation,
      payload: data,
      options: {
        skipLoader: false,
      },
    });
  },
);
export const getUserDirectManagers = createAsyncThunk(
  'home/getUserDirectManagers',
  async (data, thunkAPI) => {
    return serviceRequest({
      dispatch: thunkAPI.dispatch,
      serviceMethod: homeAPI.requestGetUsersDirectManagers,
      payload: data,
      options: {
        skipLoader: false,
      },
    });
  },
);
export const getBirthDayUser = createAsyncThunk(
  'home/getBirthDayUser',
  async (data, thunkAPI) => {
    return serviceRequest({
      dispatch: thunkAPI.dispatch,
      serviceMethod: homeAPI.requestGetBirthdayUser,
      payload: data,
      options: {
        skipLoader: false,
      },
    });
  },
);

const homeSlice = createSlice({
  name: 'home',
  initialState: initialHome,
  reducers: {},
  extraReducers: builder => {
    builder.addCase(getQuotation.fulfilled, (state, action) => {
      const {success} = Utils.getValues(action, 'payload', false);
      console.log('action', action);
      if (success) {
        state.quotationList = Utils.getValues(
          action,
          'payload.data.collection',
          [],
        );
      }
    });
    builder.addCase(getRadomQuotation.fulfilled, (state, action) => {
      const {success} = Utils.getValues(action, 'payload', false);
      if (success) {
        state.randomQuotation = Utils.getValues(action, 'payload.data', null);
      }
    });
    builder.addCase(getBirthDayUser.fulfilled, (state, action) => {
      const {success} = Utils.getValues(action, 'payload', false);
      if (success) {
quynhquang400@gmail.com's avatar
quynhquang400@gmail.com committed
93
        state.birthdayListInMonth = Utils.getValues(action, 'payload.data', []);
quynhquang400@gmail.com's avatar
quynhquang400@gmail.com committed
94 95 96 97 98 99 100
      }
    });
  },
});

const {reducer} = homeSlice;
export default reducer;