diff --git a/src/apps/concepts/components/ViewConceptsHeader.tsx b/src/apps/concepts/components/ViewConceptsHeader.tsx index 36f674f7f..1a919093b 100644 --- a/src/apps/concepts/components/ViewConceptsHeader.tsx +++ b/src/apps/concepts/components/ViewConceptsHeader.tsx @@ -31,7 +31,6 @@ import { import { APIDictionary } from "../../dictionaries/types"; import { VerifiedSource } from "../../../components/VerifiedSource"; import InfiniteScroll from "react-infinite-scroll-component"; - interface Props { containerType: string; containerUrl?: string; @@ -46,7 +45,6 @@ interface Props { initialSearch: string; pathUrl: Function; } - const useStyles = makeStyles((theme: Theme) => createStyles({ lightColour: { @@ -56,6 +54,9 @@ const useStyles = makeStyles((theme: Theme) => padding: "0.2rem 1rem", cursor: "none" }, + sourcesDropdownHeader: { + padding: "0.5rem 1rem" + }, input: { cursor: "pointer", borderBottom: "1px dotted black", @@ -79,7 +80,6 @@ const useStyles = makeStyles((theme: Theme) => } }) ); - const ViewConceptsHeader: React.FC = ({ containerType, containerUrl, @@ -92,44 +92,36 @@ const ViewConceptsHeader: React.FC = ({ initialSearch, pathUrl }) => { - const [showSources, setShowSources] = useState(false); - const [q, setQ] = useState(initialSearch); - const [preferredSources, setPreferredSources] = useState< + const [showAllSources, setShowAllSources] = useState(false); + const [queryString, setQueryString] = useState(initialSearch); + const [currentSources, setCurrentSources] = useState< { name: string; url: string }[] >(); useEffect(() => { const defaultSources = Object.entries( PREFERRED_SOURCES_VIEW_ONLY ).map(([key, value]) => ({ name: key, url: value })); - if (showSources) { + if (showAllSources) { const allSources = defaultSources .concat(sources.map(s => ({ name: s.name, url: s.url }))) .concat(dictionaries.map(d => ({ name: d.name, url: d.url }))); - setPreferredSources(allSources); + setCurrentSources(allSources); console.log(allSources); - } else setPreferredSources(defaultSources); - }, [showSources, sources, dictionaries, initialSearch]); - + } else setCurrentSources(defaultSources); + }, [showAllSources, sources, dictionaries, initialSearch]); + // TODO - Check if this is correct const fetchMoreData = () => { setTimeout(() => { - const previousSources = preferredSources + const previousSources = currentSources ?.filter(Boolean) - .concat(preferredSources.slice(11)); - setPreferredSources(previousSources); + .concat(currentSources.slice(11)); + setCurrentSources(previousSources); }, 1000); }; - const handleSearch = (q: string) => goTo(pathUrl({ q })); - const handleShowSources = (event: React.ChangeEvent) => { - setShowSources(event.target.checked); + setShowAllSources(event.target.checked); }; - - // const handleSearch = (value:String) => { - // const filteredSources=preferredSources?.filter(Boolean).filter(source => source.name===value) - // setPreferredSources(filteredSources) - // } - const classes = useStyles(); const isSourceContainer = containerType === SOURCE_CONTAINER; const isAddToDictionary = isSourceContainer && !!addConceptToDictionary; @@ -145,7 +137,6 @@ const ViewConceptsHeader: React.FC = ({ containerType === DICTIONARY_VERSION_CONTAINER ? "v" : "" }${getContainerIdFromUrl(containerUrl)}`; }; - const showSwitchSourceBasedOnContainerType = () => { return !isAddToDictionary ? null : ( <> @@ -168,14 +159,29 @@ const ViewConceptsHeader: React.FC = ({ }} anchorEl={switchSourceAnchor} keepMounted - open={Boolean(switchSourceAnchor)} + open={!!switchSourceAnchor} onClose={handleSwitchSourceClose} > - -
+ + } + label={ + showAllSources + ? `Showing all Sources` + : `Show all Sources` + } + /> + {showAllSources && { e.preventDefault(); - handleSearch(q); + handleSearch(queryString); }} > = ({ type="search" fullWidth placeholder={"Select an alternative source"} - value={q} - onChange={e => setQ(e.target.value)} + value={queryString} + onChange={e => setQueryString(e.target.value)} endAdornment={ - handleSearch(q)}> - + handleSearch(queryString)}> + } /> - - - } - checked={showSources} - onChange={handleShowSources} - color="primary" - name="displayVerified" - /> - } - label={ - showSources - ? `Showing verified Sources only` - : `Show verified Sources only` - } - /> + }
- {showSources ? ( + {showAllSources ? ( Loading...} endMessage={

end

} > - {preferredSources?.filter(Boolean).map(({ name, url }) => ( + {currentSources?.map(({ name, url }) => ( = ({ ))}
) : ( - preferredSources?.filter(Boolean).map(({ name, url }) => ( + currentSources?.map(({ name, url }) => ( = ({ ); }; - return (
= ({
); }; - export default ViewConceptsHeader;