All files / client/src/views/program-item/head/components ProgramItemHeadSignupInfo.tsx

96% Statements 24/25
34.61% Branches 9/26
66.66% Functions 6/9
95.83% Lines 23/24

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                                              356x             443x 1104x   443x   443x       443x       443x       443x     443x                                                           4x                                                             6x 7x                                                 1x           1x 1x     1x       1x 1x     1x 1x     1x                                 1x 409x      
import { ReactElement, useState } from "react";
import styled from "styled-components";
import { useTranslation } from "react-i18next";
import { Link } from "react-router";
import { sortBy } from "remeda";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { getAttendeeType } from "client/utils/getAttendeeType";
import { ProgramItem, UserSignup } from "shared/types/models/programItem";
import { ExpandButton } from "client/components/ExpandButton";
import { SignupQuestion } from "shared/types/models/settings";
import { config } from "shared/config";
import { useAppSelector } from "client/utils/hooks";
import { UserGroup } from "shared/types/models/user";
import { AppRoute } from "client/app/AppRoutes";
 
interface Props {
  isLoggedIn: boolean;
  programItem: ProgramItem;
  signups: UserSignup[];
  isDirectSignupMode: boolean;
  publicSignupQuestion: SignupQuestion | undefined;
}
 
export const ProgramItemHeadSignupInfo = ({
  isLoggedIn,
  programItem,
  signups,
  isDirectSignupMode,
  publicSignupQuestion,
}: Props): ReactElement => {
  const { t, i18n } = useTranslation();
  const useGroup = useAppSelector((state) => state.login.userGroup);
 
  const [isExpanded, setIsExpanded] = useState<boolean>(false);
 
  const attendeesText = t(
    `attendeeTypePluralNominative.${getAttendeeType(programItem.programType)}`,
  );
 
  const showMoreText = t("signup.showAttendees", {
    ATTENDEE_TYPE: attendeesText,
  });
 
  const showLessText = t("signup.hideAttendees", {
    ATTENDEE_TYPE: attendeesText,
  });
 
  const ariaId = `participants-for-${programItem.programItemId}`;
 
  const hideParticipantList =
    !(useGroup === UserGroup.ADMIN || useGroup === UserGroup.HELPER) &&
    config
      .event()
      .hideParticipantListProgramTypes.includes(programItem.programType);
 
  return (
    <SignupsInfoContainer>
      <div>
        {t("signup.signupCount", {
          ATTENDEE_COUNT: String(signups.length),
          MAX_ATTENDANCE: String(programItem.maxAttendance),
        })}
 
        {signups.length < programItem.minAttendance && isDirectSignupMode && (
          <AttendeesNeeded>
            {t("signup.attendeesNeeded", {
              COUNT: String(programItem.minAttendance - signups.length),
            })}
          </AttendeesNeeded>
        )}
      </div>
 
      {!hideParticipantList && (
        <ExpandButton
          isExpanded={isExpanded}
          showMoreText={showMoreText}
          showLessText={showLessText}
          showMoreAriaLabel={showMoreText}
          showLessAriaLabel={showLessText}
          ariaControls={ariaId}
          onClick={() => setIsExpanded(!isExpanded)}
        />
      )}
 
      {isExpanded && signups.length === 0 && (
        <NoAttendeesText>
          {t("signup.noAttendees", {
            ATTENDEE_TYPE: t(
              `attendeeTypePlural.${getAttendeeType(programItem.programType)}`,
            ),
          })}
        </NoAttendeesText>
      )}
 
      {isExpanded && signups.length > 0 && isLoggedIn && (
        <>
          {publicSignupQuestion && (
            <QuestionContainer>
              <FontAwesomeIcon
                aria-label={t("signup.signupQuestionAriaLabel")}
                icon={["far", "comment"]}
              />
              <span>
                {": "}
                {i18n.language === "fi"
                  ? publicSignupQuestion.questionFi
                  : publicSignupQuestion.questionEn}
              </span>
            </QuestionContainer>
          )}
          <AttendeeList data-testid="attendee-list">
            {sortBy(signups, (signup) => signup.username).map((signup) => (
              <li key={signup.username}>
                {signup.username}
                {publicSignupQuestion && (
                  <AnswerText>: {signup.signupMessage}</AnswerText>
                )}
              </li>
            ))}
          </AttendeeList>
        </>
      )}
 
      {isExpanded && signups.length > 0 && !isLoggedIn && (
        <AttendeeText>
          <Link to={AppRoute.LOGIN}>{t("signup.loginLink")}</Link>
          {t("signup.loginLinkEnding", {
            ATTENDEE_TYPE: t(
              `attendeeTypePluralNominative.${getAttendeeType(programItem.programType)}`,
            ),
          })}
        </AttendeeText>
      )}
    </SignupsInfoContainer>
  );
};
 
const SignupsInfoContainer = styled.div`
  display: flex;
  flex-direction: column;
  gap: 8px;
`;
 
const QuestionContainer = styled.div`
  color: ${(props) => props.theme.textSecondary};
`;
 
const AttendeeText = styled.p`
  margin: 4px 0 0 12px;
`;
 
const AnswerText = styled.span`
  color: ${(props) => props.theme.textSecondary};
`;
 
const NoAttendeesText = styled(AttendeeText)`
  color: ${(props) => props.theme.textSecondary};
`;
 
const AttendeeList = styled.ul`
  margin: 0 0 0 12px;
 
  li {
    list-style: none;
  }
 
  /* Add some space between rows to group attendees if there are many of them to show. */
  li:nth-child(10n) {
    padding-bottom: 12px;
  }
 
  li:last-child {
    padding-bottom: 0;
  }
`;
 
const AttendeesNeeded = styled.span`
  color: ${(props) => props.theme.textSecondary};
  padding-left: 8px;
`;