All files / client/src/views/helper/components PasswordManagement.tsx

79.31% Statements 23/29
54.54% Branches 6/11
100% Functions 5/5
79.31% Lines 23/29

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                      356x 26x   26x 26x   26x 26x       26x 9x       9x   9x 3x                   3x         3x 3x           6x             6x 6x     26x 9x                                                                                         1x   17x   3x       1x      
import { ReactElement, ChangeEvent, useState } from "react";
import { useTranslation } from "react-i18next";
import styled, { css } from "styled-components";
import { Link } from "react-router";
import { getUserBySerialOrUsername } from "client/services/userServices";
import { Button, ButtonStyle } from "client/components/Button";
import { ChangeUserSettingsForm } from "client/views/helper/components/ChangeUserSettingsForm";
import { ControlledInput } from "client/components/ControlledInput";
import { getDateAndTime } from "shared/utils/timeFormatter";
import { exhaustiveSwitchGuard } from "shared/utils/exhaustiveSwitchGuard";
 
export const PasswordManagement = (): ReactElement => {
  const { t } = useTranslation();
 
  const [usernameToUpdate, setUsernameToUpdate] = useState<string>("");
  const [userSerialInput, setUserSerialInput] = useState<string>("");
  const [changePasswordInputVisible, setChangePasswordInputVisible] =
    useState<boolean>(false);
  const [userFoundMessage, setUserFoundMessage] = useState<ReactElement>(
    <Message />,
  );
 
  const submitGetUser = async (): Promise<void> => {
    Iif (userSerialInput.length === 0) {
      return;
    }
 
    const response = await getUserBySerialOrUsername(userSerialInput);
 
    if (response.status === "error") {
      switch (response.errorId) {
        case "kompassiLogin":
          setUserFoundMessage(
            <Message error={true}>
              {t("passwordManagement.kompassiUserError")}
            </Message>,
          );
          setChangePasswordInputVisible(false);
          return;
        case "unknown":
          setUserFoundMessage(
            <Message error={true}>
              {t("passwordManagement.userNotFound")}
            </Message>,
          );
          setChangePasswordInputVisible(false);
          return;
        default:
          return exhaustiveSwitchGuard(response.errorId);
      }
    }
 
    setUserFoundMessage(
      <Message>
        {t("passwordManagement.foundUser")}: {response.username} (
        {response.serial}) - {t("passwordManagement.userCreatedAt")}{" "}
        {getDateAndTime(response.createdAt)}
      </Message>,
    );
    setUsernameToUpdate(response.username);
    setChangePasswordInputVisible(true);
  };
 
  const handleSerialChange = (event: ChangeEvent<HTMLInputElement>): void => {
    setUserSerialInput(event.target.value);
  };
 
  return (
    <div>
      <h3>{t("passwordManagement.helperPasswordManagement")}</h3>
      <p>
        {t("passwordManagement.kompassiLocalInfo")}{" "}
        <Link to={"https://kompassi.eu/profile/password/reset"} target="_blank">
          {t("passwordManagement.kompassiPasswordReset")}
        </Link>
        .
      </p>
      <p>{t("passwordManagement.registrationCodeOrUsername")}</p>
 
      <ControlledInput
        key="user-serial"
        placeholder={t("passwordManagement.registrationCodeOrUsername")}
        value={userSerialInput}
        onChange={handleSerialChange}
      />
 
      <ButtonWithMargin
        onClick={submitGetUser}
        buttonStyle={ButtonStyle.PRIMARY}
      >
        {t("button.find")}
      </ButtonWithMargin>
 
      {userFoundMessage}
 
      {changePasswordInputVisible && (
        <ChangeUserSettingsForm
          usernameToUpdate={usernameToUpdate}
          isLocalLogin={true}
        />
      )}
    </div>
  );
};
 
interface MessageProps {
  error?: boolean;
}
 
const Message = styled.p<MessageProps>`
  ${(messageProps) =>
    messageProps.error &&
    css`
      color: ${(props) => props.theme.textError};
    `};
`;
 
const ButtonWithMargin = styled(Button)`
  margin-top: 8px;
`;