aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
authorIvan Chen <ivan@tagg.id>2021-05-18 13:00:14 -0400
committerGitHub <noreply@github.com>2021-05-18 13:00:14 -0400
commitc8487348e1026a3a2c1a147d3eefe3ee0cc6528c (patch)
tree72061e400748adce01ad90b2b5943e135fa25323 /src/utils
parentefc84a7a5af59bcaf219d2ecb6767a3b29d01fae (diff)
parent4aca9fc0916240ce5e4284d625f240998db17bff (diff)
Merge pull request #429 from brian-tagg/tma844-bugfix-plus-sign
[TMA-844] Plus sign for profile and header in profile
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/common.ts17
-rw-r--r--src/utils/users.ts86
2 files changed, 103 insertions, 0 deletions
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..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;
+ });
+};