aboutsummaryrefslogtreecommitdiff
path: root/src/utils/search.ts
blob: 26f40b1b28ef1c9816688e4a161123237a868df6 (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
135
136
137
138
139
140
import AsyncStorage from '@react-native-community/async-storage';

import {BADGE_DATA} from '../constants/badges';
import {
  ProfilePreviewType,
  CategoryPreviewType,
  UniversityType,
} from '../types';

/*
 * Gets all possible search suggestions.
 */
export const getSearchSuggestions = (university: UniversityType): string[] => {
  const suggestions: string[] = [];
  for (const category of BADGE_DATA[university]) {
    for (const interest of category.data) {
      suggestions.push(interest.badgeName);
    }
  }
  return suggestions;
};

/*
 * AsyncStorage key for list of recently-searched users.
 */
const recentlySearchedUsersKey = '@recently_searched_users';

/*
 * Stores `user` in AsyncStorage as a recently-searched user.
 */
export const addUserToRecentlySearched = async (user: ProfilePreviewType) => {
  let users: ProfilePreviewType[];
  // retrieve and update recently-searched categories list
  try {
    const usersJSON = await AsyncStorage.getItem(recentlySearchedUsersKey);
    if (usersJSON) {
      users = JSON.parse(usersJSON);
      // if category already exists, move it to the end
      for (let i = 0; i < users.length; i++) {
        // TODO: speed up comparison by adding some id field to category
        if (users[i].id === user.id) {
          users.splice(i, 1);
          break;
        }
      }
      users.push(user);
    } else {
      users = [user];
    }
    // store updated list of recently-searched categories
    try {
      AsyncStorage.setItem(recentlySearchedUsersKey, JSON.stringify(users));
    } catch (e) {
      console.log(e);
    }
  } catch (e) {
    console.log(e);
  }
};

/*
 * Retrieves and returns user's recently-searched categories from AsyncStorage.
 */
export const getRecentlySearchedUsers = async (): Promise<
  ProfilePreviewType[]
> => {
  try {
    const usersJSON = await AsyncStorage.getItem(recentlySearchedUsersKey);
    if (usersJSON) {
      return JSON.parse(usersJSON);
    }
  } catch (e) {
    console.log(e);
  }
  return [];
};

/*
 * AsyncStorage key for list of recently-searched categories.
 */
const recentlySearchedCategoriesKey = '@recently_searched_categories';

/*
 * Stores `category` in AsyncStorage as a recently-searched category.
 */
export const addCategoryToRecentlySearched = async (
  category: CategoryPreviewType,
) => {
  let categories: CategoryPreviewType[];
  // retrieve and update recently-searched categories list
  try {
    const categoriesJSON = await AsyncStorage.getItem(
      recentlySearchedCategoriesKey,
    );
    if (categoriesJSON) {
      categories = JSON.parse(categoriesJSON);
      // if category already exists, move it to the end
      for (let i = 0; i < categories.length; i++) {
        // TODO: speed up comparison by adding some id field to category
        if (categories[i].name === category.name) {
          categories.splice(i, 1);
          break;
        }
      }
      categories.push(category);
    } else {
      categories = [category];
    }
    // store updated list of recently-searched categories
    try {
      AsyncStorage.setItem(
        recentlySearchedCategoriesKey,
        JSON.stringify(categories),
      );
    } catch (e) {
      console.log(e);
    }
  } catch (e) {
    console.log(e);
  }
};

/*
 * Retrieves and returns user's recently-searched categories from AsyncStorage.
 */
export const getRecentlySearchedCategories = async (): Promise<
  CategoryPreviewType[]
> => {
  try {
    const categoriesJSON = await AsyncStorage.getItem(
      '@recently_searched_categories',
    );
    if (categoriesJSON) {
      return JSON.parse(categoriesJSON);
    }
  } catch (e) {
    console.log(e);
  }
  return [];
};