From a27a6281a0bce3909ac387ffe4fd647b26105024 Mon Sep 17 00:00:00 2001 From: rui hildt Date: Fri, 24 Jul 2020 13:58:02 +0200 Subject: [PATCH] Add possibility to load other results on click --- src/components/App.tsx | 41 ++++++++++++++++++++++---------------- src/components/Other.tsx | 4 ++-- src/components/Results.tsx | 2 +- src/components/Search.tsx | 10 +++++----- 4 files changed, 32 insertions(+), 25 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index 72604e0..31aabdb 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -27,20 +27,20 @@ export default function App() { const [inputValue, setInputValue] = useState(''); const [getArtists, { data }] = useLazyQuery(QUERY_ARTISTS); const [artists, setArtists] = useState([]); - const [suggestions, setSuggestions] = useState(); + const [suggestions, setSuggestions] = useState([]); const [selected, setSelected] = useState(false); // Debounce the database query // Based on: https://archive.is/wip/6JDqb - const handleChange = (e: React.ChangeEvent) => { - setInputValue(e.target.value); + const handleChange = (value: string) => { + setInputValue(value); }; const updateQuery = () => { getArtists({ variables: { byName: inputValue } }); }; - const delayedQuery = useCallback(debounce(updateQuery, 500), [inputValue]); + const delayedQuery = useCallback(debounce(updateQuery, 200), [inputValue]); useEffect(() => { delayedQuery(); @@ -48,18 +48,14 @@ export default function App() { return delayedQuery.cancel; }, [inputValue, delayedQuery]); - const handleSelect = (suggestion: any) => { - const selectedName: string = suggestion.suggestion; + const handleSelect = (suggestion: string) => { let updatedArtists: Artists = []; - // Use map to create a copy of the array - let suggestedArtists: Artists = data.queryArtists - .slice(0, 5) - .map((item: Artist) => item); + let suggestedArtists: Artists = data.queryArtists.slice(0, 5); // Find the selected artist and move it to index 0 for (let i = 0; i < suggestedArtists!.length; i++) { - if (suggestedArtists![i].name === selectedName) { + if (suggestedArtists![i].name === suggestion) { let selectedArtist: Artists = suggestedArtists?.splice(i, 1); let otherArtists: Artists = suggestedArtists; updatedArtists = [...selectedArtist, ...otherArtists]; @@ -71,13 +67,22 @@ export default function App() { setSelected(true); }; - const handleClick = () => { - console.log('click') - } + const handleClick = (name: string) => { + let updatedArtists: Artists = []; + + for (let i = 0; i < artists.length; i++) { + if (artists[i].name === name) { + let selectedArtist: Artists = artists.splice(i, 1); + let otherArtists: Artists = artists; + updatedArtists = [...selectedArtist, ...otherArtists]; + break; + } + } + + setArtists(updatedArtists); + }; useEffect(() => { - // TODO optimize re-rendering, probably by using onCompleted instead of this useEffect - // See https://github.com/apollographql/apollo-client/issues/5268#issuecomment-596950174 if (data && data.queryArtists !== []) { // Limit artists to 5 const updatedArtists = data.queryArtists.slice(0, 5); @@ -101,7 +106,9 @@ export default function App() { suggestions={suggestions} handleSelect={handleSelect} /> - {selected && } + {selected && ( + + )} ); } diff --git a/src/components/Other.tsx b/src/components/Other.tsx index 8bd4b98..25bb050 100644 --- a/src/components/Other.tsx +++ b/src/components/Other.tsx @@ -8,7 +8,7 @@ export const Other = ({ }: { image: string; name: string; - handleClick: ((...args: any[]) => any) & ((event: MouseEvent) => void); + handleClick: (name: string) => void; }) => { return ( handleClick(name)} > any) & ((event: MouseEvent) => void); + handleClick: (name: string) => void; }) => { const { name: selectedName, diff --git a/src/components/Search.tsx b/src/components/Search.tsx index fade019..9ed92cf 100644 --- a/src/components/Search.tsx +++ b/src/components/Search.tsx @@ -9,9 +9,9 @@ export const Search = ({ handleSelect, }: { inputValue: string; - handleChange: (e: React.ChangeEvent) => void; - suggestions: string[] | undefined; - handleSelect: (x: { target: HTMLElement | null; suggestion: any }) => void; + handleChange: (value: string) => void; + suggestions: string[]; + handleSelect: (suggestion: string) => void; }) => ( handleChange(e.target.value)} + onSelect={(target) => handleSelect(target.suggestion)} placeholder='Type an artist name' icon={} dropHeight='large'