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, RootState, unknown, Action> => 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, RootState, unknown, Action> => 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, }, }); } };