From 0a4a1ea4db870e3a573473a5e5b711de3c093b66 Mon Sep 17 00:00:00 2001 From: rui hildt Date: Thu, 23 Jul 2020 14:23:26 +0200 Subject: [PATCH] Fix Results conditional render on artist selection --- src/components/App.tsx | 18 ++++++++++++------ src/components/Results.tsx | 4 ++-- src/components/Search.tsx | 3 +++ 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/components/App.tsx b/src/components/App.tsx index 273f106..f9e5848 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -30,27 +30,32 @@ export default function App() { const [suggestions, setSuggestions] = useState( undefined, ); + 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); }; - + useEffect(() => { delayedQuery(); // Cancel previous debounce calls during useEffect cleanup. return delayedQuery.cancel; }, [inputValue, delayedQuery]); - + + const handleSelect = () => { + setSelected(true); + }; + useEffect(() => { - // TODO optimize rendering, probably by using onCompleted instead of this 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 @@ -74,8 +79,9 @@ export default function App() { inputValue={inputValue} handleChange={handleChange} suggestions={suggestions} + handleSelect={handleSelect} /> - + ); } diff --git a/src/components/Results.tsx b/src/components/Results.tsx index 6517669..49b04d4 100644 --- a/src/components/Results.tsx +++ b/src/components/Results.tsx @@ -3,10 +3,10 @@ import { Box, Grid } from 'grommet'; import { Artists } from '../interfaces'; -export const Results = ({ artists }: { artists: Artists }) => { +export const Results = ({ artists, selected }: { artists: Artists, selected: boolean }) => { console.log('artists', artists); - return artists ? ( + return selected ? ( ) => void; suggestions: string[] | undefined; + handleSelect : () => void }) => ( } dropHeight='large'