From 7596b69482914569cbb4bb5f287bbc0a72d74133 Mon Sep 17 00:00:00 2001 From: Justin Shillingford Date: Wed, 19 Aug 2020 13:27:22 -0400 Subject: [TMA-161] Recently Searched Users (#34) * Basic AsyncStorage code * Basic implementation complete Need to fix re-rendering bug * Removed errant comment * Fixed bug for rerendering upon addition to recents Need to fix bug for rerendering upon clearing * Fixed rerendering bug for clear * Only present recents header if users in recents * Lint cleaning * Basic AsyncStorage code * Basic implementation complete Need to fix re-rendering bug * Removed errant comment * Fixed bug for rerendering upon addition to recents Need to fix bug for rerendering upon clearing * Fixed rerendering bug for clear * Only present recents header if users in recents * Lint cleaning * Added comments for a function * Updated conditional presentation to use ternary * Created component for List Section Headers * Lint cleaning * Converted component to be for Recent Searches As opposed to just the list header --- src/routes/authentication/AuthProvider.tsx | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'src/routes/authentication') diff --git a/src/routes/authentication/AuthProvider.tsx b/src/routes/authentication/AuthProvider.tsx index e52d56bc..589cb051 100644 --- a/src/routes/authentication/AuthProvider.tsx +++ b/src/routes/authentication/AuthProvider.tsx @@ -1,7 +1,13 @@ import React, {useEffect} from 'react'; import {createContext, useState} from 'react'; import RNFetchBlob from 'rn-fetch-blob'; -import {UserType, ProfileType, InstagramPostType} from '../../types'; +import AsyncStorage from '@react-native-community/async-storage'; +import { + UserType, + ProfileType, + InstagramPostType, + ProfilePreviewType, +} from '../../types'; import { PROFILE_INFO_ENDPOINT, AVATAR_PHOTO_ENDPOINT, @@ -17,6 +23,7 @@ interface AuthContextProps { avatar: string | null; cover: string | null; instaPosts: Array; + recentSearches: Array; } const NO_USER: UserType = { userId: '', @@ -35,6 +42,7 @@ export const AuthContext = createContext({ avatar: null, cover: null, instaPosts: [], + recentSearches: [], }); /** @@ -46,6 +54,9 @@ const AuthProvider: React.FC = ({children}) => { const [avatar, setAvatar] = useState(null); const [cover, setCover] = useState(null); const [instaPosts, setInstaPosts] = useState>([]); + const [recentSearches, setRecentSearches] = useState< + Array + >([]); const {userId} = user; useEffect(() => { @@ -115,10 +126,21 @@ const AuthProvider: React.FC = ({children}) => { console.log(error); } }; + const loadRecentlySearchedUsers = async () => { + try { + const asyncCache = await AsyncStorage.getItem( + '@recently_searched_users', + ); + asyncCache != null ? setRecentSearches(JSON.parse(asyncCache)) : null; + } catch (e) { + console.log(e); + } + }; loadProfileInfo(); loadAvatar(); loadCover(); loadInstaPosts(); + loadRecentlySearchedUsers(); }, [userId]); return ( @@ -135,6 +157,7 @@ const AuthProvider: React.FC = ({children}) => { logout: () => { setUser(NO_USER); }, + recentSearches, }}> {children} -- cgit v1.2.3-70-g09d2