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

80% Statements 12/15
50% Branches 2/4
75% Functions 3/4
80% Lines 12/15

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                    356x 15x 15x     15x 15x   15x 1x   1x   1x     1x     1x               1x                            
import { ReactElement, useState } from "react";
import { useTranslation } from "react-i18next";
import { Button, ButtonStyle } from "client/components/Button";
import { useAppDispatch } from "client/utils/hooks";
import {
  PostLeaveGroupErrorMessage,
  submitLeaveGroup,
} from "client/views/group/groupThunks";
import { ErrorMessage } from "client/components/ErrorMessage";
 
export const GroupMemberActions = (): ReactElement => {
  const dispatch = useAppDispatch();
  const { t } = useTranslation();
 
  const [serverError, setServerError] =
    useState<PostLeaveGroupErrorMessage | null>(null);
  const [loading, setLoading] = useState<boolean>(false);
 
  const leaveGroup = async (): Promise<void> => {
    setLoading(true);
 
    const errorMessage = await dispatch(submitLeaveGroup());
 
    Iif (errorMessage) {
      setServerError(errorMessage);
    } else {
      setServerError(null);
    }
 
    setLoading(false);
  };
 
  return (
    <>
      <Button
        disabled={loading}
        buttonStyle={ButtonStyle.PRIMARY}
        onClick={async () => await leaveGroup()}
      >
        {t("button.leaveGroup")}
      </Button>
 
      {serverError && (
        <ErrorMessage
          message={t(serverError)}
          closeError={() => setServerError(null)}
        />
      )}
    </>
  );
};