Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
Search edits (#86)
Browse files Browse the repository at this point in the history
* Fix search header shadow

* Prevent udpate search history before result

* Put search history in ref

* Reset the results on search clear
  • Loading branch information
jowlee committed May 3, 2021
1 parent 778600d commit c1e7f58
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const styles = StyleSheet.create({
fontSize: 24,
fontFamily: 'AvenirNextLTPro-Heavy',
textShadowColor: 'rgba(126, 27, 204, 0.15)',
textShadowOffset: { width: 0, height: 1 },
textShadowRadius: 3
textShadowOffset: { width: 0, height: 2 },
textShadowRadius: 4
}
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback } from 'react'
import React, { useCallback, useEffect, useState } from 'react'
import { useDispatch } from 'react-redux'
import {
StyleSheet,
Expand Down Expand Up @@ -113,31 +113,42 @@ const SearchHistoryItem = ({ text }: SearchHistoryItemProps) => {
}

const SearchHistory = () => {
const { searchHistory, clearHistory } = useSearchHistory()
const { searchHistory, clearHistory, hasFetched } = useSearchHistory()
const [displayHistory, setDisplayHistory] = useState(searchHistory.current)

// Only update history on component load and clearing of search history
useEffect(() => {
if (hasFetched) setDisplayHistory(searchHistory.current)
}, [hasFetched, setDisplayHistory])

const onClearHistory = useCallback(() => {
setDisplayHistory([])
clearHistory()
}, [clearHistory, setDisplayHistory])

const backgroundColor = useColor('neutralLight8')
const containerStyles = useTheme(styles.container, {
borderTopColor: 'neutralLight8'
})
const clearTextStyle = useTheme(styles.clearText, { color: 'neutralLight4' })

if (!searchHistory || searchHistory.length === 0) {
if (!displayHistory || displayHistory.length === 0) {
return <EmptySearch />
}

return (
<View style={containerStyles} onTouchStart={Keyboard.dismiss}>
<FlatList
keyboardShouldPersistTaps={'always'}
data={searchHistory.concat('clear')}
data={displayHistory.concat('clear')}
keyExtractor={(item, idx) => `${item}-${idx}`}
renderItem={({ item }) =>
item !== 'clear' ? (
<SearchHistoryItem key={item} text={item} />
) : (
<TouchableHighlight
key={'clear'}
onPress={clearHistory}
onPress={onClearHistory}
style={styles.clearTouchable}
underlayColor={backgroundColor}
activeOpacity={0.8}
Expand Down
16 changes: 12 additions & 4 deletions packages/audius-mobile-client/src/store/search/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AsyncStorage from '@react-native-community/async-storage'
import { useCallback, useEffect } from 'react'
import { useCallback, useEffect, useRef, useState } from 'react'
import { useSelector, useDispatch } from 'react-redux'
import * as searchActions from './actions'
import { getSearchHistory } from './selectors'
Expand Down Expand Up @@ -30,14 +30,21 @@ export const setSearchHistory = async (history: string[]) => {
const useSearchHistory = () => {
const searchHistory = useSelector(getSearchHistory)
const dispatch = useDispatch()
const [hasFetched, setHasFetched] = useState(false)

const searchHistoryInstance = useRef(searchHistory)
useEffect(() => {
searchHistoryInstance.current = searchHistory
}, [searchHistory])

useEffect(() => {
const work = async () => {
const history = await fetchSearchHistory()
dispatch(searchActions.setSearchHistory(history))
setHasFetched(true)
}
work()
}, [dispatch])
}, [dispatch, setHasFetched])

const clearHistory = useCallback(async () => {
dispatch(searchActions.setSearchHistory([]))
Expand All @@ -57,9 +64,10 @@ const useSearchHistory = () => {
)

return {
searchHistory,
searchHistory: searchHistoryInstance,
clearHistory,
appendSearchItem
appendSearchItem,
hasFetched
}
}

Expand Down
9 changes: 8 additions & 1 deletion packages/audius-mobile-client/src/store/search/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,17 @@ const reducer = (state: SearchState = initialState, action: SearchActions) => {
isOpen: false
}
case UPDATE_QUERY: {
return {
const updatedState = {
...state,
query: action.query
}
if (action.query === '') {
updatedState.results = {
...initialState.results
}
updatedState.resultQuery = ''
}
return updatedState
}
case SUBMIT_QUERY: {
return {
Expand Down

0 comments on commit c1e7f58

Please sign in to comment.