aboutsummaryrefslogtreecommitdiff
path: root/src/utils/users.ts
diff options
context:
space:
mode:
authorLeon Jiang <35908040+leonyjiang@users.noreply.github.com>2021-03-10 18:15:26 -0500
committerLeon Jiang <35908040+leonyjiang@users.noreply.github.com>2021-03-10 18:15:26 -0500
commit594de4668248bac9b744e6882329183c95ac339c (patch)
tree3df4f2b29fe50f932f91f4bc7bc38eebb66215c2 /src/utils/users.ts
parent0d4e5234d011921514037df756557afc660d2188 (diff)
Add utils, fix ordering of recent searches
Diffstat (limited to 'src/utils/users.ts')
-rw-r--r--src/utils/users.ts102
1 files changed, 75 insertions, 27 deletions
diff --git a/src/utils/users.ts b/src/utils/users.ts
index 15107c99..3e0d9eef 100644
--- a/src/utils/users.ts
+++ b/src/utils/users.ts
@@ -166,55 +166,86 @@ 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();
+/*
+ * 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;
}
- recentlySearchedList.unshift(user);
}
+ 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);
}
- } else {
- recentlySearchedList = [user];
+ } catch (e) {
+ console.log(e);
}
+};
+
+/*
+ * Retrieves and returns user's recently-searched categories from AsyncStorage.
+ */
+export const getRecentlySearchedUsers = async (): Promise<
+ ProfilePreviewType[]
+> => {
try {
- let recentlySearchedListString = JSON.stringify(recentlySearchedList);
- await AsyncStorage.setItem(
- '@recently_searched_users',
- recentlySearchedListString,
- );
+ const usersJSON = await AsyncStorage.getItem(recentlySearchedUsersKey);
+ if (usersJSON) return JSON.parse(usersJSON);
} catch (e) {
console.log(e);
}
+ return [];
};
/*
- * Stores `category` in AsyncStorage as a recently searched category.
+ * 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,
) => {
- const recentlySearchedCategoriesKey = '@recently_searched_categories';
let categories: CategoryPreviewType[];
- // retrieve recently-searched categories and set new list
+ // retrieve and update recently-searched categories 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;
+ // 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];
@@ -232,3 +263,20 @@ export const addCategoryToRecentlySearched = async (
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 [];
+};