aboutsummaryrefslogtreecommitdiff
path: root/src/utils/common.ts
blob: c1049c4200c56aa71cfbd17a7aef871d0a4fff19 (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
import {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';

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;
};