-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor search #1594
Refactor search #1594
Changes from all commits
04209b9
32edffe
572b752
671e866
60c2959
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { loadListIfNecessary } from 'core/caching/cacheUtils'; | ||
import { SearchResult } from '../components/types'; | ||
import useDebounce from 'utils/hooks/useDebounce'; | ||
import { useState } from 'react'; | ||
import { | ||
resultsError, | ||
resultsLoad, | ||
resultsLoaded, | ||
SearchResultItem, | ||
} from '../store'; | ||
import { useApiClient, useAppDispatch, useAppSelector } from 'core/hooks'; | ||
|
||
type UseSearchReturn = { | ||
error: unknown; | ||
isLoading: boolean; | ||
results: SearchResultItem[]; | ||
setQuery: (q: string) => void; | ||
}; | ||
|
||
export default function useSearch(orgId: number): UseSearchReturn { | ||
const apiClient = useApiClient(); | ||
const [isTyping, setIsTyping] = useState(false); | ||
const [queryString, setQueryString] = useState(''); | ||
const dispatch = useAppDispatch(); | ||
const list = useAppSelector( | ||
(state) => state.search.matchesByQuery[queryString] | ||
); | ||
|
||
const debouncedFinishedTyping = useDebounce(async () => { | ||
setIsTyping(false); | ||
}, 600); | ||
|
||
const setQuery = (q: string) => { | ||
setQueryString(q); | ||
setIsTyping(true); | ||
debouncedFinishedTyping(); | ||
}; | ||
|
||
if (!isTyping && queryString.length > 2) { | ||
const future = loadListIfNecessary(list, dispatch, { | ||
actionOnError: (err) => resultsError([queryString, err]), | ||
actionOnLoad: () => resultsLoad(queryString), | ||
actionOnSuccess: (results) => resultsLoaded([queryString, results]), | ||
loader: () => | ||
apiClient | ||
.post<SearchResult[], { q: string }>(`/api/search?orgId=${orgId}`, { | ||
q: queryString, | ||
}) | ||
.then((results) => | ||
results.map((result) => ({ | ||
id: `${result.type}-${result.match.id}`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aaah this is so much nicer than the solution I came up with in my |
||
result, | ||
})) | ||
), | ||
}); | ||
|
||
return { | ||
error: future.error, | ||
isLoading: future.isLoading, | ||
results: future.data || [], | ||
setQuery, | ||
}; | ||
} else { | ||
return { | ||
error: null, | ||
isLoading: false, | ||
results: [], | ||
setQuery, | ||
}; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; | ||
|
||
import { SearchResult } from './components/types'; | ||
import { remoteList, RemoteList } from 'utils/storeUtils'; | ||
|
||
export type SearchResultItem = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep i should have done like this as well.... |
||
id: string; | ||
result: SearchResult; | ||
}; | ||
|
||
export interface SearchStoreSlice { | ||
matchesByQuery: Record<string, RemoteList<SearchResultItem>>; | ||
} | ||
|
||
const initialState: SearchStoreSlice = { | ||
matchesByQuery: {}, | ||
}; | ||
|
||
const searchSlice = createSlice({ | ||
initialState, | ||
name: 'search', | ||
reducers: { | ||
resultsError: (state, action: PayloadAction<[string, unknown]>) => { | ||
const [query, error] = action.payload; | ||
state.matchesByQuery[query] = remoteList(); | ||
state.matchesByQuery[query].error = error; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! I have not seen the error part of these in action anywhere else i think, seems like something we want to take into more parts! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True! I just delayed error handling (lazily) but there was a test here that failed, and I had to decide whether to rip out the test and all the error code, or just implement crude error handling, so I chose the latter. 😊 |
||
state.matchesByQuery[query].loaded = new Date().toISOString(); | ||
}, | ||
resultsLoad: (state, action: PayloadAction<string>) => { | ||
const query = action.payload; | ||
state.matchesByQuery[query] = remoteList(); | ||
state.matchesByQuery[query].isLoading = true; | ||
}, | ||
resultsLoaded: ( | ||
state, | ||
action: PayloadAction<[string, SearchResultItem[]]> | ||
) => { | ||
const [query, results] = action.payload; | ||
state.matchesByQuery[query] = remoteList(results); | ||
state.matchesByQuery[query].loaded = new Date().toISOString(); | ||
}, | ||
}, | ||
}); | ||
|
||
export default searchSlice; | ||
export const { resultsError, resultsLoad, resultsLoaded } = searchSlice.actions; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ooo smart addition! I can see this coming in handy! :D