2019-11-21 10:52:49 +00:00
|
|
|
import React from "react";
|
2020-06-17 10:19:56 +00:00
|
|
|
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";
|
2021-03-13 14:24:45 +00:00
|
|
|
import {
|
|
|
|
|
createMessage,
|
|
|
|
|
ERROR_MESSAGE_NAME_EMPTY,
|
|
|
|
|
NAME_SPACE_ERROR,
|
|
|
|
|
} from "constants/messages";
|
2020-06-17 10:19:56 +00:00
|
|
|
import { AppState } from "reducers";
|
2019-11-21 10:52:49 +00:00
|
|
|
import {
|
|
|
|
|
CreateApplicationFormValues,
|
|
|
|
|
createApplicationFormSubmitHandler,
|
2020-10-08 10:32:31 +00:00
|
|
|
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";
|
2019-12-23 12:16:33 +00:00
|
|
|
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";
|
2019-11-07 04:59:40 +00:00
|
|
|
|
2020-06-17 10:19:56 +00:00
|
|
|
type Props = InjectedFormProps<
|
|
|
|
|
CreateApplicationFormValues,
|
2020-11-03 13:05:40 +00:00
|
|
|
{
|
|
|
|
|
onCancel: () => void;
|
|
|
|
|
orgId: string;
|
|
|
|
|
initialValues: Record<string, unknown>;
|
|
|
|
|
}
|
2020-06-17 10:19:56 +00:00
|
|
|
> & {
|
|
|
|
|
onCancel: () => void;
|
|
|
|
|
orgId: string;
|
2020-11-03 13:05:40 +00:00
|
|
|
initialValues: Record<string, unknown>;
|
2020-06-17 10:19:56 +00:00
|
|
|
};
|
|
|
|
|
|
2020-10-08 10:32:31 +00:00
|
|
|
const validate = (values: CreateApplicationFormValues) => {
|
|
|
|
|
if (!values[CREATE_APPLICATION_FORM_NAME_FIELD]) {
|
2021-03-13 14:24:45 +00:00
|
|
|
return {
|
|
|
|
|
[CREATE_APPLICATION_FORM_NAME_FIELD]: createMessage(
|
|
|
|
|
ERROR_MESSAGE_NAME_EMPTY,
|
|
|
|
|
),
|
|
|
|
|
};
|
2020-10-08 10:32:31 +00:00
|
|
|
} else if (!values[CREATE_APPLICATION_FORM_NAME_FIELD].trim()) {
|
|
|
|
|
return {
|
2021-03-13 14:24:45 +00:00
|
|
|
[CREATE_APPLICATION_FORM_NAME_FIELD]: createMessage(NAME_SPACE_ERROR),
|
2020-10-08 10:32:31 +00:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-03 10:37:03 +00:00
|
|
|
// TODO(abhinav): abstract onCancel out.
|
2020-06-17 10:19:56 +00:00
|
|
|
|
2021-04-28 10:28:39 +00:00
|
|
|
function CreateApplicationForm(props: Props) {
|
2021-05-13 08:35:39 +00:00
|
|
|
const { error, handleSubmit, invalid, pristine, submitting } = props;
|
2019-11-07 04:59:40 +00:00
|
|
|
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} />}
|
2020-02-24 07:52:22 +00:00
|
|
|
<FormGroup intent={error ? "danger" : "none"}>
|
2020-10-08 10:32:31 +00:00
|
|
|
<TextField
|
|
|
|
|
name={CREATE_APPLICATION_FORM_NAME_FIELD}
|
|
|
|
|
placeholder="Name"
|
|
|
|
|
/>
|
2021-04-28 10:28:39 +00:00
|
|
|
<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
|
2021-04-28 10:28:39 +00:00
|
|
|
canSubmit={!invalid}
|
|
|
|
|
data-cy="t--create-app-submit"
|
|
|
|
|
divider
|
2020-02-03 10:37:03 +00:00
|
|
|
onCancel={props.onCancel}
|
|
|
|
|
onSubmit={handleSubmit(createApplicationFormSubmitHandler)}
|
2021-04-28 10:28:39 +00:00
|
|
|
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>
|
2019-11-07 04:59:40 +00:00
|
|
|
);
|
2021-04-28 10:28:39 +00:00
|
|
|
}
|
2019-11-07 04:59:40 +00:00
|
|
|
|
2020-06-17 10:19:56 +00:00
|
|
|
const mapStateToProps = (state: AppState, props: Props): any => {
|
|
|
|
|
const orgId = props.orgId;
|
|
|
|
|
return {
|
2020-06-25 10:04:57 +00:00
|
|
|
initialValues: { orgId },
|
2020-06-17 10:19:56 +00:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(
|
|
|
|
|
reduxForm<
|
|
|
|
|
CreateApplicationFormValues,
|
2020-11-03 13:05:40 +00:00
|
|
|
{
|
|
|
|
|
onCancel: () => void;
|
|
|
|
|
orgId: string;
|
|
|
|
|
initialValues: Record<string, unknown>;
|
|
|
|
|
}
|
2020-06-17 10:19:56 +00:00
|
|
|
>({
|
2020-10-08 10:32:31 +00:00
|
|
|
validate,
|
2020-02-03 10:37:03 +00:00
|
|
|
form: CREATE_APPLICATION_FORM_NAME,
|
|
|
|
|
onSubmit: createApplicationFormSubmitHandler,
|
2020-06-17 10:19:56 +00:00
|
|
|
})(CreateApplicationForm),
|
|
|
|
|
);
|