PromucFlow_constructor/app/client/src/pages/Applications/CreateApplicationForm.tsx

99 lines
2.8 KiB
TypeScript
Raw Normal View History

2019-11-21 10:52:49 +00:00
import React from "react";
import { connect } from "react-redux";
import { Form, reduxForm, InjectedFormProps, Field } from "redux-form";
2019-11-21 10:52:49 +00:00
import { CREATE_APPLICATION_FORM_NAME } from "constants/forms";
import {
createMessage,
ERROR_MESSAGE_NAME_EMPTY,
NAME_SPACE_ERROR,
} from "constants/messages";
import { AppState } from "reducers";
2019-11-21 10:52:49 +00:00
import {
CreateApplicationFormValues,
createApplicationFormSubmitHandler,
CREATE_APPLICATION_FORM_NAME_FIELD,
2019-12-16 08:49:10 +00:00
} from "./helpers";
2019-11-25 09:15:11 +00:00
import TextField from "components/editorComponents/form/fields/TextField";
import FormGroup from "components/editorComponents/form/FormGroup";
2020-02-03 10:37:03 +00:00
import FormFooter from "components/editorComponents/form/FormFooter";
import FormMessage from "components/editorComponents/form/FormMessage";
type Props = InjectedFormProps<
CreateApplicationFormValues,
{
onCancel: () => void;
orgId: string;
initialValues: Record<string, unknown>;
}
> & {
onCancel: () => void;
orgId: string;
initialValues: Record<string, unknown>;
};
const validate = (values: CreateApplicationFormValues) => {
if (!values[CREATE_APPLICATION_FORM_NAME_FIELD]) {
return {
[CREATE_APPLICATION_FORM_NAME_FIELD]: createMessage(
ERROR_MESSAGE_NAME_EMPTY,
),
};
} else if (!values[CREATE_APPLICATION_FORM_NAME_FIELD].trim()) {
return {
[CREATE_APPLICATION_FORM_NAME_FIELD]: createMessage(NAME_SPACE_ERROR),
};
}
return {};
};
2020-02-03 10:37:03 +00:00
// TODO(abhinav): abstract onCancel out.
function CreateApplicationForm(props: Props) {
const { error, handleSubmit, invalid, pristine, submitting } = props;
return (
2019-11-21 10:52:49 +00:00
<Form onSubmit={handleSubmit(createApplicationFormSubmitHandler)}>
2020-02-03 10:37:03 +00:00
{error && !pristine && <FormMessage intent="danger" message={error} />}
<FormGroup intent={error ? "danger" : "none"}>
<TextField
name={CREATE_APPLICATION_FORM_NAME_FIELD}
placeholder="Name"
/>
<Field component="input" name="orgId" type="hidden" />
2019-11-21 10:52:49 +00:00
</FormGroup>
2020-02-03 10:37:03 +00:00
<FormFooter
canSubmit={!invalid}
data-cy="t--create-app-submit"
divider
2020-02-03 10:37:03 +00:00
onCancel={props.onCancel}
onSubmit={handleSubmit(createApplicationFormSubmitHandler)}
size="small"
2020-02-03 10:37:03 +00:00
submitOnEnter
submitText="Submit"
submitting={submitting && !error}
/>
2019-11-21 10:52:49 +00:00
</Form>
);
}
const mapStateToProps = (state: AppState, props: Props): any => {
const orgId = props.orgId;
return {
2020-06-25 10:04:57 +00:00
initialValues: { orgId },
};
};
export default connect(mapStateToProps)(
reduxForm<
CreateApplicationFormValues,
{
onCancel: () => void;
orgId: string;
initialValues: Record<string, unknown>;
}
>({
validate,
2020-02-03 10:37:03 +00:00
form: CREATE_APPLICATION_FORM_NAME,
onSubmit: createApplicationFormSubmitHandler,
})(CreateApplicationForm),
);