From 853bd0600e0da87af578c17ce9249e40888017b5 Mon Sep 17 00:00:00 2001 From: Brian Kim Date: Wed, 12 May 2021 17:42:47 -0700 Subject: Modularized as much as possible --- src/utils/common.ts | 17 ++++++++++++ src/utils/users.ts | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) (limited to 'src/utils') diff --git a/src/utils/common.ts b/src/utils/common.ts index ce4ab7d1..95e77f64 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -180,3 +180,20 @@ const _crestIcon = (university: UniversityType) => { return require('../assets/images/bwbadges.png'); } }; + +export const validateImageLink = async (url: string | undefined) => { + if (!url) { + return false; + } + return fetch(url) + .then((res) => { + if (res.status === 200) { + return true; + } else { + return false; + } + }) + .catch((_) => { + return false; + }); +}; diff --git a/src/utils/users.ts b/src/utils/users.ts index 334cb3c0..bc81bbc6 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,77 @@ export const navigateToProfile = async ( screenType, }); }; + +export const patchProfile = async (title: string, userId: string) => { + let imageSettings = {}; + switch (title) { + case 'Select Header Picture': + imageSettings = { + smartAlbums: [ + 'Favorites', + 'RecentlyAdded', + 'SelfPortraits', + 'Screenshots', + 'UserLibrary', + ], + width: 580, + height: 580, + cropping: true, + cropperToolbarTitle: title, + mediaType: 'photo', + }; + break; + case 'Select Profile Picture': + imageSettings = { + smartAlbums: [ + 'Favorites', + 'RecentlyAdded', + 'SelfPortraits', + 'Screenshots', + 'UserLibrary', + ], + width: 580, + height: 580, + cropping: true, + cropperToolbarTitle: title, + mediaType: 'photo', + cropperCircleOverlay: true, + }; + break; + } + + return await ImagePicker.openPicker(imageSettings) + .then((picture) => { + if ('path' in picture) { + const request = new FormData(); + switch (title) { + case 'Select Header Picture': + request.append('largeProfilePicture', { + uri: picture.path, + name: 'large_profile_pic.jpg', + type: 'image/jpg', + }); + break; + case 'Select Profile Picture': + request.append('smallProfilePicture', { + uri: picture.path, + name: 'small_profile_pic.jpg', + type: 'image/jpg', + }); + break; + } + + return patchEditProfile(request, userId) + .then((_) => { + return true; + }) + .catch((error) => { + Alert.alert(error); + return false; + }); + } + }) + .catch((_) => { + return false; + }); +}; -- cgit v1.2.3-70-g09d2 From e884ad25b4f2358406eee8a2766890291538a518 Mon Sep 17 00:00:00 2001 From: Brian Kim Date: Thu, 13 May 2021 12:16:49 -0700 Subject: Cleaned up code somewhat --- src/components/profile/Cover.tsx | 36 +++++++++++++-------------- src/components/profile/TaggAvatar.tsx | 2 +- src/utils/users.ts | 47 +++++++++++++++++++---------------- 3 files changed, 44 insertions(+), 41 deletions(-) (limited to 'src/utils') diff --git a/src/components/profile/Cover.tsx b/src/components/profile/Cover.tsx index 82231417..5d5b4234 100644 --- a/src/components/profile/Cover.tsx +++ b/src/components/profile/Cover.tsx @@ -13,7 +13,7 @@ import GreyPurplePlus from '../../assets/icons/grey-purple-plus.svg'; import {useDispatch, useSelector} from 'react-redux'; import {loadUserData, resetHeaderAndProfileImage} from '../../store/actions'; import {RootState} from '../../store/rootreducer'; -import {patchProfile, validateImageLink} from '../../utils'; +import {normalize, patchProfile, validateImageLink} from '../../utils'; interface CoverProps { userXId: string | undefined; @@ -44,7 +44,7 @@ const Cover: React.FC = ({userXId, screenType}) => { const handleNewImage = async () => { setLoading(true); - const result = await patchProfile('Select Header Picture', user.userId); + const result = await patchProfile('header', user.userId); setLoading(true); if (result) { setNeedsUpdate(true); @@ -62,22 +62,20 @@ const Cover: React.FC = ({userXId, screenType}) => { if (!validImage && userXId === undefined && !loading) { return ( - <> - - - handleNewImage()}> - - Add Picture - - - - + + + handleNewImage()}> + + Add Picture + + + ); } else { return ( @@ -108,7 +106,7 @@ const styles = StyleSheet.create({ text: { color: 'white', position: 'absolute', - fontSize: 18, + fontSize: normalize(16), top: 80, right: 20, }, diff --git a/src/components/profile/TaggAvatar.tsx b/src/components/profile/TaggAvatar.tsx index 1f6bbba6..304b9e3a 100644 --- a/src/components/profile/TaggAvatar.tsx +++ b/src/components/profile/TaggAvatar.tsx @@ -46,7 +46,7 @@ const TaggAvatar: React.FC = ({ const handleNewImage = async () => { setLoading(true); - const result = await patchProfile('Select Profile Picture', user.userId); + const result = await patchProfile('profile', user.userId); if (result) { setNeedsUpdate(true); } else { diff --git a/src/utils/users.ts b/src/utils/users.ts index bc81bbc6..430c843f 100644 --- a/src/utils/users.ts +++ b/src/utils/users.ts @@ -244,10 +244,19 @@ export const navigateToProfile = async ( }); }; -export const patchProfile = async (title: string, userId: string) => { +export const patchProfile = async ( + title: 'profile' | 'header', + userId: string, +) => { let imageSettings = {}; + let screenTitle: string; + let requestTitle: string; + let fileName: string; switch (title) { - case 'Select Header Picture': + case 'header': + screenTitle = 'Select Header Picture'; + requestTitle = 'largeProfilePicture'; + fileName = 'large_profile_pic.jpg'; imageSettings = { smartAlbums: [ 'Favorites', @@ -259,11 +268,14 @@ export const patchProfile = async (title: string, userId: string) => { width: 580, height: 580, cropping: true, - cropperToolbarTitle: title, + cropperToolbarTitle: screenTitle, mediaType: 'photo', }; break; - case 'Select Profile Picture': + case 'profile': + screenTitle = 'Select Profile Picture'; + requestTitle = 'smallProfilePicture'; + fileName = 'small_profile_pic.jpg'; imageSettings = { smartAlbums: [ 'Favorites', @@ -275,33 +287,26 @@ export const patchProfile = async (title: string, userId: string) => { width: 580, height: 580, cropping: true, - cropperToolbarTitle: title, + 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(); - switch (title) { - case 'Select Header Picture': - request.append('largeProfilePicture', { - uri: picture.path, - name: 'large_profile_pic.jpg', - type: 'image/jpg', - }); - break; - case 'Select Profile Picture': - request.append('smallProfilePicture', { - uri: picture.path, - name: 'small_profile_pic.jpg', - type: 'image/jpg', - }); - break; - } + request.append(requestTitle, { + uri: picture.path, + name: fileName, + type: 'image/jpg', + }); return patchEditProfile(request, userId) .then((_) => { -- cgit v1.2.3-70-g09d2