diff options
author | Ivan Chen <ivan@tagg.id> | 2021-05-19 17:37:03 -0400 |
---|---|---|
committer | Ivan Chen <ivan@tagg.id> | 2021-05-19 17:37:03 -0400 |
commit | 2531a2b672bb60e871d419453ae8558f0869bce3 (patch) | |
tree | b23f79f6378384cffff9e032d12066014e9dafff /src/utils/users.ts | |
parent | 1a4113ee3e47be229e28fc5a935ada174781b00b (diff) | |
parent | cddcfef3c32e08aedef1e4908bd477e45bef1974 (diff) |
Merge branch 'master' into tma858-remind-users
# Conflicts:
# src/components/friends/InviteFriendTile.tsx
Diffstat (limited to 'src/utils/users.ts')
-rw-r--r-- | src/utils/users.ts | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/src/utils/users.ts b/src/utils/users.ts index 334cb3c0..8505cde2 100644 --- a/src/utils/users.ts +++ b/src/utils/users.ts @@ -1,3 +1,4 @@ +import {Alert} from 'react-native'; import AsyncStorage from '@react-native-community/async-storage'; import {INTEGRATED_SOCIAL_LIST} from '../constants'; import {isUserBlocked, loadSocialPosts, removeBadgesService} from '../services'; @@ -24,6 +25,8 @@ import { UserType, UniversityBadge, } from './../types/types'; +import ImagePicker from 'react-native-image-crop-picker'; +import {patchEditProfile} from '../services'; const loadData = async (dispatch: AppDispatch, user: UserType) => { await Promise.all([ @@ -240,3 +243,86 @@ export const navigateToProfile = async ( screenType, }); }; + +/* Function to open imagepicker and + * select images, which are sent to + * the database to update the profile + */ +export const patchProfile = async ( + title: 'profile' | 'header', + userId: string, +) => { + let imageSettings = {}; + let screenTitle: string; + let requestTitle: string; + let fileName: string; + switch (title) { + case 'header': + screenTitle = 'Select Header Picture'; + requestTitle = 'largeProfilePicture'; + fileName = 'large_profile_pic.jpg'; + imageSettings = { + smartAlbums: [ + 'Favorites', + 'RecentlyAdded', + 'SelfPortraits', + 'Screenshots', + 'UserLibrary', + ], + width: 580, + height: 580, + cropping: true, + cropperToolbarTitle: screenTitle, + mediaType: 'photo', + }; + break; + case 'profile': + screenTitle = 'Select Profile Picture'; + requestTitle = 'smallProfilePicture'; + fileName = 'small_profile_pic.jpg'; + imageSettings = { + smartAlbums: [ + 'Favorites', + 'RecentlyAdded', + 'SelfPortraits', + 'Screenshots', + 'UserLibrary', + ], + width: 580, + height: 580, + cropping: true, + cropperToolbarTitle: screenTitle, + mediaType: 'photo', + cropperCircleOverlay: true, + }; + break; + default: + screenTitle = ''; + requestTitle = ''; + fileName = ''; + } + + return await ImagePicker.openPicker(imageSettings) + .then((picture) => { + if ('path' in picture) { + const request = new FormData(); + request.append(requestTitle, { + uri: picture.path, + name: fileName, + type: 'image/jpg', + }); + + return patchEditProfile(request, userId) + .then((_) => { + return true; + }) + .catch((error) => { + Alert.alert(error); + return false; + }); + } + }) + .catch((_) => { + return false; + }); +}; |