aboutsummaryrefslogtreecommitdiff
path: root/src/store/actions/userBlock.ts
blob: 827966826b2ecadacf8f6df56ee27b01eb054bd9 (plain)
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
import {Action, ThunkAction} from '@reduxjs/toolkit';
import {blockOrUnblockUser, loadBlockedUsers} from '../../services';
import {ProfilePreviewType, UserType} from '../../types/types';
import {getTokenOrLogout} from '../../utils';
import {updateBlockedList, userBlockFetched} from '../reducers';
import {RootState} from '../rootReducer';

export const loadBlockedList =
  (
    userId: string,
  ): ThunkAction<Promise<void>, RootState, unknown, Action<string>> =>
  async (dispatch) => {
    try {
      const token = await getTokenOrLogout(dispatch);
      const blocked = await loadBlockedUsers(userId, token);
      dispatch({
        type: userBlockFetched.type,
        payload: blocked,
      });
    } catch (error) {
      console.log(error);
    }
  };

export const blockUnblockUser =
  (
    blocker: UserType,
    blocked: ProfilePreviewType,
    isBlocked: boolean,
  ): ThunkAction<Promise<void>, RootState, unknown, Action<string>> =>
  async (dispatch) => {
    const token = await getTokenOrLogout(dispatch);
    const success = blockOrUnblockUser(
      blocker.userId,
      blocked.id,
      token,
      isBlocked,
    );
    if (success) {
      dispatch({
        type: updateBlockedList.type,
        payload: {
          isBlocked,
          data: blocked,
        },
      });
    }
  };