diff options
author | Ivan Chen <ivan@tagg.id> | 2021-03-23 16:18:19 -0400 |
---|---|---|
committer | Ivan Chen <ivan@tagg.id> | 2021-03-23 16:18:19 -0400 |
commit | 4844b69ed6c381fe8e573e77d32302965c4de274 (patch) | |
tree | 605f08e0019c877564a9631be604d83e67dd7caf | |
parent | 8022d908ca09860424529d818e210d63fff9f398 (diff) |
updated types, using api/profile
-rw-r--r-- | src/constants/api.ts | 1 | ||||
-rw-r--r-- | src/services/UserProfileService.ts | 24 | ||||
-rw-r--r-- | src/store/actions/userX.ts | 77 | ||||
-rw-r--r-- | src/store/initialStates.ts | 6 | ||||
-rw-r--r-- | src/types/types.ts | 12 | ||||
-rw-r--r-- | src/utils/friends.ts | 4 | ||||
-rw-r--r-- | src/utils/users.ts | 4 |
7 files changed, 93 insertions, 35 deletions
diff --git a/src/constants/api.ts b/src/constants/api.ts index 34ef9a1c..d2d43063 100644 --- a/src/constants/api.ts +++ b/src/constants/api.ts @@ -9,6 +9,7 @@ export const REGISTER_ENDPOINT: string = API_URL + 'register/'; export const EDIT_PROFILE_ENDPOINT: string = API_URL + 'edit-profile/'; export const SEND_OTP_ENDPOINT: string = API_URL + 'send-otp/'; export const VERIFY_OTP_ENDPOINT: string = API_URL + 'verify-otp/'; +export const USER_PROFILE_ENDPOINT: string = API_URL + 'profile/'; export const PROFILE_INFO_ENDPOINT: string = API_URL + 'user-profile-info/'; export const HEADER_PHOTO_ENDPOINT: string = API_URL + 'header-pic/'; export const PROFILE_PHOTO_ENDPOINT: string = API_URL + 'profile-pic/'; diff --git a/src/services/UserProfileService.ts b/src/services/UserProfileService.ts index de24d2d6..e733cb47 100644 --- a/src/services/UserProfileService.ts +++ b/src/services/UserProfileService.ts @@ -7,6 +7,7 @@ import { GET_TWITTER_POSTS_ENDPOINT, HEADER_PHOTO_ENDPOINT, PASSWORD_RESET_ENDPOINT, + USER_PROFILE_ENDPOINT, PROFILE_INFO_ENDPOINT, PROFILE_PHOTO_ENDPOINT, REGISTER_ENDPOINT, @@ -25,7 +26,7 @@ import { SUCCESS_PWD_RESET, SUCCESS_VERIFICATION_CODE_SENT, } from '../constants/strings'; -import {SocialAccountType} from '../types'; +import {SocialAccountType, ProfileType} from '../types'; export const loadProfileInfo = async (token: string, userId: string) => { try { @@ -333,3 +334,24 @@ export const sendRegister = async ( return undefined; } }; + +export const fetchUserProfile = async (userId: string, token?: string) => { + try { + if (!token) { + token = (await AsyncStorage.getItem('token')) ?? ''; + } + const response = await fetch(USER_PROFILE_ENDPOINT + userId + '/', { + method: 'GET', + headers: { + Authorization: 'Token ' + token, + }, + }); + if (response.status === 200) { + const data: ProfileType = await response.json(); + return data; + } + } catch (error) { + console.log(error); + return undefined; + } +}; diff --git a/src/store/actions/userX.ts b/src/store/actions/userX.ts index 325c7568..5d49cdf9 100644 --- a/src/store/actions/userX.ts +++ b/src/store/actions/userX.ts @@ -1,8 +1,8 @@ import {Action, ThunkAction} from '@reduxjs/toolkit'; +import moment from 'moment'; import { - getProfilePic, + fetchUserProfile, loadFriends, - loadMomentCategories, loadMoments, loadProfileInfo, } from '../../services'; @@ -37,11 +37,56 @@ export const loadUserX = ( payload: {screenType, userId, user}, }); const token = await getTokenOrLogout(dispatch); - loadProfileInfo(token, userId).then((data) => { - dispatch({ - type: userXProfileFetched.type, - payload: {screenType, userId, data}, - }); + fetchUserProfile(userId, token).then((profile) => { + if (profile) { + let { + name, + biography, + website, + birthday, + gender, + snapchat, + tiktok, + university_class, + profile_completion_stage, + suggested_people_linked, + friendship_status, + friendship_requester_id, + } = profile.profile_info; + dispatch({ + type: userXProfileFetched.type, + payload: { + screenType, + userId, + data: { + name, + biography, + website, + birthday: birthday && moment(birthday).format('YYYY-MM-DD'), + gender, + snapchat, + tiktok, + university_class, + profile_completion_stage, + suggested_people_linked, + friendship_status, + friendship_requester_id, + }, + }, + }); + 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).then((data) => dispatch({ @@ -49,18 +94,6 @@ export const loadUserX = ( payload: {screenType, userId, data}, }), ); - getProfilePic(token, userId, 'small').then((data) => - dispatch({ - type: userXAvatarFetched.type, - payload: {screenType, userId, data}, - }), - ); - getProfilePic(token, userId, 'large').then((data) => - dispatch({ - type: userXCoverFetched.type, - payload: {screenType, userId, data}, - }), - ); loadFriends(userId, token).then((data) => dispatch({ type: userXFriendsFetched.type, @@ -73,12 +106,6 @@ export const loadUserX = ( payload: {screenType, userId, data}, }), ); - loadMomentCategories(userId, token).then((data) => { - dispatch({ - type: userXMomentCategoriesFetched.type, - payload: {screenType, userId, data}, - }); - }); } catch (error) { console.log(error); } diff --git a/src/store/initialStates.ts b/src/store/initialStates.ts index d89927a7..8ffdd559 100644 --- a/src/store/initialStates.ts +++ b/src/store/initialStates.ts @@ -3,14 +3,14 @@ import { MomentType, NotificationType, ProfilePreviewType, - ProfileType, + ProfileInfoType, ScreenType, SocialAccountType, UserType, UserXType, } from '../types'; -export const NO_PROFILE: ProfileType = { +export const NO_PROFILE: ProfileInfoType = { biography: '', website: '', name: '', @@ -40,7 +40,7 @@ export const EMPTY_PROFILE_PREVIEW_LIST = <ProfilePreviewType[]>[]; export const NO_USER_DATA = { user: <UserType>NO_USER, - profile: <ProfileType>NO_PROFILE, + profile: <ProfileInfoType>NO_PROFILE, avatar: <string | undefined>undefined, cover: <string | undefined>undefined, isOnboardedUser: false, diff --git a/src/types/types.ts b/src/types/types.ts index 4f62310a..8937e74c 100644 --- a/src/types/types.ts +++ b/src/types/types.ts @@ -21,7 +21,15 @@ export interface CategoryPreviewType { export type FriendshipStatusType = 'friends' | 'requested' | 'no_record'; -export interface ProfileType { +export type ProfileType = { + profile_pic: string; + header_pic: string; + profile_info: ProfileInfoType; + moment_categories: string[]; + linked_socials: string[]; +}; + +export interface ProfileInfoType { name: string; biography: string; website: string; @@ -147,7 +155,7 @@ export interface UserXType { socialAccounts: Record<string, SocialAccountType>; momentCategories: string[]; user: UserType; - profile: ProfileType; + profile: ProfileInfoType; avatar: string | undefined; cover: string | undefined; } diff --git a/src/utils/friends.ts b/src/utils/friends.ts index 3398c123..6e3b645a 100644 --- a/src/utils/friends.ts +++ b/src/utils/friends.ts @@ -1,7 +1,7 @@ // Handles click on friend/requested/unfriend button import {RootState} from '../store/rootReducer'; -import {ProfilePreviewType, ProfileType, ScreenType, UserType} from '../types'; +import {ProfilePreviewType, ProfileInfoType, ScreenType, UserType} from '../types'; import {AppDispatch} from '../store/configureStore'; import { addFriend, @@ -25,7 +25,7 @@ import {getUserAsProfilePreviewType} from './users'; export const handleFriendUnfriend = async ( screenType: ScreenType, user: UserType, - profile: ProfileType, + profile: ProfileInfoType, dispatch: AppDispatch, state: RootState, loggedInUser: UserType, diff --git a/src/utils/users.ts b/src/utils/users.ts index af4f3813..d1f416e0 100644 --- a/src/utils/users.ts +++ b/src/utils/users.ts @@ -20,7 +20,7 @@ import {RootState} from './../store/rootReducer'; import { ProfilePreviewType, CategoryPreviewType, - ProfileType, + ProfileInfoType, ScreenType, UserType, } from './../types/types'; @@ -137,7 +137,7 @@ export const getTokenOrLogout = async (dispatch: Function): Promise<string> => { */ export const getUserAsProfilePreviewType = ( passedInUser: UserType, - passedInProfile: ProfileType, + passedInProfile: ProfileInfoType, ): ProfilePreviewType => { const fullName = passedInProfile.name.split(' '); return { |