Skip to content

Commit

Permalink
improved searchPage reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
johan-lindell committed Oct 1, 2024
1 parent f298f33 commit 9a56fee
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
12 changes: 2 additions & 10 deletions components/header/navbar/searchpage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,14 @@ const SearchOverlay = ({
const debouncedHandleSearch = debounce(handleSearch, 500, debounceTimeout)
const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const { value } = event.target
dispatch({ type: 'SET_IS_FETCHED', payload: false })
dispatch({ type: 'SET_QUERY', payload: value })
dispatch({ type: 'INPUT_CHANGED', payload: value })
if (value && value.length >= 2) {
dispatch({ type: 'SET_SEARCHING', payload: true })
debouncedHandleSearch([value, fileSearch])
}
}

const toggleSearchType = () => {
dispatch({ type: 'SET_IS_FETCHED', payload: false })
dispatch({ type: 'SET_FILE_SEARCH', payload: !fileSearch })
dispatch({ type: 'TOGGLE_SEARCH_TYPE' })
handleSearch(query, !fileSearch)
}

Expand Down Expand Up @@ -178,13 +175,8 @@ const SearchOverlay = ({
Vill du söka filer?{' '}
<span
onClick={() => {
dispatch({
type: 'SET_IS_FETCHED',
payload: false,
})
dispatch({
type: 'SET_FILE_SEARCH',
payload: true,
})
handleSearch(query, true)
}}
Expand Down
27 changes: 10 additions & 17 deletions components/header/navbar/searchpage/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,14 @@ type State = {
}

type Action =
| { type: 'SET_QUERY'; payload: string }
| { type: 'SET_FILE_SEARCH'; payload: boolean }
| { type: 'SET_FILE_SEARCH' }
| { type: 'SET_SEARCHING'; payload: boolean }
| { type: 'SET_IS_FETCHED'; payload: boolean }
| { type: 'SET_FILE_RESULTS'; payload: drive_v3.Schema$FileList }
| { type: 'SET_RESULTS'; payload: SearchData }
| { type: 'CLEAR_RESULTS' }
| {
type: 'APPEND_FILE_RESULTS'
payload: { filteredFiles: drive_v3.Schema$File[]; nextPageToken: string }
}
| { type: 'TOGGLE_SEARCH_TYPE' }
| { type: 'INPUT_CHANGED'; payload: string }

export const initialState: State = {
query: '',
Expand All @@ -39,10 +36,8 @@ export const initialState: State = {

export const searchReducer = (state: State, action: Action): State => {
switch (action.type) {
case 'SET_QUERY':
return { ...state, query: action.payload }
case 'SET_FILE_SEARCH':
return { ...state, fileSearch: action.payload }
return { ...state, fileSearch: true, isFetched: false }
case 'SET_SEARCHING':
return { ...state, searching: action.payload }
case 'SET_IS_FETCHED':
Expand All @@ -51,16 +46,14 @@ export const searchReducer = (state: State, action: Action): State => {
return { ...state, fileResults: action.payload }
case 'SET_RESULTS':
return { ...state, results: action.payload }
case 'APPEND_FILE_RESULTS':
case 'TOGGLE_SEARCH_TYPE':
return { ...state, fileSearch: !state.fileSearch, isFetched: false }
case 'INPUT_CHANGED':
return {
...state,
fileResults: {
files: [
...(state.fileResults.files ?? []),
...(action.payload.filteredFiles || []),
],
nextPageToken: action.payload.nextPageToken,
},
query: action.payload,
isFetched: false,
searching: action.payload.length >= 2,
}
case 'CLEAR_RESULTS':
return { ...initialState, fileSearch: state.fileSearch }
Expand Down

0 comments on commit 9a56fee

Please sign in to comment.