2023-05-04 12:59:57 +00:00
|
|
|
import React, { useState } from "react";
|
2023-05-19 18:37:06 +00:00
|
|
|
import { Button, Input, toast, Text } from "design-system";
|
2021-03-04 09:37:02 +00:00
|
|
|
import { useDispatch, useSelector } from "react-redux";
|
|
|
|
|
import { getCurrentUser } from "selectors/usersSelectors";
|
|
|
|
|
import { forgotPasswordSubmitHandler } from "pages/UserAuth/helpers";
|
2021-12-23 14:16:09 +00:00
|
|
|
import {
|
|
|
|
|
FORGOT_PASSWORD_SUCCESS_TEXT,
|
2023-05-04 12:59:57 +00:00
|
|
|
USER_DISPLAY_NAME_CHAR_CHECK_FAILED,
|
|
|
|
|
USER_DISPLAY_NAME_PLACEHOLDER,
|
|
|
|
|
USER_DISPLAY_PICTURE_PLACEHOLDER,
|
|
|
|
|
USER_EMAIL_PLACEHOLDER,
|
|
|
|
|
USER_RESET_PASSWORD,
|
2022-02-11 18:08:46 +00:00
|
|
|
} from "@appsmith/constants/messages";
|
2021-03-04 09:37:02 +00:00
|
|
|
import { logoutUser, updateUserDetails } from "actions/userActions";
|
2022-10-06 06:07:52 +00:00
|
|
|
import UserProfileImagePicker from "./UserProfileImagePicker";
|
2023-02-07 09:23:15 +00:00
|
|
|
import { Wrapper, FieldWrapper, LabelWrapper } from "./StyledComponents";
|
2022-05-20 09:07:02 +00:00
|
|
|
import { ANONYMOUS_USERNAME } from "constants/userConstants";
|
2023-05-04 12:59:57 +00:00
|
|
|
import { ALL_LANGUAGE_CHARACTERS_REGEX } from "constants/Regex";
|
|
|
|
|
import { createMessage } from "design-system-old/build/constants/messages";
|
2023-05-19 18:37:06 +00:00
|
|
|
import { notEmptyValidator } from "design-system-old";
|
2023-05-16 09:04:48 +00:00
|
|
|
import { getIsFormLoginEnabled } from "@appsmith/selectors/tenantSelectors";
|
2021-03-04 09:37:02 +00:00
|
|
|
|
2023-05-04 12:59:57 +00:00
|
|
|
const nameValidator = (
|
|
|
|
|
value: string,
|
|
|
|
|
): {
|
|
|
|
|
isValid: boolean;
|
|
|
|
|
message: string;
|
|
|
|
|
} => {
|
|
|
|
|
const notEmpty = notEmptyValidator(value);
|
|
|
|
|
if (!notEmpty.isValid) {
|
|
|
|
|
return notEmpty;
|
|
|
|
|
}
|
|
|
|
|
if (!new RegExp(`^[${ALL_LANGUAGE_CHARACTERS_REGEX} 0-9.'-]+$`).test(value)) {
|
|
|
|
|
return {
|
|
|
|
|
isValid: false,
|
|
|
|
|
message: createMessage(USER_DISPLAY_NAME_CHAR_CHECK_FAILED),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
isValid: true,
|
|
|
|
|
message: "",
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2021-04-28 10:28:39 +00:00
|
|
|
function General() {
|
2021-03-04 09:37:02 +00:00
|
|
|
const user = useSelector(getCurrentUser);
|
2023-05-16 09:04:48 +00:00
|
|
|
const isFormLoginEnabled = useSelector(getIsFormLoginEnabled);
|
2023-05-04 12:59:57 +00:00
|
|
|
const [name, setName] = useState(user?.name);
|
2021-03-04 09:37:02 +00:00
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const forgotPassword = async () => {
|
|
|
|
|
try {
|
|
|
|
|
await forgotPasswordSubmitHandler({ email: user?.email }, dispatch);
|
2023-05-19 18:37:06 +00:00
|
|
|
toast.show(createMessage(FORGOT_PASSWORD_SUCCESS_TEXT, user?.email), {
|
|
|
|
|
kind: "success",
|
2021-03-04 09:37:02 +00:00
|
|
|
});
|
|
|
|
|
dispatch(logoutUser());
|
|
|
|
|
} catch (error) {
|
2023-05-19 18:37:06 +00:00
|
|
|
toast.show((error as { _error: string })._error, {
|
|
|
|
|
kind: "success",
|
2021-03-04 09:37:02 +00:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-05-04 12:59:57 +00:00
|
|
|
const saveName = () => {
|
|
|
|
|
name &&
|
|
|
|
|
nameValidator(name).isValid &&
|
|
|
|
|
dispatch(
|
|
|
|
|
updateUserDetails({
|
|
|
|
|
name,
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
};
|
2021-03-04 09:37:02 +00:00
|
|
|
|
2022-05-20 09:07:02 +00:00
|
|
|
if (user?.email === ANONYMOUS_USERNAME) return null;
|
|
|
|
|
|
2021-03-04 09:37:02 +00:00
|
|
|
return (
|
|
|
|
|
<Wrapper>
|
2021-06-16 18:36:34 +00:00
|
|
|
<FieldWrapper>
|
|
|
|
|
<LabelWrapper>
|
2023-05-19 18:37:06 +00:00
|
|
|
<Text kind="body-m">
|
2023-05-04 12:59:57 +00:00
|
|
|
{createMessage(USER_DISPLAY_PICTURE_PLACEHOLDER)}
|
|
|
|
|
</Text>
|
2021-06-16 18:36:34 +00:00
|
|
|
</LabelWrapper>
|
2023-05-19 18:37:06 +00:00
|
|
|
<div className="user-profile-image-picker">
|
|
|
|
|
<UserProfileImagePicker />
|
|
|
|
|
</div>
|
2021-06-16 18:36:34 +00:00
|
|
|
</FieldWrapper>
|
|
|
|
|
<FieldWrapper>
|
2023-05-19 18:37:06 +00:00
|
|
|
<Input
|
|
|
|
|
data-testid="t--display-name"
|
|
|
|
|
defaultValue={name}
|
|
|
|
|
isRequired
|
|
|
|
|
label={createMessage(USER_DISPLAY_NAME_PLACEHOLDER)}
|
|
|
|
|
labelPosition="top"
|
|
|
|
|
onBlur={saveName}
|
|
|
|
|
onChange={setName}
|
|
|
|
|
onKeyPress={(ev: React.KeyboardEvent) => {
|
|
|
|
|
if (ev.key === "Enter") {
|
|
|
|
|
saveName();
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
placeholder={createMessage(USER_DISPLAY_NAME_PLACEHOLDER)}
|
|
|
|
|
renderAs="input"
|
|
|
|
|
size="md"
|
|
|
|
|
type="text"
|
|
|
|
|
/>
|
2021-06-16 18:36:34 +00:00
|
|
|
</FieldWrapper>
|
2021-03-04 09:37:02 +00:00
|
|
|
<FieldWrapper>
|
2023-05-19 18:37:06 +00:00
|
|
|
<Input
|
|
|
|
|
data-testid="t--user-name"
|
|
|
|
|
defaultValue={user?.email}
|
|
|
|
|
isDisabled
|
|
|
|
|
isReadOnly
|
|
|
|
|
label={createMessage(USER_EMAIL_PLACEHOLDER)}
|
|
|
|
|
labelPosition="top"
|
|
|
|
|
placeholder={createMessage(USER_EMAIL_PLACEHOLDER)}
|
|
|
|
|
renderAs="input"
|
|
|
|
|
size="md"
|
|
|
|
|
type="text"
|
|
|
|
|
/>
|
|
|
|
|
</FieldWrapper>
|
|
|
|
|
<FieldWrapper>
|
|
|
|
|
<div
|
|
|
|
|
style={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
flex: "1 1 0%",
|
|
|
|
|
justifyContent: "flex-end",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-05-16 09:04:48 +00:00
|
|
|
{isFormLoginEnabled && (
|
2023-05-19 18:37:06 +00:00
|
|
|
<Button
|
|
|
|
|
kind="secondary"
|
|
|
|
|
onClick={forgotPassword}
|
|
|
|
|
renderAs="a"
|
|
|
|
|
size="md"
|
|
|
|
|
>
|
2023-05-04 12:59:57 +00:00
|
|
|
{createMessage(USER_RESET_PASSWORD)}
|
2023-05-19 18:37:06 +00:00
|
|
|
</Button>
|
2022-03-03 13:19:10 +00:00
|
|
|
)}
|
2021-03-04 09:37:02 +00:00
|
|
|
</div>
|
|
|
|
|
</FieldWrapper>
|
|
|
|
|
</Wrapper>
|
|
|
|
|
);
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2021-03-04 09:37:02 +00:00
|
|
|
|
|
|
|
|
export default General;
|