diff --git a/src/components/App.tsx b/src/components/App.tsx index 6adf37a..229d93b 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -26,21 +26,22 @@ const QUERY_ARTISTS = gql` export default function App() { const [inputValue, setInputValue] = useState(''); const [getArtists, { data }] = useLazyQuery(QUERY_ARTISTS); - const [artists, setArtists] = useState(undefined); - const [suggestions, setSuggestions] = useState( - undefined, - ); + const [artists, setArtists] = useState([]); + const [suggestions, setSuggestions] = useState(); const [selected, setSelected] = useState(false); // Debounce the database query // Based on: https://archive.is/wip/6JDqb - const updateQuery = () => { - getArtists({ variables: { byName: inputValue } }); - }; - const delayedQuery = useCallback(debounce(updateQuery, 500), [inputValue]); const handleChange = (e: React.ChangeEvent) => { setInputValue(e.target.value); }; + + const updateQuery = () => { + getArtists({ variables: { byName: inputValue } }); + }; + + const delayedQuery = useCallback(debounce(updateQuery, 500), [inputValue]); + useEffect(() => { delayedQuery(); // Cancel previous debounce calls during useEffect cleanup. @@ -49,15 +50,19 @@ export default function App() { const handleSelect = (suggestion: any) => { const selectedName: string = suggestion.suggestion; - const suggestedArtists = data.queryArtists.slice(0, 5); - let updatedArtists: Artists; + let updatedArtists: Artists = []; + + // Use map to create a copy of the array + let suggestedArtists: Artists = data.queryArtists + .slice(0, 5) + .map((item: Artist) => item); // 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!] + for (let i = 0; i < suggestedArtists!.length; i++) { + if (suggestedArtists![i].name === selectedName) { + let selectedArtist: Artists = suggestedArtists?.splice(i, 1); + let otherArtists: Artists = suggestedArtists; + updatedArtists = [...selectedArtist, ...otherArtists]; break; } } @@ -78,7 +83,6 @@ export default function App() { }, ); setSuggestions(updatedSuggestions); - // setArtists(updatedArtists); } }, [data]); diff --git a/src/components/Results.tsx b/src/components/Results.tsx index 738cf89..a70f895 100644 --- a/src/components/Results.tsx +++ b/src/components/Results.tsx @@ -1,16 +1,18 @@ import React from 'react'; -import { Box, Grid } from 'grommet'; +import { Box, Grid, Avatar } from 'grommet'; import { Artists } from '../interfaces'; 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; + const { + name: selectedName, + image: selectedImage, + albums: selectedAlbums, + } = artists[0]; return ( { ]} > - Artist + +

{selectedName}

- Similar +

Similar results

+ {artists.map((artist) => ( + + +

{artist.name}

+
+ ))}
- discography +

discography

+ {selectedAlbums.map((albums) => ( + + +

{albums.name}

+
+ ))}
); diff --git a/src/interfaces/index.ts b/src/interfaces/index.ts index d199c5f..eb35373 100644 --- a/src/interfaces/index.ts +++ b/src/interfaces/index.ts @@ -11,4 +11,4 @@ export interface Artist { albums: Album[]; } -export type Artists = Artist[] | undefined; \ No newline at end of file +export type Artists = Artist[]; diff --git a/tsconfig.json b/tsconfig.json index f2850b7..4acaa3a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -17,7 +17,8 @@ "resolveJsonModule": true, "isolatedModules": true, "noEmit": true, - "jsx": "react" + "jsx": "react", + "downlevelIteration": true }, "include": [ "src"