import {Action, ThunkAction} from '@reduxjs/toolkit'; import moment from 'moment'; import { fetchUserProfile, loadFriends, loadMoments, loadProfileInfo, } from '../../services'; import {ScreenType, UserType} from '../../types/types'; import { resetScreen, userXAvatarFetched, userXCoverFetched, userXFriendsFetched, userXMomentCategoriesFetched, userXMomentsFetched, userXProfileFetched, userXRequested, userXSocialsFetched, userXUserFetched, } from '../reducers'; import {RootState} from '../rootReducer'; import {getTokenOrLogout, loadAllSocialsForUser} from './../../utils'; import {userXInStore} from './../../utils/'; export const loadUserX = ( user: UserType, screenType: ScreenType, ): ThunkAction, RootState, unknown, Action> => async (dispatch) => { try { const {userId} = user; await dispatch({ type: userXRequested.type, payload: {screenType, userId}, }); await dispatch({ type: userXUserFetched.type, payload: {screenType, userId, user}, }); const token = await getTokenOrLogout(dispatch); fetchUserProfile(userId, token).then((profile) => { if (profile) { const birthday = profile.profile_info.birthday; dispatch({ type: userXProfileFetched.type, payload: { screenType, userId, data: { ...profile.profile_info, birthday: birthday && moment(birthday).format('YYYY-MM-DD'), }, }, }); dispatch({ type: userXAvatarFetched.type, payload: {screenType, userId, data: profile.profile_pic}, }); dispatch({ type: userXCoverFetched.type, payload: {screenType, userId, data: profile.header_pic}, }); dispatch({ type: userXMomentCategoriesFetched.type, payload: {screenType, userId, data: profile.moment_categories}, }); } }); loadAllSocialsForUser(userId, token).then((data) => dispatch({ type: userXSocialsFetched.type, payload: {screenType, userId, data}, }), ); loadFriends(userId, token).then((data) => dispatch({ type: userXFriendsFetched.type, payload: {screenType, userId, data}, }), ); loadMoments(userId, token).then((data) => dispatch({ type: userXMomentsFetched.type, payload: {screenType, userId, data}, }), ); } catch (error) { console.log(error); } }; export const updateUserXFriends = ( userId: string, state: RootState, ): ThunkAction, RootState, unknown, Action> => async (dispatch) => { try { const screens = [ ScreenType.Profile, ScreenType.Search, ScreenType.Notifications, ]; const token = await getTokenOrLogout(dispatch); screens.forEach((screenType) => { if (userXInStore(state, screenType, userId)) { loadFriends(userId, token).then((data) => dispatch({ type: userXFriendsFetched.type, payload: {screenType, userId, data}, }), ); } }); } catch (error) { console.log(error); } }; export const resetScreenType = ( screenType: ScreenType, ): ThunkAction, RootState, unknown, Action> => async (dispatch) => { try { dispatch({ type: resetScreen.type, payload: {screenType}, }); } catch (error) { console.log(error); } }; export const updateUserXProfileAllScreens = ( userId: string, state: RootState, ): ThunkAction, RootState, unknown, Action> => async (dispatch) => { try { const screens = [ ScreenType.Profile, ScreenType.Search, ScreenType.Notifications, ]; const token = await getTokenOrLogout(dispatch); screens.forEach((screenType) => { if (userXInStore(state, screenType, userId)) { loadProfileInfo(token, userId).then((data) => { dispatch({ type: userXProfileFetched.type, payload: {screenType, userId, data}, }); }); } }); } catch (error) { console.log(error); } };