diff options
author | Leon Jiang <35908040+leonyjiang@users.noreply.github.com> | 2021-03-08 03:20:38 -0500 |
---|---|---|
committer | Leon Jiang <35908040+leonyjiang@users.noreply.github.com> | 2021-03-08 03:20:38 -0500 |
commit | 46a578b5cc9c3230448e8d9db0ba6ff75d5b8852 (patch) | |
tree | f20839746850f2b093a03e58755c484e55092227 /src | |
parent | 9c101786697df2c3fe79a2d9186a7f79530b7283 (diff) |
Create util for saving recently-searched categories
Diffstat (limited to 'src')
-rw-r--r-- | src/utils/users.ts | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/src/utils/users.ts b/src/utils/users.ts index 653c941e..efae6d20 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,36 @@ 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); + 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); + } +}; |