Add update to artists state upon new selection
This commit is contained in:
parent
0a4a1ea4db
commit
09fdb55b3e
@ -8,7 +8,7 @@ import { Search } from './Search';
|
|||||||
import { Results } from './Results';
|
import { Results } from './Results';
|
||||||
import { Artists, Artist } from '../interfaces';
|
import { Artists, Artist } from '../interfaces';
|
||||||
|
|
||||||
const QUERY_ARTIST_ALBUMS = gql`
|
const QUERY_ARTISTS = gql`
|
||||||
query Artist($byName: String!) {
|
query Artist($byName: String!) {
|
||||||
queryArtists(byName: $byName) {
|
queryArtists(byName: $byName) {
|
||||||
name
|
name
|
||||||
@ -25,7 +25,7 @@ const QUERY_ARTIST_ALBUMS = gql`
|
|||||||
|
|
||||||
export default function App() {
|
export default function App() {
|
||||||
const [inputValue, setInputValue] = useState('');
|
const [inputValue, setInputValue] = useState('');
|
||||||
const [getArtists, { data }] = useLazyQuery(QUERY_ARTIST_ALBUMS);
|
const [getArtists, { data }] = useLazyQuery(QUERY_ARTISTS);
|
||||||
const [artists, setArtists] = useState<Artists>(undefined);
|
const [artists, setArtists] = useState<Artists>(undefined);
|
||||||
const [suggestions, setSuggestions] = useState<string[] | undefined>(
|
const [suggestions, setSuggestions] = useState<string[] | undefined>(
|
||||||
undefined,
|
undefined,
|
||||||
@ -37,20 +37,32 @@ export default function App() {
|
|||||||
const updateQuery = () => {
|
const updateQuery = () => {
|
||||||
getArtists({ variables: { byName: inputValue } });
|
getArtists({ variables: { byName: inputValue } });
|
||||||
};
|
};
|
||||||
|
|
||||||
const delayedQuery = useCallback(debounce(updateQuery, 500), [inputValue]);
|
const delayedQuery = useCallback(debounce(updateQuery, 500), [inputValue]);
|
||||||
|
|
||||||
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
setInputValue(e.target.value);
|
setInputValue(e.target.value);
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
delayedQuery();
|
delayedQuery();
|
||||||
// Cancel previous debounce calls during useEffect cleanup.
|
// Cancel previous debounce calls during useEffect cleanup.
|
||||||
return delayedQuery.cancel;
|
return delayedQuery.cancel;
|
||||||
}, [inputValue, delayedQuery]);
|
}, [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);
|
setSelected(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -66,7 +78,7 @@ export default function App() {
|
|||||||
},
|
},
|
||||||
);
|
);
|
||||||
setSuggestions(updatedSuggestions);
|
setSuggestions(updatedSuggestions);
|
||||||
setArtists(updatedArtists);
|
// setArtists(updatedArtists);
|
||||||
}
|
}
|
||||||
}, [data]);
|
}, [data]);
|
||||||
|
|
||||||
@ -81,7 +93,7 @@ export default function App() {
|
|||||||
suggestions={suggestions}
|
suggestions={suggestions}
|
||||||
handleSelect={handleSelect}
|
handleSelect={handleSelect}
|
||||||
/>
|
/>
|
||||||
<Results artists={artists} selected={selected} />
|
{selected && <Results artists={artists} />}
|
||||||
</Grommet>
|
</Grommet>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -3,10 +3,12 @@ import { Box, Grid } from 'grommet';
|
|||||||
|
|
||||||
import { Artists } from '../interfaces';
|
import { Artists } from '../interfaces';
|
||||||
|
|
||||||
export const Results = ({ artists, selected }: { artists: Artists, selected: boolean }) => {
|
export const Results = ({ artists }: { artists: Artists }) => {
|
||||||
console.log('artists', artists);
|
// Selected artist is always the first of the array
|
||||||
|
const selectedArtist: Artists = artists?.splice(0, 1);
|
||||||
|
const otherArtists = artists;
|
||||||
|
|
||||||
return selected ? (
|
return (
|
||||||
<Grid
|
<Grid
|
||||||
rows={['small', 'small']}
|
rows={['small', 'small']}
|
||||||
columns={['1/4', '3/4']}
|
columns={['1/4', '3/4']}
|
||||||
@ -26,7 +28,5 @@ export const Results = ({ artists, selected }: { artists: Artists, selected: boo
|
|||||||
discography
|
discography
|
||||||
</Box>
|
</Box>
|
||||||
</Grid>
|
</Grid>
|
||||||
) : (
|
|
||||||
<></>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -6,12 +6,12 @@ export const Search = ({
|
|||||||
inputValue,
|
inputValue,
|
||||||
handleChange,
|
handleChange,
|
||||||
suggestions,
|
suggestions,
|
||||||
handleSelect
|
handleSelect,
|
||||||
}: {
|
}: {
|
||||||
inputValue: string;
|
inputValue: string;
|
||||||
handleChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
handleChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
|
||||||
suggestions: string[] | undefined;
|
suggestions: string[] | undefined;
|
||||||
handleSelect : () => void
|
handleSelect: (x: { target: HTMLElement | null; suggestion: any }) => void;
|
||||||
}) => (
|
}) => (
|
||||||
<Box
|
<Box
|
||||||
as='section'
|
as='section'
|
||||||
|
Loading…
Reference in New Issue
Block a user