diff --git a/packages/login/src/components/ActivateUserForm/index.tsx b/packages/login/src/components/ActivateUserForm/index.tsx new file mode 100644 index 0000000000..d7bb2e368b --- /dev/null +++ b/packages/login/src/components/ActivateUserForm/index.tsx @@ -0,0 +1,295 @@ +// (c) Copyright Ascensio System SIA 2009-2024 +// +// This program is a free software product. +// You can redistribute it and/or modify it under the terms +// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software +// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended +// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of +// any third-party rights. +// +// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty +// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see +// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html +// +// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021. +// +// The interactive user interfaces in modified source and object code versions of the Program must +// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3. +// +// Pursuant to Section 7(b) of the License you must retain the original Product logo when +// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under +// trademark law for use of our trademarks. +// +// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing +// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0 +// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode + +"use client"; + +import { useContext, useState, KeyboardEvent, ChangeEvent } from "react"; +import { useTranslation } from "react-i18next"; + +import { FieldContainer } from "@docspace/shared/components/field-container"; +import { + InputSize, + InputType, + TextInput, +} from "@docspace/shared/components/text-input"; +import { toastr } from "@docspace/shared/components/toast"; +import { EmployeeActivationStatus } from "@docspace/shared/enums"; +import { TPasswordHash } from "@docspace/shared/api/settings/types"; +import { createPasswordHash } from "@docspace/shared/utils/common"; +import { PasswordInput } from "@docspace/shared/components/password-input"; +import { ALLOWED_PASSWORD_CHARACTERS } from "@docspace/shared/constants"; +import { Button, ButtonSize } from "@docspace/shared/components/button"; +import { login } from "@docspace/shared/utils/loginUtils"; + +import withLoader from "@/HOCs/withLoader"; +import { TActivateConfirmUser, TError, WithLoaderProps } from "@/types"; +import { + changePassword, + updateActivationStatus, + updateUser, +} from "@/utils/actions"; + +import { RegisterContainer } from "../CreateUserForm/CreateUserForm.styled"; +import { GreetingUserContainer } from "../CreateUserForm/sub-components/GreetingUserContainer"; +import { ConfirmRouteContext } from "../ConfirmRoute"; + +type ActivateUserFormPorps = { + passwordHash: TPasswordHash; + defaultPage?: string; +} & WithLoaderProps; + +const ActivateUserForm = ({ + passwordSettings, + passwordHash, + defaultPage = "/", +}: ActivateUserFormPorps) => { + const { t } = useTranslation(["Confirm", "Common"]); + const { linkData } = useContext(ConfirmRouteContext); + + const emailFromLink = linkData?.email ? linkData.email : ""; + + const [name, setName] = useState(linkData.firstname ?? ""); + const [nameValid, setNameValid] = useState(true); + const [surName, setSurName] = useState(linkData.lastname ?? ""); + const [surNameValid, setSurNameValid] = useState(true); + const [password, setPassword] = useState(""); + const [passwordValid, setPasswordValid] = useState(true); + const [isPasswordErrorShow, setIsPasswordErrorShow] = useState(false); + const [isLoading, setIsLoading] = useState(false); + + const onChangeName = (e: ChangeEvent) => { + setName(e.target.value); + setNameValid(true); + }; + + const onChangeSurName = (e: ChangeEvent) => { + setSurName(e.target.value); + setSurNameValid(true); + }; + + const onChangePassword = (e: ChangeEvent) => { + setPassword(e.target.value); + }; + + const onValidatePassword = (progressScore: boolean) => { + setPasswordValid(progressScore); + }; + + const onBlurPassword = () => { + setIsPasswordErrorShow(true); + }; + + const onSubmit = async () => { + setIsLoading(true); + if (!name?.trim()) setNameValid(false); + if (!surName?.trim()) setSurNameValid(false); + if (!password.trim()) { + setPasswordValid(false); + setIsPasswordErrorShow(true); + } + + if (!nameValid || !surNameValid || !password.trim() || !passwordValid) { + setIsLoading(false); + return; + } + + const hash = createPasswordHash(password, passwordHash); + + const loginData = { + userName: linkData.email ?? "", + passwordHash: hash, + }; + + const personalData = { + firstname: name, + lastname: surName, + }; + + try { + await activateConfirmUser({ + personalData, + loginData, + key: linkData.confirmHeader ?? "", + userId: linkData.uid ?? "", + activationStatus: EmployeeActivationStatus.Activated, + }); + + setIsLoading(false); + + window.location.replace(defaultPage); + } catch (error) { + const knownError = error as TError; + let errorMessage: string; + + if (typeof knownError === "object") { + errorMessage = + knownError?.response?.data?.error?.message || + knownError?.statusText || + knownError?.message || + ""; + } else { + errorMessage = knownError; + } + + setIsLoading(false); + toastr.error(errorMessage); + } + }; + + const activateConfirmUser = async ({ + personalData, + loginData, + key, + userId, + activationStatus, + }: TActivateConfirmUser) => { + const { userName, passwordHash } = loginData; + + const res1 = await changePassword(passwordHash, userId, key); + const res2 = await updateActivationStatus(activationStatus, userId, key); + const res3 = await login(userName, passwordHash); + const res4 = await updateUser( + userId, + personalData.firstname, + personalData.lastname, + ); + }; + + const onKeyPress = (event: KeyboardEvent) => { + if (event.key === "Enter") { + onSubmit(); + } + }; + + return ( + +
+ + + + + + + + + + + + + + +