aboutsummaryrefslogtreecommitdiff
path: root/src/utils
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils')
-rw-r--r--src/utils/common.ts42
-rw-r--r--src/utils/users.ts26
2 files changed, 42 insertions, 26 deletions
diff --git a/src/utils/common.ts b/src/utils/common.ts
index cb0bd62d..645f229a 100644
--- a/src/utils/common.ts
+++ b/src/utils/common.ts
@@ -1,11 +1,17 @@
import AsyncStorage from '@react-native-community/async-storage';
import moment from 'moment';
-import {Linking} from 'react-native';
+import {ImageSourcePropType, Linking} from 'react-native';
import {getAll} from 'react-native-contacts';
-import {BROWSABLE_SOCIAL_URLS, TOGGLE_BUTTON_TYPE} from '../constants';
+import {
+ BADGE_DATA,
+ BROWSABLE_SOCIAL_URLS,
+ TOGGLE_BUTTON_TYPE,
+} from '../constants';
import {
ContactType,
NotificationType,
+ UniversityBadge,
+ UniversityBadgeDisplayType,
UniversityBadgeType,
UniversityType,
} from './../types/types';
@@ -198,7 +204,37 @@ export const validateImageLink = async (url: string | undefined) => {
});
};
+/**
+ * Turns a list of badges into display badges (just a badge with img) by
+ * looking up the img source from our badge asset lookup constant.
+ *
+ * WARNING: Assumes a small list of badges, complexity goes up exponentially.
+ *
+ * @param badges list of university badges
+ * @param university university of which all the badges belong
+ * @returns list of display badges
+ */
+export const badgesToDisplayBadges = (
+ badges: UniversityBadge[],
+ university: UniversityType,
+) => {
+ const badgeSet: Set<string> = new Set(badges.map((b) => b.category + b.name));
+ const badgeToImgMap: Record<string, ImageSourcePropType> = {};
+ BADGE_DATA[university].forEach((category) => {
+ category.data.forEach((badgeInfo) => {
+ const key = category.title + badgeInfo.badgeName;
+ if (badgeSet.has(key)) {
+ badgeToImgMap[key] = badgeInfo.badgeImage;
+ }
+ });
+ });
+ return <UniversityBadgeDisplayType[]>badges.map((b) => ({
+ ...b,
+ img: badgeToImgMap[b.category + b.name],
+ }));
+};
+
// Documentation: https://stackoverflow.com/questions/2901102/how-to-print-a-number-with-commas-as-thousands-separators-in-javascript
export const numberWithCommas = (digits: number) => {
return digits.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
-};
+ \ No newline at end of file
diff --git a/src/utils/users.ts b/src/utils/users.ts
index 8505cde2..64ad10e9 100644
--- a/src/utils/users.ts
+++ b/src/utils/users.ts
@@ -1,7 +1,8 @@
-import {Alert} from 'react-native';
import AsyncStorage from '@react-native-community/async-storage';
+import {Alert} from 'react-native';
+import ImagePicker from 'react-native-image-crop-picker';
import {INTEGRATED_SOCIAL_LIST} from '../constants';
-import {isUserBlocked, loadSocialPosts, removeBadgesService} from '../services';
+import {isUserBlocked, loadSocialPosts, patchEditProfile} from '../services';
import {
loadAllSocials,
loadBlockedList,
@@ -11,7 +12,6 @@ import {
loadUserMoments,
loadUserNotifications,
logout,
- updateUserBadges,
} from '../store/actions';
import {NO_SOCIAL_ACCOUNTS} from '../store/initialStates';
import {loadUserMomentCategories} from './../store/actions/momentCategories';
@@ -23,10 +23,7 @@ import {
ProfilePreviewType,
ScreenType,
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([
@@ -205,23 +202,6 @@ export const canViewProfile = (
return false;
};
-/* Function to call remove badge service,
- * remove selected badge from list passed in and
- * dispatch thunk action to update store
- */
-export const removeUserBadge = async (
- badges: UniversityBadge[],
- badgeName: string,
- userId: string,
- dispatch: AppDispatch,
-) => {
- const success = await removeBadgesService([badgeName], userId);
- if (success === true) {
- badges = badges.filter((badge) => badge.name !== badgeName);
- dispatch(updateUserBadges(badges));
- }
-};
-
export const navigateToProfile = async (
state: RootState,
dispatch: any,