All files / client/src/views/all-program-items/components SearchAndFilterCard.tsx

78.04% Statements 32/41
41.66% Branches 5/12
88.23% Functions 15/17
78.04% Lines 32/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227                                                                              520x                   1564x 1564x 1564x 2483x             1564x   1564x 1564x 1564x   1564x 1341x       1169x       1803x           1564x       1564x                       1564x                                                                   4692x         3128x             9x 9x                                 12x                                 6x 6x 6x                                   1x           1542x         1x   777x     1x   392x     1x         1x      
import { Dispatch, ReactElement, SetStateAction } from "react";
import { useTranslation } from "react-i18next";
import styled from "styled-components";
import { config } from "shared/config";
import {
  AgeGroup,
  Language,
  ProgramType,
  Tag,
} from "shared/types/models/programItem";
import { Checkbox } from "client/components/Checkbox";
import { ControlledInput } from "client/components/ControlledInput";
import { MultiSelectDropdown } from "client/components/MultiSelectDropdown";
import { ProgramTypeSelection } from "client/components/ProgramTypeSelection";
import { RadioButton } from "client/components/RadioButton";
import { RadioButtonGroup } from "client/components/RadioButtonGroup";
import { RaisedCard } from "client/components/RaisedCard";
import { useAppSelector } from "client/utils/hooks";
import { SessionStorageValue } from "client/utils/sessionStorage";
import { selectProgramTypeForTexts } from "client/views/admin/adminSlice";
import {
  selectAgeGroups,
  selectLanguages,
  selectTags,
} from "client/views/all-program-items/allProgramItemsSlice";
import { RevolvingDoorProgramItemsInfo } from "client/views/all-program-items/components/RevolvingDoorProgramItemsInfo";
import { StartingTimeOption } from "client/views/all-program-items/programListUtils";
 
interface Props {
  selectedTags: (Tag | Language | AgeGroup)[];
  setSelectedTags: Dispatch<SetStateAction<(Tag | Language | AgeGroup)[]>>;
  selectedStartingTime: StartingTimeOption;
  setSelectedStartingTime: Dispatch<SetStateAction<StartingTimeOption>>;
  searchTerm: string;
  setSearchTerm: Dispatch<SetStateAction<string>>;
  hideFullItems: boolean;
  setHideFullItems: Dispatch<SetStateAction<boolean>>;
}
 
