diff --git a/package-lock.json b/package-lock.json index 3932ebcb..ee2ca57c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -48,7 +48,7 @@ "react-native-screens": "~3.22.0", "react-native-svg": "13.9.0", "react-native-url-polyfill": "^2.0.0", - "react-native-vector-icons": "^10.0.0", + "react-native-vector-icons": "^10.0.2", "react-scroll-to-top": "^3.0.0" }, "devDependencies": { @@ -16420,9 +16420,9 @@ } }, "node_modules/react-native-vector-icons": { - "version": "10.0.0", - "resolved": "https://registry.npmjs.org/react-native-vector-icons/-/react-native-vector-icons-10.0.0.tgz", - "integrity": "sha512-efMOVbZIebY8RszZPzPBoXi9pvD/NFYmjIDYxRoc9LYSzV8rMJtT8FfcO2hPu85Rn2x9xktha0+qn0B7EqMAcQ==", + "version": "10.0.2", + "resolved": "https://registry.npmjs.org/react-native-vector-icons/-/react-native-vector-icons-10.0.2.tgz", + "integrity": "sha512-ZwhUkJhIMkGL3cW7IT4sEEHu2AOzerqsRQ73UzXsB+ecBpVK5bRmp0XswiQleZKZalZfs/WIfWLXLfTQHcQo6A==", "dependencies": { "prop-types": "^15.7.2", "yargs": "^16.1.1" diff --git a/package.json b/package.json index c86d30e9..2d3ca87b 100644 --- a/package.json +++ b/package.json @@ -52,7 +52,7 @@ "react-native-screens": "~3.22.0", "react-native-svg": "13.9.0", "react-native-url-polyfill": "^2.0.0", - "react-native-vector-icons": "^10.0.0", + "react-native-vector-icons": "^10.0.2", "react-scroll-to-top": "^3.0.0" }, "devDependencies": { diff --git a/src/app/(tabs)/search/index.tsx b/src/app/(tabs)/search/index.tsx index 60ad27d7..2d9be8fc 100644 --- a/src/app/(tabs)/search/index.tsx +++ b/src/app/(tabs)/search/index.tsx @@ -3,11 +3,12 @@ import { useIsFocused } from '@react-navigation/native'; import { SearchBar } from '@rneui/themed'; import { Link, router } from 'expo-router'; import React, { useEffect, useState } from 'react'; -import { Button, FlatList, View } from 'react-native'; +import { Button, FlatList, View, Text } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import styles from './styles'; import FilterModal from '../../../components/FilterModal/FilterModal'; +import RecentSearchCard from '../../../components/RecentSearchCard/RecentSearchCard'; import SearchCard from '../../../components/SearchCard/SearchCard'; import { fetchAllStoryPreviews } from '../../../queries/stories'; import { StoryPreview, RecentSearch } from '../../../queries/types'; @@ -37,11 +38,37 @@ function SearchScreen() { setSearchResults(updatedData); }; + const clearRecentSearches = () => { + setRecentSearches([]); + setRecentSearch([]); + }; + + const searchResultStacking = (searchString: string) => { + if (searchString !== '') { + const maxArrayLength = 5; + setRecentSearches(recentSearches.reverse()); + for (let i = 0; i < recentSearches.length; i++) { + if (searchString === recentSearches[i].value) { + setRecentSearches(recentSearches.splice(i, 1)); + break; + } + } + if (recentSearches.length >= maxArrayLength) { + setRecentSearches(recentSearches.splice(0, 1)); + } + + const result: RecentSearch = { + value: searchString, + numResults: searchResults.length, + }; + setRecentSearches(recentSearches.concat(result).reverse()); + } + }; + // Gets the recentSearches (Set) from Async Storage const getRecentSearch = async () => { try { - const jsonValue = await AsyncStorage.getItem('1'); //'GWN_RECENT_SEARCHES_ARRAY'); - console.log('\nGetting AsyncStorage: ' + jsonValue); + const jsonValue = await AsyncStorage.getItem('GWN_RECENT_SEARCHES_ARRAY'); return jsonValue != null ? JSON.parse(jsonValue) : []; } catch (error) { console.log(error); // error reading value @@ -52,8 +79,7 @@ function SearchScreen() { const setRecentSearch = async (recentSearchesArray: RecentSearch[]) => { try { const jsonValue = JSON.stringify(recentSearchesArray); - console.log('\nSetting AsyncStorage: ' + jsonValue); - await AsyncStorage.setItem('1', jsonValue); // 'GWN_RECENT_SEARCHES_ARRAY', jsonValue); + await AsyncStorage.setItem('GWN_RECENT_SEARCHES_ARRAY', jsonValue); } catch (error) { console.log(error); // saving error } @@ -69,12 +95,9 @@ function SearchScreen() { })(); }, []); - // UseEffect upon change of recentSearches (Set) - // EVENTUALLY FIX TO WHERE IT DOES IT BEFORE EXITING PAGE RATHER THAN EVERY ALTERATION OF SET (LIKE THIS FOR TESTING RN) useEffect(() => { setRecentSearch(recentSearches); - console.log(''); - }, [focus]); // fix this useEffect to be when it switches page + }, [focus]); return ( @@ -94,18 +117,42 @@ function SearchScreen() { onChangeText={text => searchFunction(text)} value={search} onSubmitEditing={searchString => { - const result: RecentSearch = { - value: searchString.nativeEvent.text, // works - numResults: searchResults.length, // works - }; - - setRecentSearches(recentSearches.concat(result)); + searchResultStacking(searchString.nativeEvent.text); }} />