From 09fdb55b3efd0d7fef3598556a0e6191f7a2b8bc Mon Sep 17 00:00:00 2001 From: rui hildt Date: Thu, 23 Jul 2020 16:23:15 +0200 Subject: [PATCH] Add update to artists state upon new selection --- src/components/App.tsx | 28 ++++++++++++++++++++-------- src/components/Results.tsx | 10 +++++----- src/components/Search.tsx | 4 ++-- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index f9e5848..6adf37a 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -8,7 +8,7 @@ import { Search } from './Search'; import { Results } from './Results'; import { Artists, Artist } from '../interfaces'; -const QUERY_ARTIST_ALBUMS = gql` +const QUERY_ARTISTS = gql` query Artist($byName: String!) { queryArtists(byName: $byName) { name @@ -25,7 +25,7 @@ const QUERY_ARTIST_ALBUMS = gql` export default function App() { const [inputValue, setInputValue] = useState(''); - const [getArtists, { data }] = useLazyQuery(QUERY_ARTIST_ALBUMS); + const [getArtists, { data }] = useLazyQuery(QUERY_ARTISTS); const [artists, setArtists] = useState(undefined); const [suggestions, setSuggestions] = useState( undefined, @@ -37,20 +37,32 @@ export default function App() { const updateQuery = () => { getArtists({ variables: { byName: inputValue } }); }; - const delayedQuery = useCallback(debounce(updateQuery, 500), [inputValue]); - const handleChange = (e: React.ChangeEvent) => { setInputValue(e.target.value); }; - useEffect(() => { delayedQuery(); // Cancel previous debounce calls during useEffect cleanup. return delayedQuery.cancel; }, [inputValue, delayedQuery]); - const handleSelect = () => { + const handleSelect = (suggestion: any) => { + const selectedName: string = suggestion.suggestion; + const suggestedArtists = data.queryArtists.slice(0, 5); + let updatedArtists: Artists; + + // Find the selected artist and move it to index 0 + for (let i = 0; i < suggestedArtists.length; i++) { + if (suggestedArtists[i].name === selectedName) { + const selectedArtist: Artists = suggestedArtists?.splice(i, 1); + const otherArtists: Artists = suggestedArtists; + updatedArtists = [...selectedArtist!, ...otherArtists!] + break; + } + } + + setArtists(updatedArtists); setSelected(true); }; @@ -66,7 +78,7 @@ export default function App() { }, ); setSuggestions(updatedSuggestions); - setArtists(updatedArtists); + // setArtists(updatedArtists); } }, [data]); @@ -81,7 +93,7 @@ export default function App() { suggestions={suggestions} handleSelect={handleSelect} /> - + {selected && } ); } diff --git a/src/components/Results.tsx b/src/components/Results.tsx index 49b04d4..738cf89 100644 --- a/src/components/Results.tsx +++ b/src/components/Results.tsx @@ -3,10 +3,12 @@ import { Box, Grid } from 'grommet'; import { Artists } from '../interfaces'; -export const Results = ({ artists, selected }: { artists: Artists, selected: boolean }) => { - console.log('artists', artists); +export const Results = ({ artists }: { artists: Artists }) => { + // Selected artist is always the first of the array + const selectedArtist: Artists = artists?.splice(0, 1); + const otherArtists = artists; - return selected ? ( + return ( - ) : ( - <> ); }; diff --git a/src/components/Search.tsx b/src/components/Search.tsx index 54d192f..fade019 100644 --- a/src/components/Search.tsx +++ b/src/components/Search.tsx @@ -6,12 +6,12 @@ export const Search = ({ inputValue, handleChange, suggestions, - handleSelect + handleSelect, }: { inputValue: string; handleChange: (e: React.ChangeEvent) => void; suggestions: string[] | undefined; - handleSelect : () => void + handleSelect: (x: { target: HTMLElement | null; suggestion: any }) => void; }) => (