export const SearchAndFilterCard = ({
  selectedTags,
  setSelectedTags,
  selectedStartingTime,
  setSelectedStartingTime,
  searchTerm,
  setSearchTerm,
  hideFullItems,
  setHideFullItems,
}: Props): ReactElement => {
  const { t } = useTranslation();
  const programTypeForTexts = useAppSelector(selectProgramTypeForTexts);
  const activeProgramTypes = useAppSelector(
    (state) => state.admin.activeProgramTypes,
  );
 
  // The search matches the game system field, so advertise it in the
  // placeholder whenever tabletop RPGs are among the listed program types
  // (an empty selection means all program types)
  const searchIncludesRpgs =
    activeProgramTypes.length === 0 ||
    activeProgramTypes.includes(ProgramType.TABLETOP_RPG);
  const tagFilters = useAppSelector(selectTags);
  const ageGroupFilters = useAppSelector(selectAgeGroups);
  const languageFilters = useAppSelector(selectLanguages);
 
  const tagOptions = [
    languageFilters.map((filter) => ({
      value: filter,
      title: t(`programItemLanguage.${filter}`),
    })),
    tagFilters.map((filter) => ({
      value: filter,
      title: t(`tags.${filter}`),
    })),
    ageGroupFilters.map((filter) => ({
      value: filter,
      title: t(`ageGroup.${filter}`),
    })),
  ].flat();
 
  const allProgramItemsLabel = t("allProgramItems", {
    PROGRAM_TYPE: t(`programTypePlural.${programTypeForTexts}`),
  });
 
  const toggleTag = (value: string): void => {
    const tag = value as Tag | Language | AgeGroup;
    const nextTags = selectedTags.includes(tag)
      ? selectedTags.filter((selected) => selected !== tag)
      : [...selectedTags, tag];
    setSelectedTags(nextTags);
    sessionStorage.setItem(
      SessionStorageValue.ALL_PROGRAM_ITEMS_TAG,
      JSON.stringify(nextTags),
    );
  };
 
  const clearTags = (): void => {
    setSelectedTags([]);
    sessionStorage.removeItem(SessionStorageValue.ALL_PROGRAM_ITEMS_TAG);
  };
 
  return (
    <Container>
      <InputContainer>
        <StyledLabel htmlFor="programTypeSelection">
          {t("selectedProgramType")}
        </StyledLabel>
        <ProgramTypeSelection id="programTypeSelection" />
      </InputContainer>
 
      {config.event().enableTagDropdown && (
        <InputContainer>
          <StyledLabel htmlFor="tagSelection">{t("chooseTag")}</StyledLabel>
          <MultiSelectDropdown
            id="tagSelection"
            options={tagOptions}
            selectedValues={selectedTags}
            onToggle={toggleTag}
            onClear={clearTags}
            placeholder={allProgramItemsLabel}
            testId="tag-filter"
          />
        </InputContainer>
      )}
 
      <InputContainer>
        <StyledLegend>{t("startingTime")}</StyledLegend>
        <RadioButtonGroup>
          {Object.entries(StartingTimeOption)
            .filter(([_, val]) =>
              config.event().enableRevolvingDoor
                ? true
                : val !== StartingTimeOption.REVOLVING_DOOR,
            )
            .map(([key, val]) => {
              return (
                <RadioButton
                  checked={selectedStartingTime === val}
                  key={key}
                  id={key}
                  label={t(val)}
                  onChange={() => {
                    setSelectedStartingTime(val);
                    sessionStorage.setItem(
                      SessionStorageValue.ALL_PROGRAM_ITEMS_STARTING_TIME,
                      val,
                    );
                  }}
                />
              );
            })}
        </RadioButtonGroup>
      </InputContainer>
 
      <InputContainer>
        <StyledLabel htmlFor="find">{t("find")}</StyledLabel>
        <ControlledInput
          id="find"
          value={searchTerm}
          onChange={(event) => {
            setSearchTerm(event.target.value);
          }}
          placeholder={t(
            searchIncludesRpgs ? "searchWithTitleOrSystem" : "searchWithTitle",
            {
              PROGRAM_TYPE: t(`programTypeGenetive.${programTypeForTexts}`),
            },
          )}
          resetValue={() => {
            setSearchTerm("");
          }}
        />
      </InputContainer>
      <InputContainer>
        <Checkbox
          checked={hideFullItems}
          onChange={() => {
            const nextValue = !hideFullItems;
            setHideFullItems(nextValue);
            sessionStorage.setItem(
              SessionStorageValue.ALL_PROGRAM_ITEMS_HIDE_FULL,
              JSON.stringify(nextValue),
            );
          }}
          id="hide-full-items"
          label={t("hideFullItems")}
        />
      </InputContainer>
      {selectedStartingTime === StartingTimeOption.REVOLVING_DOOR && (
        <RevolvingDoorProgramItemsInfoContainer>
          <RevolvingDoorProgramItemsInfo />
        </RevolvingDoorProgramItemsInfoContainer>
      )}
    </Container>
  );
};
 
const Container = styled(RaisedCard)`
  display: grid;
  row-gap: 16px;
  column-gap: 24px;
  grid-template-columns: 1fr 1fr;
 
  @media (max-width: ${(props) => props.theme.breakpointPhone}) {
    grid-template-columns: 1fr;
  }
`;
 
const StyledLabel = styled.label`
  padding: 0 0 2px 4px;
  font-size: ${(props) => props.theme.fontSizeSmall};
`;
 
const StyledLegend = styled.legend`
  padding: 0 0 2px 4px;
  font-size: ${(props) => props.theme.fontSizeSmall};
`;
 
const InputContainer = styled.div`
  display: flex;
  flex-direction: column;
`;
 
const RevolvingDoorProgramItemsInfoContainer = styled.div`
  grid-column: 1/-1;
`;