Skip to content

Commit

Permalink
new challenge filter enchancement added for host app
Browse files Browse the repository at this point in the history
  • Loading branch information
vihangckale committed Jul 17, 2024
1 parent f8a40ee commit b00f415
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 3 deletions.
23 changes: 23 additions & 0 deletions apps/host/src/components/common/checkbox/checkbox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
interface Props {
checked: boolean;
setNewChallenge: React.Dispatch<React.SetStateAction<boolean>>;
label: string;
className: string;
containerClass: string;
}
const CustomCheckbox = ({ checked, setNewChallenge, label, className, containerClass }: Props) => {
return (
<div className={containerClass}>
<input
className={className}
type="checkbox"
id="checkbox"
name="checkbox"
onChange={(e) => setNewChallenge(e.target.checked)}
checked={checked}
/>
<label htmlFor="checkbox">{label}</label>
</div>
);
};
export default CustomCheckbox;
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { contributors } from '@fmc/data/content';
import { ETag, OptionType } from '@fmc/data/types';
import { Difficulties } from '@fmc/data/constants';
import { Search } from 'lucide-react';
import CustomCheckbox from '@/components/common/checkbox/checkbox';

interface Props {
searchInput: string;
Expand All @@ -18,6 +19,8 @@ interface Props {
setSelectedChallengesByTags: React.Dispatch<React.SetStateAction<ETag[] | []>>;
isSegmentBtn1: boolean;
setIsSegmentBtn1: React.Dispatch<React.SetStateAction<boolean>>;
setNewChallenge: React.Dispatch<React.SetStateAction<boolean>>;
newChallenge: boolean;
}

const ChallengeFilters = ({
Expand All @@ -31,6 +34,8 @@ const ChallengeFilters = ({
setSelectedChallengesByTags,
isSegmentBtn1,
setIsSegmentBtn1,
setNewChallenge,
newChallenge,
}: Props) => {
const DeveloperList = useMemo(() => {
const developerList = new Map();
Expand Down Expand Up @@ -72,6 +77,13 @@ const ChallengeFilters = ({
optionSelected={selectedDifficulties}
setOptionSelected={(val: OptionType[]) => setSelectedDifficulties(val)}
/>
<CustomCheckbox
className={styles.checkbox}
checked={newChallenge}
setNewChallenge={setNewChallenge}
label="New Challenges"
containerClass={styles.checkboxContainer}
/>
<div className={styles.customSegment}>
<button
data-active={isSegmentBtn1 ? true : false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,18 @@
cursor: pointer;
}
}
.checkboxContainer {
width: fit-content;
display: flex;
align-items: center;
column-gap: 0.3rem;
.checkbox {
width: 20px;
height: 20px;
cursor: pointer;
}
label {
cursor: pointer;
}
cursor: pointer;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ function ChallengeGrid({ challenges, linkPrefix, links }: Props) {
const [selectedDifficulties, setSelectedDifficulties] = useState<OptionType[]>([]);
const [selectedChallengesByTags, setSelectedChallengesByTags] = useState<ETag[]>([]);
const [isSegmentBtn1, setIsSegmentBtn1] = useState(false);
const [newChallenge, setNewChallenge] = useState<boolean>(false);

useEffect(() => {
setFilteredChallenges(() =>
Expand All @@ -28,10 +29,11 @@ function ChallengeGrid({ challenges, linkPrefix, links }: Props) {
contributors: optionSelected,
difficulties: selectedDifficulties,
tags: selectedChallengesByTags, // Convert OptionType[] to ETag[]
newChallenge,
})
);

if (!searchInput && !optionSelected && !selectedDifficulties) {
if (!searchInput && !optionSelected && !selectedDifficulties && !newChallenge) {
setFilteredChallenges(challenges);
}
}, [
Expand All @@ -41,6 +43,7 @@ function ChallengeGrid({ challenges, linkPrefix, links }: Props) {
selectedDifficulties,
isSegmentBtn1,
selectedChallengesByTags,
newChallenge,
]);
return (
<div className={styles.container}>
Expand All @@ -55,6 +58,8 @@ function ChallengeGrid({ challenges, linkPrefix, links }: Props) {
setSelectedChallengesByTags={setSelectedChallengesByTags}
isSegmentBtn1={isSegmentBtn1}
setIsSegmentBtn1={setIsSegmentBtn1}
newChallenge={newChallenge}
setNewChallenge={setNewChallenge}
/>

{filteredChallenges.length ? (
Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions shared/data/types/challenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ export interface IGetChallengesByid {
contributors: OptionType[];
difficulties: OptionType[];
tags: ETag[] | [];
newChallenge: boolean;
}
11 changes: 9 additions & 2 deletions shared/data/utils/challenges.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,25 @@ export function getChallengesByTags(challenges: IChallenge[], tags: ETag[], isRe
return tags.some((tag: ETag) => (challenge.tags as ETag[])?.includes(tag));
});
}

export function getChallengesByNewChallenge(challenges: IChallenge[], newChallenge: boolean) {
if (!newChallenge) return challenges;
return challenges.filter(({ isNew }) => isNew);
}
export function getChallengesByid({
challenges,
title,
contributors,
difficulties,
tags,
newChallenge,
}: IGetChallengesByid) {
const isResetTags = !tags || tags.length === 0 || (tags?.length == 1 && tags[0] == ETag.all);
if (
(!title || title.length === 0) &&
(!contributors || contributors.length === 0) &&
(!difficulties || difficulties.length === 0) &&
isResetTags
isResetTags &&
!newChallenge
) {
return challenges;
}
Expand All @@ -102,7 +107,9 @@ export function getChallengesByid({
filteredChallenges = getChallengesByContributors(filteredChallenges, contributors);

filteredChallenges = getChallengesByDifficulties(filteredChallenges, difficulties);

filteredChallenges = getChallengesByTags(filteredChallenges, tags, isResetTags);

filteredChallenges = getChallengesByNewChallenge(filteredChallenges, newChallenge);
return filteredChallenges;
}

0 comments on commit b00f415

Please sign in to comment.