Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

start with all Sub Communities selected by default #89

Open
wants to merge 2 commits into
base: bugbash
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/components/Checkbox/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import iconCheckM from "assets/icons/checkmark-medium.png";
import iconCheckS from "assets/icons/checkmark-small.png";

function Checkbox({ checked, onChange, size, errorMsg }) {
const [changeCount, setChangeCount] = useState(0);
const [checkedInternal, setCheckedInternal] = useState(checked);
let sizeStyle = size === "lg" ? "lgSize" : null;
const imgSrc =
Expand All @@ -19,12 +20,15 @@ function Checkbox({ checked, onChange, size, errorMsg }) {
sizeStyle = size === "xs" ? "xsSize" : "smSize";
}
const delayedOnChange = useRef(
_.debounce((q, cb) => cb(q), process.env.GUIKIT.DEBOUNCE_ON_CHANGE_TIME) // eslint-disable-line no-undef
_.debounce((q, cb) => {
cb(q);
setChangeCount((n) => n + 1);
}, process.env.GUIKIT.DEBOUNCE_ON_CHANGE_TIME) // eslint-disable-line no-undef
).current;

useEffect(() => {
setCheckedInternal(checked);
}, [checked]);
}, [changeCount, checked]);

return (
<label styleName={`container ${sizeStyle}`}>
Expand Down
54 changes: 33 additions & 21 deletions src/containers/Filter/ChallengeFilter/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ const ChallengeFilter = ({
<span key={subCommunity.communityName}>
<Checkbox
checked={
// select all if query filters neither events nor groups
events.length + groups.length === 0 ||
events.includes(
utils.challenge.getCommunityEvent(subCommunity)
) ||
Expand All @@ -225,32 +227,42 @@ const ChallengeFilter = ({
const isTCOEvent = utils.challenge.isTCOEventCommunity(
subCommunity
);
let filterChange;
const filterChange = { events, groups };

if (events.length + groups.length === 0) {
// select all if query filters neither events nor groups
filterChange.events = challengeSubCommunities
.filter(utils.challenge.isTCOEventCommunity)
.map(utils.challenge.getCommunityEvent);
filterChange.groups = challengeSubCommunities
.filter(utils.challenge.isGroupCommunity)
.map(utils.challenge.getCommunityGroup);
}

if (isTCOEvent) {
const newEvents = checked
? events.concat(
utils.challenge.getCommunityEvent(subCommunity)
)
: events.filter(
(event) =>
event !==
utils.challenge.getCommunityEvent(subCommunity)
);
filterChange = { events: newEvents };
const scEvent = utils.challenge.getCommunityEvent(
subCommunity
);
filterChange.events = checked
? _.union(filterChange.events, [scEvent])
: _.without(filterChange.events, scEvent);
} else {
const newGroups = checked
? groups.concat(
utils.challenge.getCommunityGroup(subCommunity)
)
: groups.filter(
(group) =>
group !==
utils.challenge.getCommunityGroup(subCommunity)
);
filterChange = { groups: newGroups };
const scGroup = utils.challenge.getCommunityGroup(
subCommunity
);
filterChange.groups = checked
? _.union(filterChange.groups, [scGroup])
: _.without(filterChange.groups, scGroup);
}

// clear community filters if all sub-communities are selected
if (
filterChange.groups.length + filterChange.events.length >=
challengeSubCommunities.length
) {
filterChange.events = [];
filterChange.groups = [];
}
updateFilter(filterChange);
}}
/>
Expand Down