All files / client/src/views/group/components NotInGroupActions.tsx

93.18% Statements 41/44
50% Branches 7/14
91.66% Functions 11/12
93.18% Lines 41/44

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                                      356x 132x 132x   132x 132x 132x 132x 132x       132x 15x 15x 15x     132x 15x 15x 15x     132x 14x   14x   14x     14x 14x     14x     132x 13x   13x       12x 1x   11x 11x     12x     132x     15x                 15x               15x                     14x                                             13x                                 1x       1x       1x 55x        
import { ChangeEvent, ReactElement, useState } from "react";
import styled from "styled-components";
import { useTranslation } from "react-i18next";
import { Button, ButtonStyle } from "client/components/Button";
import {
  PostCreateGroupErrorMessage,
  PostJoinGroupErrorMessage,
  submitCreateGroup,
  submitJoinGroup,
} from "client/views/group/groupThunks";
import { useAppDispatch } from "client/utils/hooks";
import { ErrorMessage } from "client/components/ErrorMessage";
import { ButtonGroup } from "client/components/ButtonGroup";
import { ControlledInput } from "client/components/ControlledInput";
 
interface Props {
  disabled: boolean;
}
 
export const NotInGroupActions = ({ disabled }: Props): ReactElement => {
  const dispatch = useAppDispatch();
  const { t } = useTranslation();
 
  const [loading, setLoading] = useState<boolean>(false);
  const [showCreateGroup, setShowCreateGroup] = useState<boolean>(false);
  const [showJoinGroup, setShowJoinGroup] = useState<boolean>(false);
  const [joinGroupValue, setJoinGroupValue] = useState<string>("");
  const [serverError, setServerError] = useState<
    PostJoinGroupErrorMessage | PostCreateGroupErrorMessage | null
  >(null);
 
  const openCreateGroup = (): void => {
    setServerError(null);
    setShowCreateGroup(true);
    setShowJoinGroup(false);
  };
 
  const openJoinGroup = (): void => {
    setServerError(null);
    setShowJoinGroup(true);
    setShowCreateGroup(false);
  };
 
  const createGroup = async (): Promise<void> => {
    setLoading(true);
 
    const errorMessage = await dispatch(submitCreateGroup());
 
    Iif (errorMessage) {
      setServerError(errorMessage);
    } else {
      setServerError(null);
      setShowCreateGroup(false);
    }
 
    setLoading(false);
  };
 
  const joinGroup = async (): Promise<void> => {
    setLoading(true);
 
    const errorMessage = await dispatch(
      submitJoinGroup({ groupCode: joinGroupValue.trim() }),
    );
 
    if (errorMessage) {
      setServerError(errorMessage);
    } else {
      setServerError(null);
      setShowCreateGroup(false);
    }
 
    setLoading(false);
  };
 
  const handleJoinGroupChange = (
    event: ChangeEvent<HTMLInputElement>,
  ): void => {
    setJoinGroupValue(event.target.value);
  };
 
  return (
    <>
      <ButtonGroup>
        <Button
          disabled={showCreateGroup || disabled}
          buttonStyle={ButtonStyle.PRIMARY}
          onClick={() => openCreateGroup()}
        >
          {t("button.createGroup")}
        </Button>
 
        <Button
          disabled={showJoinGroup || disabled}
          buttonStyle={ButtonStyle.PRIMARY}
          onClick={() => openJoinGroup()}
        >
          {t("button.joinGroup")}
        </Button>
      </ButtonGroup>
      {showCreateGroup && (
        <>
          <p>{t("group.createGroupConfirmationMessage")}</p>
          <Button
            disabled={loading}
            buttonStyle={ButtonStyle.PRIMARY}
            onClick={async () => await createGroup()}
          >
            {t("button.createGroupConfirmation")}
          </Button>
        </>
      )}
 
      {showJoinGroup && (
        <>
          <InfoTextParagraph>
            {t("group.joiningGroupWillCancelUpcomingLotterySignups")}
          </InfoTextParagraph>
 
          <StyledInput
            key="joinGroup"
            placeholder={t("group.enterGroupCreatorCode")}
            value={joinGroupValue}
            onChange={handleJoinGroupChange}
          />
 
          <ButtonWithMargin
            disabled={loading}
            buttonStyle={ButtonStyle.PRIMARY}
            onClick={async () => await joinGroup()}
          >
            {t("button.joinGroup")}
          </ButtonWithMargin>
        </>
      )}
 
      {serverError && (
        <ErrorMessage
          message={t(serverError)}
          closeError={() => setServerError(null)}
        />
      )}
    </>
  );
};
 
const ButtonWithMargin = styled(Button)`
  margin-top: 8px;
`;
 
const InfoTextParagraph = styled.p`
  font-weight: 600;
`;
 
const StyledInput = styled(ControlledInput)`
  @media (min-width: ${(props) => props.theme.breakpointPhone}) {
    max-width: 500px;
  }
`;