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

83 lines
2.6 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 { 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: {} }
> & {
onCancel: () => void;
orgId: string;
initialValues: {};
};
const validate = (values: CreateApplicationFormValues) => {
if (!values[CREATE_APPLICATION_FORM_NAME_FIELD]) {
return { [CREATE_APPLICATION_FORM_NAME_FIELD]: ERROR_MESSAGE_NAME_EMPTY };
} else if (!values[CREATE_APPLICATION_FORM_NAME_FIELD].trim()) {
return {
[CREATE_APPLICATION_FORM_NAME_FIELD]: NAME_SPACE_ERROR,
};
}
return {};
};
2020-02-03 10:37:03 +00:00
// TODO(abhinav): abstract onCancel out.
const CreateApplicationForm = (props: Props) => {
const { error, handleSubmit, pristine, submitting, invalid } = 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 type="hidden" name="orgId" component="input" />
2019-11-21 10:52:49 +00:00
</FormGroup>
2020-02-03 10:37:03 +00:00
<FormFooter
onCancel={props.onCancel}
onSubmit={handleSubmit(createApplicationFormSubmitHandler)}
divider
canSubmit={!invalid}
2020-02-03 10:37:03 +00:00
submitOnEnter
data-cy="t--create-app-submit"
2020-02-03 10:37:03 +00:00
submitText="Submit"
size="small"
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: {} }
>({
validate,
2020-02-03 10:37:03 +00:00
form: CREATE_APPLICATION_FORM_NAME,
onSubmit: createApplicationFormSubmitHandler,
})(CreateApplicationForm),
);