import { ContactType, NotificationType, UniversityType, UniversityBadgeType, } from './../types/types'; import moment from 'moment'; import {Linking} from 'react-native'; import {BROWSABLE_SOCIAL_URLS, TOGGLE_BUTTON_TYPE} from '../constants'; import AsyncStorage from '@react-native-community/async-storage'; import {getAll} from 'react-native-contacts'; export const getToggleButtonText: ( buttonType: string, state: boolean, ) => string | null = (buttonType, state) => { switch (buttonType) { case TOGGLE_BUTTON_TYPE.FRIEND_UNFRIEND: return state ? 'Unfriend' : 'Add Friend'; case TOGGLE_BUTTON_TYPE.BLOCK_UNBLOCK: return state ? 'Unblock' : 'Block'; default: return null; } }; export const handleOpenSocialUrlOnBrowser = ( handle: string | undefined, social: string, ) => { if (handle && social in BROWSABLE_SOCIAL_URLS) { Linking.openURL(BROWSABLE_SOCIAL_URLS[social] + `${handle}/`); } }; //Returns university class just like we would like to display on profile page export const getUniversityClass = (universityClass: number) => { return `Class of '${(universityClass % 2000).toString()}`; }; export const getDateAge: ( date: moment.Moment, ) => 'today' | 'yesterday' | 'thisWeek' | 'unknown' = (date: moment.Moment) => { const today = moment().startOf('day'); const yesterday = moment().subtract(1, 'days').startOf('day'); const weekOld = moment().subtract(7, 'days').startOf('day'); if (date.isSame(today, 'd')) { return 'today'; } else if (date.isSame(yesterday, 'd')) { return 'yesterday'; } else if (date.isAfter(weekOld)) { return 'thisWeek'; } else { // this can be longer than a week or in the future return 'unknown'; } }; export const moveCategory: ( categories: string[], category: string, moveUp: boolean, ) => string[] = (categories, category, moveUp) => { const i = categories.indexOf(category); const swapTarget = moveUp ? i - 1 : i + 1; if ((moveUp && i === 0) || (!moveUp && i > categories.length)) { return categories; } const tmp = categories[i]; categories[i] = categories[swapTarget]; categories[swapTarget] = tmp; return categories; }; export const checkImageUploadStatus = (statusMap: object) => { for (let [key, value] of Object.entries(statusMap)) { if (value != 'Success') { return false; } } return true; }; export const haveUnreadNotifications = async ( notifications: NotificationType[], ): Promise => { for (const n of notifications) { const notificationDate = moment(n.timestamp); const prevLastViewed = await AsyncStorage.getItem('notificationLastViewed'); const lastViewed: moment.Moment | undefined = prevLastViewed == null ? moment.unix(0) : moment(prevLastViewed); const dateAge = getDateAge(notificationDate); if (dateAge === 'unknown') { continue; } const unread = lastViewed ? lastViewed.diff(notificationDate) < 0 : false; if (unread) { return true; } } return false; }; // https://stackoverflow.com/a/2450976 export const shuffle = (array: any[]) => { var currentIndex = array.length, temporaryValue, randomIndex; // While there remain elements to shuffle... while (currentIndex !== 0) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex -= 1; // And swap it with the current element. temporaryValue = array[currentIndex]; array[currentIndex] = array[randomIndex]; array[randomIndex] = temporaryValue; } return array; }; export const extractContacts = async () => { let retrievedContacts: Array = []; await getAll().then((contacts) => { contacts.map((contact) => { contact.phoneNumbers.map((phoneNumber) => { retrievedContacts.push({ first_name: contact.givenName, last_name: contact.familyName, phone_number: phoneNumber.number, }); }); }); }); return retrievedContacts; }; export const getUniversityBadge = ( university: UniversityType, type: UniversityBadgeType, ) => { switch (type) { case 'Search': return _searchPageIcon(university); case 'Crest': return _crestIcon(university); default: return require('../assets/images/bwbadges.png'); } }; const _searchPageIcon = (university: UniversityType) => { switch (university) { case UniversityType.Cornell: return require('../assets/universities/cornell-search.png'); case UniversityType.Brown: return require('../assets/images/bwbadges.png'); default: return require('../assets/images/bwbadges.png'); } }; const _crestIcon = (university: UniversityType) => { switch (university) { case UniversityType.Cornell: return require('../assets/universities/cornell.png'); case UniversityType.Brown: return require('../assets/universities/brown.png'); default: return require('../assets/images/bwbadges.png'); } };