diff options
| author | Ivan Chen <ivan@tagg.id> | 2021-03-12 14:33:16 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-03-12 14:33:16 -0500 |
| commit | 3585aacbcfe148fa7ce1ed5d3d3fd33ac784be48 (patch) | |
| tree | 9b4ff60063ccb7f14b9865e24e59502ae20db576 /src/utils | |
| parent | f810a63d3cd0be2e9fefda747189cd0b3f8ceb86 (diff) | |
| parent | 5eabfa9af6df007bdee61382b4061db8ad5f0683 (diff) | |
Merge pull request #293 from leonyjiang/tma683-dynamic-search-placeholder
[TMA-683] Dynamic Search Suggestions
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/index.ts | 1 | ||||
| -rw-r--r-- | src/utils/search.ts | 132 | ||||
| -rw-r--r-- | src/utils/users.ts | 66 |
3 files changed, 133 insertions, 66 deletions
diff --git a/src/utils/index.ts b/src/utils/index.ts index 82c94100..739e6fb8 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -3,3 +3,4 @@ export * from './moments'; export * from './common'; export * from './users'; export * from './friends'; +export * from './search'; diff --git a/src/utils/search.ts b/src/utils/search.ts new file mode 100644 index 00000000..4293ff25 --- /dev/null +++ b/src/utils/search.ts @@ -0,0 +1,132 @@ +import AsyncStorage from '@react-native-community/async-storage'; + +import {BADGE_DATA} from '../constants/badges'; +import {ProfilePreviewType, CategoryPreviewType} from '../types'; + +/* + * Gets all possible search suggestions. + */ +export const getSearchSuggestions = (): string[] => { + const suggestions: string[] = []; + for (const category of BADGE_DATA) { + 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 []; +}; diff --git a/src/utils/users.ts b/src/utils/users.ts index 15107c99..af4f3813 100644 --- a/src/utils/users.ts +++ b/src/utils/users.ts @@ -166,69 +166,3 @@ export const defaultUserProfile = () => { return defaultImage; }; -export const addUserToRecentlyViewed = async (user: ProfilePreviewType) => { - const jsonValue = await AsyncStorage.getItem('@recently_searched_users'); - let recentlySearchedList = jsonValue != null ? JSON.parse(jsonValue) : null; - if (recentlySearchedList) { - if (recentlySearchedList.length > 0) { - if ( - recentlySearchedList.some( - (saved_user: ProfilePreviewType) => saved_user.id === user.id, - ) - ) { - console.log('User already in recently searched.'); - } else { - if (recentlySearchedList.length >= 10) { - recentlySearchedList.pop(); - } - recentlySearchedList.unshift(user); - } - } - } else { - recentlySearchedList = [user]; - } - try { - let recentlySearchedListString = JSON.stringify(recentlySearchedList); - await AsyncStorage.setItem( - '@recently_searched_users', - recentlySearchedListString, - ); - } catch (e) { - console.log(e); - } -}; - -/* - * Stores `category` in AsyncStorage as a recently searched category. - */ -export const addCategoryToRecentlySearched = async ( - category: CategoryPreviewType, -) => { - const recentlySearchedCategoriesKey = '@recently_searched_categories'; - let categories: CategoryPreviewType[]; - // retrieve recently-searched categories and set new list - try { - const categoriesJSON = await AsyncStorage.getItem( - recentlySearchedCategoriesKey, - ); - if (categoriesJSON) { - categories = JSON.parse(categoriesJSON); - // TODO: make this more efficient by comparing shorter key - if (categories.find((c) => c.name === category.name)) return; - 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); - } -}; |
