All files / client/src/views/program-item/body ProgramItemBody.tsx

83.33% Statements 10/12
42.85% Branches 3/7
75% Functions 3/4
80% Lines 8/10

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                                356x       559x   1448x 1448x   559x   559x                                                           1x       1x        
import { ReactElement, useState } from "react";
import styled from "styled-components";
import { useTranslation } from "react-i18next";
import { ProgramItem } from "shared/types/models/programItem";
import { ProgramItemDetails } from "client/views/program-item/body/components/ProgramItemDetails";
import { UserGroup } from "shared/types/models/user";
import { AdminActionCard } from "client/views/program-item/body/components/AdminActionCard";
import { useAppSelector } from "client/utils/hooks";
import { ExpandButton } from "client/components/ExpandButton";
import { TextWithLinks } from "client/markdown/components/TextWithLinks";
 
interface Props {
  programItem: ProgramItem;
  isAlwaysExpanded: boolean;
}
 
export const ProgramItemBody = ({
  programItem,
  isAlwaysExpanded,
}: Props): ReactElement => {
  const { t } = useTranslation();
 
  const loggedIn = useAppSelector((state) => state.login.loggedIn);
  const userGroup = useAppSelector((state) => state.login.userGroup);
 
  const [isExpanded, setIsExpanded] = useState<boolean>(isAlwaysExpanded);
 
  const id = `more-info-${programItem.programItemId}`;
 
  return (
    <div>
      <ShortDescription>
        <TextWithLinks>{programItem.shortDescription}</TextWithLinks>
      </ShortDescription>
      {!isAlwaysExpanded && (
        <ExpandButton
          isExpanded={isExpanded}
          showMoreText={t("programItemInfo.showMore")}
          showLessText={t("programItemInfo.showLess")}
          showMoreAriaLabel={`${t("programItemInfo.showMoreAriaLabel")} ${programItem.title}`}
          showLessAriaLabel={`${t("programItemInfo.showLessAriaLabel")} ${programItem.title}`}
          ariaControls={id}
          onClick={() => setIsExpanded(!isExpanded)}
        />
      )}
      {isExpanded && (
        <ExpandedDescriptionContainer id={id}>
          <ProgramItemDetails programItem={programItem} />
          {loggedIn && userGroup === UserGroup.ADMIN && (
            <AdminActionCard programItem={programItem} />
          )}
        </ExpandedDescriptionContainer>
      )}
    </div>
  );
};
 
const ExpandedDescriptionContainer = styled.div`
  padding-top: 8px;
`;
 
const ShortDescription = styled.p`
  overflow-wrap: break-word;
  margin: 8px 0 8px 0;
`;