-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Only fetch tags when dropdowns are opened
- Loading branch information
1 parent
f8cbc91
commit 811c874
Showing
4 changed files
with
132 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 86 additions & 43 deletions
129
src/content-tags-drawer/ContentTagsDropDownSelector.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,112 @@ | ||
import React from 'react'; | ||
import React, { useState, useEffect } from 'react'; | ||
import { | ||
SelectableBox, | ||
Icon, | ||
useToggle, | ||
Spinner, | ||
} from '@edx/paragon'; | ||
import { useIntl } from '@edx/frontend-platform/i18n'; | ||
import { ArrowDropDown, ArrowDropUp } from '@edx/paragon/icons'; | ||
import PropTypes from 'prop-types'; | ||
import messages from './messages'; | ||
import './ContentTagsDropDownSelector.scss'; | ||
|
||
import { useTaxonomyTagsDataResponse, useIsTaxonomyTagsDataLoaded } from './api/hooks/selectors'; | ||
|
||
const ContentTagsDropDownSelector = ({ | ||
taxonomyId, taxonomyTag, level, useTaxonomyTagsData, | ||
taxonomyId, level, subTagsUrl, | ||
}) => { | ||
const [isOpen, open, close] = useToggle(false); | ||
const intl = useIntl(); | ||
// This array represents the states of the dropdowns on this level | ||
// Each index contains whether the dropdown is open/closed | ||
// O === Closed | ||
// 1 === Open | ||
const [dropdownStates, setDropdownStates] = useState([]); | ||
|
||
const isOpen = (i) => (dropdownStates[i] === 1); | ||
|
||
const clickAndEnterHandler = (index) => { | ||
const updatedState = dropdownStates.map((dropdownState, i) => { | ||
if (index === i) { | ||
return dropdownState === 0 ? 1 : 0; | ||
} | ||
return dropdownState; | ||
}); | ||
|
||
setDropdownStates(updatedState); | ||
}; | ||
|
||
const useTaxonomyTagsData = (id, fullPathProvided) => { | ||
const taxonomyTagsData = useTaxonomyTagsDataResponse(id, fullPathProvided); | ||
const isTaxonomyTagsLoaded = useIsTaxonomyTagsDataLoaded(id, fullPathProvided); | ||
return { taxonomyTagsData, isTaxonomyTagsLoaded }; | ||
}; | ||
|
||
const clickAndEnterHandler = () => (isOpen ? close() : open()); | ||
const { taxonomyTagsData, isTaxonomyTagsLoaded } = useTaxonomyTagsData(taxonomyId, subTagsUrl); | ||
|
||
const { taxonomyTagsData, isTaxonomyTagsLoaded } = useTaxonomyTagsData(taxonomyId, taxonomyTag.subTagsUrl); | ||
useEffect(() => { | ||
if (isTaxonomyTagsLoaded && taxonomyTagsData) { | ||
// When the data first loads, all the dropdowns are initially closed | ||
setDropdownStates(new Array(taxonomyTagsData.results.length).fill(0)); | ||
} | ||
}, [isTaxonomyTagsLoaded, taxonomyTagsData]); | ||
|
||
return ( | ||
<div style={{ display: 'flex', flexDirection: 'column', paddingLeft: `${level * 1}rem` }}> | ||
<div style={{ display: 'flex' }}> | ||
<SelectableBox | ||
inputHidden={false} | ||
type="checkbox" | ||
className="pgn__selectable_box taxonomy-tags-selectable-box" | ||
aria-label={`${taxonomyTag.value} checkbox`} | ||
> | ||
{taxonomyTag.value} | ||
</SelectableBox> | ||
{ taxonomyTag.subTagsUrl | ||
&& ( | ||
<div className="taxonomy-tags-arrow-drop-down" data-link={taxonomyTag.subTagsUrl}> | ||
<Icon | ||
src={isOpen ? ArrowDropUp : ArrowDropDown} | ||
onClick={clickAndEnterHandler} | ||
tabIndex="0" | ||
onKeyPress={(event) => (event.key === 'Enter' ? clickAndEnterHandler() : null)} | ||
/> | ||
</div> | ||
isTaxonomyTagsLoaded && taxonomyTagsData | ||
? taxonomyTagsData.results.map((taxonomyTag, i) => ( | ||
<div style={{ display: 'flex', flexDirection: 'column', paddingLeft: `${level * 1}rem` }} key={`selector-div-${taxonomyTag.value}`}> | ||
<div style={{ display: 'flex' }}> | ||
<SelectableBox | ||
inputHidden={false} | ||
type="checkbox" | ||
className="pgn__selectable_box taxonomy-tags-selectable-box" | ||
aria-label={`${taxonomyTag.value} checkbox`} | ||
> | ||
{taxonomyTag.value} | ||
</SelectableBox> | ||
{ taxonomyTag.subTagsUrl | ||
&& ( | ||
<div className="taxonomy-tags-arrow-drop-down" data-link={taxonomyTag.subTagsUrl}> | ||
<Icon | ||
src={isOpen(i) ? ArrowDropUp : ArrowDropDown} | ||
onClick={() => clickAndEnterHandler(i)} | ||
tabIndex="0" | ||
onKeyPress={(event) => (event.key === 'Enter' ? clickAndEnterHandler(i) : null)} | ||
/> | ||
</div> | ||
)} | ||
</div> | ||
|
||
{ taxonomyTag.subTagsUrl && isOpen(i) && ( | ||
<ContentTagsDropDownSelector | ||
key={`selector-${taxonomyTag.value}`} | ||
taxonomyId={taxonomyId} | ||
subTagsUrl={taxonomyTag.subTagsUrl} | ||
level={level + 1} | ||
/> | ||
)} | ||
</div> | ||
|
||
{ taxonomyTag.subTagsUrl && isOpen && isTaxonomyTagsLoaded && taxonomyTagsData | ||
&& taxonomyTagsData.results.map((childTag) => ( | ||
<ContentTagsDropDownSelector | ||
key={`selector-${childTag.value}`} | ||
taxonomyId={taxonomyId} | ||
taxonomyTag={childTag} | ||
level={level + 1} | ||
useTaxonomyTagsData={useTaxonomyTagsData} | ||
</div> | ||
)) | ||
: ( | ||
<div className="d-flex justify-content-center align-items-center flex-column"> | ||
<Spinner | ||
animation="border" | ||
size="xl" | ||
screenReaderText={intl.formatMessage(messages.loadingTagsDropdownMessage)} | ||
/> | ||
))} | ||
|
||
</div> | ||
</div> | ||
) | ||
); | ||
}; | ||
|
||
ContentTagsDropDownSelector.defaultProps = { | ||
subTagsUrl: undefined, | ||
}; | ||
|
||
ContentTagsDropDownSelector.propTypes = { | ||
taxonomyId: PropTypes.number.isRequired, | ||
taxonomyTag: PropTypes.shape({ | ||
value: PropTypes.string, | ||
subTagsUrl: PropTypes.string, | ||
}).isRequired, | ||
level: PropTypes.number.isRequired, | ||
useTaxonomyTagsData: PropTypes.func.isRequired, | ||
subTagsUrl: PropTypes.string, | ||
}; | ||
|
||
export default ContentTagsDropDownSelector; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters