onLeaveSlice.js 4.65 KB
Newer Older
quynhquang400@gmail.com's avatar
quynhquang400@gmail.com committed
1 2 3 4 5 6 7 8
import {createAsyncThunk, createSlice} from '@reduxjs/toolkit';

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

import onLeaveApi from '../../network/api/onLeaveApi';
import Utils from '../../utils';

const initialHome = {
9
  directManagersList: [],
quynhquang400@gmail.com's avatar
quynhquang400@gmail.com committed
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
};

export const getUserLeavesDay = createAsyncThunk(
  'onLeave/getUserLeavesDay',
  async (data, thunkAPI) => {
    return serviceRequest({
      dispatch: thunkAPI.dispatch,
      serviceMethod: onLeaveApi.requestGetLeavesDay,
      payload: data,
      options: {
        skipLoader: false,
      },
    });
  },
);
export const getUserRestDay = createAsyncThunk(
  'onLeave/getUserRestDay',
  async (data, thunkAPI) => {
    return serviceRequest({
      dispatch: thunkAPI.dispatch,
      serviceMethod: onLeaveApi.requestGetRestDay,
      payload: data,
      options: {
        skipLoader: false,
      },
    });
  },
);
38 39 40 41 42 43 44 45 46 47 48 49 50
export const getApproveRequestLeavesDays = createAsyncThunk(
  'onLeave/getApproveRequestLeavesDays',
  async (data, thunkAPI) => {
    return serviceRequest({
      dispatch: thunkAPI.dispatch,
      serviceMethod: onLeaveApi.requestGetLeaveDayApproved,
      payload: data,
      options: {
        skipLoader: false,
      },
    });
  },
);
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
export const getLeaveCategory = createAsyncThunk(
  'onLeave/getLeaveCategory',
  async (data, thunkAPI) => {
    return serviceRequest({
      dispatch: thunkAPI.dispatch,
      serviceMethod: onLeaveApi.requestGetLeaveCategory,
      payload: data,
      options: {
        skipLoader: false,
      },
    });
  },
);
export const getDirectManagers = createAsyncThunk(
  'onLeave/getDirectManagers',
  async (data, thunkAPI) => {
    return serviceRequest({
      dispatch: thunkAPI.dispatch,
      serviceMethod: onLeaveApi.requestGetDirectManagers,
      payload: data,
      options: {
        skipLoader: false,
      },
    });
  },
);
export const postLeaveRequest = createAsyncThunk(
  'onLeave/postLeaveRequest',
  async (data, thunkAPI) => {
    return serviceRequest({
      dispatch: thunkAPI.dispatch,
      serviceMethod: onLeaveApi.requestPostLeaveRequest,
      payload: data,
      options: {
        skipLoader: false,
      },
    });
  },
);
90 91 92 93 94 95 96 97 98 99 100 101 102
export const postSubmitApproveLeaveRequest = createAsyncThunk(
  'onLeave/postSubmitApproveLeaveRequest',
  async (data, thunkAPI) => {
    return serviceRequest({
      dispatch: thunkAPI.dispatch,
      serviceMethod: onLeaveApi.requestPostApproveLeaveRequest,
      payload: data,
      options: {
        skipLoader: false,
      },
    });
  },
);
quynhquang400@gmail.com's avatar
quynhquang400@gmail.com committed
103 104 105
const onLeaveSlice = createSlice({
  name: 'onLeave',
  initialState: initialHome,
106 107 108 109 110 111
  reducers: {
    handleSelectManagersLeaveRequest: (state, payload) => {
      const {index, value} = payload.payload;
      let managers = JSON.parse(JSON.stringify(state.directManagersList));
      if (managers.length > 0) {
        managers[index].isChecked = value;
112
        state.directManagersList = [...managers];
113 114 115
      }
    },
  },
quynhquang400@gmail.com's avatar
quynhquang400@gmail.com committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  extraReducers: builder => {
    builder.addCase(getUserLeavesDay.fulfilled, (state, action) => {
      const {success} = Utils.getValues(action, 'payload', false);
      if (success) {
        state.usersLeavesDayList = Utils.getValues(
          action,
          'payload.data.collection',
          [],
        );
      }
    });
    builder.addCase(getUserRestDay.fulfilled, (state, action) => {
      const {success} = Utils.getValues(action, 'payload', false);
      if (success) {
        state.userRestDayList = Utils.getValues(
          action,
          'payload.data.collection',
          [],
        );
      }
    });
137 138 139 140 141 142 143 144 145 146
    builder.addCase(getApproveRequestLeavesDays.fulfilled, (state, action) => {
      const {success} = Utils.getValues(action, 'payload', false);
      if (success) {
        state.approveRequestLeavesDaysList = Utils.getValues(
          action,
          'payload.data.collection',
          [],
        );
      }
    });
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
    builder.addCase(getLeaveCategory.fulfilled, (state, action) => {
      const {success} = Utils.getValues(action, 'payload', false);
      if (success) {
        state.leaveCategory = Utils.getValues(action, 'payload.data', []);
      }
    });
    builder.addCase(getDirectManagers.fulfilled, (state, action) => {
      const {success} = Utils.getValues(action, 'payload', false);
      if (success) {
        const managers = Utils.getValues(action, 'payload.data', []);
        const addKeySelect = managers.map(item => {
          return {
            ...item,
            isChecked: false,
          };
        });
        state.directManagersList = [...addKeySelect];
      }
    });
quynhquang400@gmail.com's avatar
quynhquang400@gmail.com committed
166 167 168
  },
});

169 170
const {reducer, actions} = onLeaveSlice;
export const {handleSelectManagersLeaveRequest} = actions;
quynhquang400@gmail.com's avatar
quynhquang400@gmail.com committed
171
export default reducer;