aboutsummaryrefslogtreecommitdiff
path: root/src/utils/common.ts
blob: 0a76ec08786fdc161da2fc3f18e0ba067ac19ca6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import {ContactType, NotificationType} 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<boolean> => {
  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<ContactType> = [];
  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;
};