diff options
author | Ivan Chen <ivan@tagg.id> | 2021-03-08 19:54:42 -0500 |
---|---|---|
committer | Ivan Chen <ivan@tagg.id> | 2021-03-08 19:54:42 -0500 |
commit | f899dc058f90813c5ae71e55407a11e6ca13e1ed (patch) | |
tree | 84c93e869fab6d434d6709dc1d471c6e588c4e4e /src/utils/users.ts | |
parent | 8837ea74dbff9cf455cd7cb092767e51de3900f8 (diff) | |
parent | d77f43663fbe409b011b5509bcbff3d07f8ded55 (diff) |
Merge branch 'master' into tma684-search-for-badges
# Conflicts:
# src/components/search/SearchResultCell.tsx
# src/components/search/SearchResultList.tsx
Diffstat (limited to 'src/utils/users.ts')
-rw-r--r-- | src/utils/users.ts | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/utils/users.ts b/src/utils/users.ts index 653c941e..15107c99 100644 --- a/src/utils/users.ts +++ b/src/utils/users.ts @@ -19,6 +19,7 @@ import {AppDispatch} from './../store/configureStore'; import {RootState} from './../store/rootReducer'; import { ProfilePreviewType, + CategoryPreviewType, ProfileType, ScreenType, UserType, @@ -196,3 +197,38 @@ export const addUserToRecentlyViewed = async (user: ProfilePreviewType) => { 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); + } +}; |