diff options
author | Ivan Chen <ivan@tagg.id> | 2021-03-08 16:27:08 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-08 16:27:08 -0500 |
commit | d77f43663fbe409b011b5509bcbff3d07f8ded55 (patch) | |
tree | 6bef0a405a92185d08b0850d199ee507e8bfe90c /src/utils | |
parent | 7e5f9c63360f8c4505bb414384e13f8c0f7576e4 (diff) | |
parent | 973c8a03681724f2e303fcd021301764bfc4717c (diff) |
Merge pull request #286 from leonyjiang/tma660-recently-searched-categories
[TMA-660] Recently Searched Categories
Diffstat (limited to 'src/utils')
-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); + } +}; |