-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathAdapterForm.tsx
More file actions
150 lines (131 loc) · 5.07 KB
/
AdapterForm.tsx
File metadata and controls
150 lines (131 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import React, { useMemo } from 'react';
import styled from 'styled-components';
import { Formik, Form } from 'formik';
import { Col, Row, RowContainer } from 'components/lookup-tables/layout-componets';
import { FormSubmit } from 'components/common';
import { useCreateAdapter, useUpdateAdapter } from 'components/lookup-tables/hooks/useLookupTablesAPI';
import useScopePermissions from 'hooks/useScopePermissions';
import usePluginEntities from 'hooks/usePluginEntities';
import useSendTelemetry from 'logic/telemetry/useSendTelemetry';
import { TELEMETRY_EVENT_TYPE } from 'logic/telemetry/Constants';
import type { LookupTableAdapter } from 'logic/lookup-tables/types';
import AdapterFormFields from './AdapterFormFields';
const FlexForm = styled(Form)`
display: flex;
flex-direction: column;
flex-grow: 1;
width: 100%;
`;
type TitleProps = {
title: string;
typeName: string;
create: boolean;
};
const Title = ({ title, typeName, create }: TitleProps) => {
const TagName = create ? 'h3' : 'h2';
return (
<TagName style={{ marginBottom: '12px', width: '100%' }}>
{title} <small>({typeName})</small>
</TagName>
);
};
const INIT_ADAPTER = {
id: undefined,
title: '',
description: '',
name: '',
custom_error_ttl_enabled: false,
custom_error_ttl: null,
custom_error_ttl_unit: null,
config: {},
};
type Props = {
type: string;
title: string;
saved: (response: any) => void;
onCancel: () => void;
create?: boolean;
dataAdapter?: LookupTableAdapter;
};
const DataAdapterForm = ({ type, title, saved, onCancel, create = false, dataAdapter = INIT_ADAPTER }: Props) => {
const sendTelemetry = useSendTelemetry();
const { loadingScopePermissions, scopePermissions } = useScopePermissions(dataAdapter);
const { createAdapter, creatingAdapter } = useCreateAdapter();
const { updateAdapter, updatingAdapter } = useUpdateAdapter();
const adapterPlugins = usePluginEntities('lookupTableAdapters');
const plugin = adapterPlugins.find((p) => p.type === type);
const DocComponent = plugin?.documentationComponent;
const pluginDisplayName = plugin?.displayName || type;
const initialValues = useMemo(() => {
if (plugin?.prepareConfig && dataAdapter?.config) {
return { ...dataAdapter, config: plugin.prepareConfig(dataAdapter.config) };
}
return dataAdapter;
}, [dataAdapter, plugin]);
const handleSubmit = async (values: LookupTableAdapter) => {
const promise = create ? createAdapter(values) : updateAdapter(values);
return promise.then((response) => {
sendTelemetry(TELEMETRY_EVENT_TYPE.LUT[create ? 'DATA_ADAPTER_CREATED' : 'DATA_ADAPTER_UPDATED'], {
app_pathname: 'lut',
app_section: 'lut_data_adapter',
event_details: {
type: dataAdapter?.config?.type,
},
});
saved(response);
});
};
const canModify = create || (!loadingScopePermissions && scopePermissions?.is_mutable);
return (
<RowContainer style={{ flexGrow: 1 }} $withDocs={!!DocComponent}>
<Col style={{ flexGrow: 1, height: '100%' }}>
<Title title={title} typeName={pluginDisplayName} create={create} />
<Formik initialValues={initialValues} onSubmit={handleSubmit} validateOnBlur={false} enableReinitialize>
{({ isSubmitting, isValid }) => (
<FlexForm className="form form-horizontal">
<Row $gap="xl" style={{ flexGrow: 1 }}>
<div style={{ width: DocComponent ? '60%' : '100%' }}>
<AdapterFormFields />
</div>
{DocComponent && (
<div style={{ width: '40%', flexGrow: 0 }}>
<DocComponent dataAdapterId={dataAdapter?.id} />
</div>
)}
</Row>
{canModify && (
<Row $align="center" $justify="flex-end">
<FormSubmit
submitButtonText={create ? 'Create adapter' : 'Update adapter'}
submitLoadingText={create ? 'Creating adapter...' : 'Updating adapter...'}
isSubmitting={isSubmitting || creatingAdapter || updatingAdapter}
disabledSubmit={isSubmitting || creatingAdapter || updatingAdapter || !isValid}
isAsyncSubmit
onCancel={onCancel}
/>
</Row>
)}
</FlexForm>
)}
</Formik>
</Col>
</RowContainer>
);
};
export default DataAdapterForm;