diff options
author | Ivan Chen <ivan@tagg.id> | 2021-06-07 17:53:27 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-07 17:53:27 -0400 |
commit | 5ef5b0776cbbde697919308bbfbab2aed00ca493 (patch) | |
tree | 0180365917b8483c660435381fb1e83393caee45 /src/utils/common.ts | |
parent | 63c5de57ea2989fd2b66211a06b51bb42c5d20ea (diff) | |
parent | c18b2436897cd92e7a33c33c75e13dba1fec8ffe (diff) |
Merge pull request #455 from IvanIFChen/tma878-profile-badges
[TMA-878] Profile Badges
Diffstat (limited to 'src/utils/common.ts')
-rw-r--r-- | src/utils/common.ts | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/src/utils/common.ts b/src/utils/common.ts index 95e77f64..cfd9244a 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'; @@ -197,3 +203,33 @@ export const validateImageLink = async (url: string | undefined) => { return false; }); }; + +/** + * 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], + })); +}; |