2022-12-20 06:22:36 +00:00
|
|
|
import React, { useRef, useState } from "react";
|
2021-09-12 16:36:43 +00:00
|
|
|
import styled from "styled-components";
|
|
|
|
|
import { connect } from "react-redux";
|
|
|
|
|
import DetailsForm from "./DetailsForm";
|
|
|
|
|
import NewsletterForm from "./NewsletterForm";
|
|
|
|
|
import AppsmithLogo from "assets/images/appsmith_logo.png";
|
|
|
|
|
import {
|
2022-12-20 06:22:36 +00:00
|
|
|
WELCOME_FORM_CUSTOM_USECASE_FIELD_NAME,
|
2021-09-12 16:36:43 +00:00
|
|
|
WELCOME_FORM_EMAIL_FIELD_NAME,
|
|
|
|
|
WELCOME_FORM_NAME,
|
|
|
|
|
WELCOME_FORM_NAME_FIELD_NAME,
|
|
|
|
|
WELCOME_FORM_PASSWORD_FIELD_NAME,
|
|
|
|
|
WELCOME_FORM_ROLE_FIELD_NAME,
|
|
|
|
|
WELCOME_FORM_ROLE_NAME_FIELD_NAME,
|
2022-12-20 06:22:36 +00:00
|
|
|
WELCOME_FORM_USECASE_FIELD_NAME,
|
2021-09-12 16:36:43 +00:00
|
|
|
WELCOME_FORM_VERIFY_PASSWORD_FIELD_NAME,
|
2022-09-02 17:15:08 +00:00
|
|
|
} from "@appsmith/constants/forms";
|
2022-04-13 10:07:11 +00:00
|
|
|
import {
|
|
|
|
|
FormErrors,
|
|
|
|
|
formValueSelector,
|
|
|
|
|
getFormSyncErrors,
|
|
|
|
|
InjectedFormProps,
|
|
|
|
|
reduxForm,
|
|
|
|
|
} from "redux-form";
|
2021-09-12 16:36:43 +00:00
|
|
|
import { isEmail, isStrongPassword } from "utils/formhelpers";
|
2022-08-24 12:16:32 +00:00
|
|
|
import { AppState } from "@appsmith/reducers";
|
2022-01-07 06:08:17 +00:00
|
|
|
import { SUPER_USER_SUBMIT_PATH } from "@appsmith/constants/ApiConstants";
|
2021-09-12 16:36:43 +00:00
|
|
|
|
|
|
|
|
const PageWrapper = styled.div`
|
|
|
|
|
width: 100%;
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: center;
|
2021-09-17 10:01:42 +00:00
|
|
|
height: 100vh;
|
|
|
|
|
overflow: auto;
|
|
|
|
|
position: relative;
|
|
|
|
|
z-index: 100;
|
2021-09-12 16:36:43 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const SetupFormContainer = styled.div`
|
2021-09-24 09:17:58 +00:00
|
|
|
padding: 120px 42px 0px 0px;
|
2021-09-12 16:36:43 +00:00
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const SetupStep = styled.div<{ active: boolean }>`
|
|
|
|
|
display: ${(props) => (props.active ? "block" : "none")};
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const LogoContainer = styled.div`
|
|
|
|
|
padding-left: ${(props) => props.theme.spaces[17] * 2}px;
|
|
|
|
|
padding-top: ${(props) => props.theme.spaces[12] * 2}px;
|
|
|
|
|
transform: translate(-11px, 0);
|
|
|
|
|
background-color: ${(props) => props.theme.colors.homepageBackground};
|
|
|
|
|
position: fixed;
|
|
|
|
|
width: 566px;
|
|
|
|
|
height: 112px;
|
|
|
|
|
z-index: 1;
|
|
|
|
|
top: 0;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const AppsmithLogoImg = styled.img`
|
|
|
|
|
max-width: 170px;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const SpaceFiller = styled.div`
|
|
|
|
|
height: 100px;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
export type DetailsFormValues = {
|
|
|
|
|
name?: string;
|
|
|
|
|
email?: string;
|
|
|
|
|
password?: string;
|
|
|
|
|
verifyPassword?: string;
|
|
|
|
|
role?: string;
|
|
|
|
|
useCase?: string;
|
2022-02-19 06:26:10 +00:00
|
|
|
custom_useCase?: string;
|
2021-09-12 16:36:43 +00:00
|
|
|
role_name?: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const validate = (values: DetailsFormValues) => {
|
|
|
|
|
const errors: DetailsFormValues = {};
|
|
|
|
|
if (!values.name) {
|
|
|
|
|
errors.name = "Please enter a valid Full Name";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!values.email || !isEmail(values.email)) {
|
|
|
|
|
errors.email = "Please enter a valid Email address";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!values.password || !isStrongPassword(values.password)) {
|
|
|
|
|
errors.password = "Please enter a strong password";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!values.verifyPassword || values.password != values.verifyPassword) {
|
|
|
|
|
errors.verifyPassword = "Please reenter the password";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!values.role) {
|
|
|
|
|
errors.role = "Please select a role";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (values.role == "other" && !values.role_name) {
|
|
|
|
|
errors.role_name = "Please enter a role";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!values.useCase) {
|
|
|
|
|
errors.useCase = "Please select a use case";
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-19 06:26:10 +00:00
|
|
|
if (values.useCase === "other" && !values.custom_useCase)
|
|
|
|
|
errors.custom_useCase = "Please enter a use case";
|
|
|
|
|
|
2021-09-12 16:36:43 +00:00
|
|
|
return errors;
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-13 10:07:11 +00:00
|
|
|
export type SetupFormProps = DetailsFormValues & {
|
|
|
|
|
formSyncErrors?: FormErrors<string, string>;
|
|
|
|
|
} & InjectedFormProps<
|
|
|
|
|
DetailsFormValues,
|
|
|
|
|
{
|
|
|
|
|
formSyncErrors?: FormErrors<string, string>;
|
|
|
|
|
}
|
|
|
|
|
>;
|
|
|
|
|
|
|
|
|
|
function SetupForm(props: SetupFormProps) {
|
2021-09-12 16:36:43 +00:00
|
|
|
const signupURL = `/api/v1/${SUPER_USER_SUBMIT_PATH}`;
|
|
|
|
|
const [showDetailsForm, setShowDetailsForm] = useState(true);
|
|
|
|
|
const formRef = useRef<HTMLFormElement>(null);
|
|
|
|
|
const onSubmit = () => {
|
|
|
|
|
const form: HTMLFormElement = formRef.current as HTMLFormElement;
|
|
|
|
|
const verifyPassword: HTMLInputElement = document.querySelector(
|
|
|
|
|
`[name="verifyPassword"]`,
|
|
|
|
|
) as HTMLInputElement;
|
|
|
|
|
const roleInput = document.createElement("input");
|
|
|
|
|
verifyPassword.removeAttribute("name");
|
|
|
|
|
roleInput.type = "text";
|
|
|
|
|
roleInput.name = "role";
|
|
|
|
|
roleInput.style.display = "none";
|
2022-02-19 06:26:10 +00:00
|
|
|
if (props.role !== "other") {
|
2021-09-12 16:36:43 +00:00
|
|
|
roleInput.value = props.role as string;
|
|
|
|
|
} else {
|
|
|
|
|
roleInput.value = props.role_name as string;
|
|
|
|
|
const roleNameInput: HTMLInputElement = document.querySelector(
|
|
|
|
|
`[name="role_name"]`,
|
|
|
|
|
) as HTMLInputElement;
|
|
|
|
|
if (roleNameInput) roleNameInput.remove();
|
|
|
|
|
}
|
|
|
|
|
form.appendChild(roleInput);
|
|
|
|
|
const useCaseInput = document.createElement("input");
|
|
|
|
|
useCaseInput.type = "text";
|
|
|
|
|
useCaseInput.name = "useCase";
|
|
|
|
|
useCaseInput.style.display = "none";
|
2022-02-19 06:26:10 +00:00
|
|
|
if (props.useCase !== "other") {
|
|
|
|
|
useCaseInput.value = props.useCase as string;
|
|
|
|
|
} else {
|
|
|
|
|
useCaseInput.value = props.custom_useCase as string;
|
|
|
|
|
const customUseCaseInput: HTMLInputElement = document.querySelector(
|
|
|
|
|
`[name="custom_useCase"]`,
|
|
|
|
|
) as HTMLInputElement;
|
|
|
|
|
if (customUseCaseInput) customUseCaseInput.remove();
|
|
|
|
|
}
|
2021-09-12 16:36:43 +00:00
|
|
|
form.appendChild(useCaseInput);
|
|
|
|
|
return true;
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-13 10:07:11 +00:00
|
|
|
const onKeyDown = (event: React.KeyboardEvent<HTMLFormElement>) => {
|
|
|
|
|
if (event.key === "Enter") {
|
|
|
|
|
if (props.valid) {
|
|
|
|
|
if (showDetailsForm) {
|
|
|
|
|
// If we are on the details page we do not want to submit the form
|
|
|
|
|
// instead we move the user to the next page
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
onNext();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// The fields to be marked as touched so that we can display the errors
|
|
|
|
|
const toTouch = [];
|
|
|
|
|
// We fetch the fields which are invalid
|
|
|
|
|
for (const key in props.formSyncErrors) {
|
|
|
|
|
props.formSyncErrors.hasOwnProperty(key) && toTouch.push(key);
|
|
|
|
|
}
|
|
|
|
|
props.touch(...toTouch);
|
|
|
|
|
// prevent submitting the form on enter if the values are invalid
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onNext = () => {
|
|
|
|
|
setShowDetailsForm(false);
|
|
|
|
|
};
|
|
|
|
|
|
2021-09-12 16:36:43 +00:00
|
|
|
return (
|
|
|
|
|
<PageWrapper>
|
|
|
|
|
<SetupFormContainer>
|
|
|
|
|
<LogoContainer>
|
|
|
|
|
<AppsmithLogoImg alt="Appsmith logo" src={AppsmithLogo} />
|
|
|
|
|
</LogoContainer>
|
|
|
|
|
<form
|
|
|
|
|
action={signupURL}
|
2022-04-13 10:07:11 +00:00
|
|
|
data-testid="super-user-form"
|
2021-09-12 16:36:43 +00:00
|
|
|
id="super-user-form"
|
|
|
|
|
method="POST"
|
2022-04-13 10:07:11 +00:00
|
|
|
onKeyDown={onKeyDown}
|
2021-09-12 16:36:43 +00:00
|
|
|
onSubmit={onSubmit}
|
|
|
|
|
ref={formRef}
|
|
|
|
|
>
|
|
|
|
|
<SetupStep active={showDetailsForm}>
|
2022-04-13 10:07:11 +00:00
|
|
|
<DetailsForm {...props} onNext={onNext} />
|
2021-09-12 16:36:43 +00:00
|
|
|
</SetupStep>
|
|
|
|
|
<SetupStep active={!showDetailsForm}>
|
|
|
|
|
<NewsletterForm />
|
|
|
|
|
</SetupStep>
|
|
|
|
|
</form>
|
|
|
|
|
<SpaceFiller />
|
|
|
|
|
</SetupFormContainer>
|
|
|
|
|
</PageWrapper>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const selector = formValueSelector(WELCOME_FORM_NAME);
|
|
|
|
|
export default connect((state: AppState) => {
|
|
|
|
|
return {
|
|
|
|
|
name: selector(state, WELCOME_FORM_NAME_FIELD_NAME),
|
|
|
|
|
email: selector(state, WELCOME_FORM_EMAIL_FIELD_NAME),
|
|
|
|
|
password: selector(state, WELCOME_FORM_PASSWORD_FIELD_NAME),
|
|
|
|
|
verify_password: selector(state, WELCOME_FORM_VERIFY_PASSWORD_FIELD_NAME),
|
|
|
|
|
role: selector(state, WELCOME_FORM_ROLE_FIELD_NAME),
|
|
|
|
|
role_name: selector(state, WELCOME_FORM_ROLE_NAME_FIELD_NAME),
|
|
|
|
|
useCase: selector(state, WELCOME_FORM_USECASE_FIELD_NAME),
|
2022-02-19 06:26:10 +00:00
|
|
|
custom_useCase: selector(state, WELCOME_FORM_CUSTOM_USECASE_FIELD_NAME),
|
2022-04-13 10:07:11 +00:00
|
|
|
formSyncErrors: getFormSyncErrors(WELCOME_FORM_NAME)(state),
|
2021-09-12 16:36:43 +00:00
|
|
|
};
|
|
|
|
|
}, null)(
|
2022-04-13 10:07:11 +00:00
|
|
|
reduxForm<DetailsFormValues, { formSyncErrors?: FormErrors<string, string> }>(
|
|
|
|
|
{
|
|
|
|
|
validate,
|
|
|
|
|
form: WELCOME_FORM_NAME,
|
|
|
|
|
touchOnBlur: true,
|
|
|
|
|
},
|
|
|
|
|
)(SetupForm),
|
2021-09-12 16:36:43 +00:00
|
|
|
);
|