forked from pgadmin-org/pgadmin4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchemaDialogView.jsx
More file actions
271 lines (245 loc) · 8.59 KB
/
SchemaDialogView.jsx
File metadata and controls
271 lines (245 loc) · 8.59 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/////////////////////////////////////////////////////////////
//
// pgAdmin 4 - PostgreSQL Tools
//
// Copyright (C) 2013 - 2025, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
//////////////////////////////////////////////////////////////
import React, { useState, useEffect, useMemo } from 'react';
import CloseIcon from '@mui/icons-material/Close';
import DoneIcon from '@mui/icons-material/Done';
import InfoIcon from '@mui/icons-material/InfoRounded';
import HelpIcon from '@mui/icons-material/HelpRounded';
import PublishIcon from '@mui/icons-material/Publish';
import SaveIcon from '@mui/icons-material/Save';
import SettingsBackupRestoreIcon from '@mui/icons-material/SettingsBackupRestore';
import { Box } from '@mui/material';
import _ from 'lodash';
import PropTypes from 'prop-types';
import { parseApiError } from 'sources/api_instance';
import { usePgAdmin } from 'sources/PgAdminProvider';
import { useIsMounted } from 'sources/custom_hooks';
import { DefaultButton, PgIconButton } from 'sources/components/Buttons';
import CustomPropTypes from 'sources/custom_prop_types';
import gettext from 'sources/gettext';
import { FormLoader } from './FormLoader';
import FormView from './FormView';
import { ResetButton } from './ResetButton';
import { SaveButton } from './SaveButton';
import { SchemaStateContext } from './SchemaState';
import { StyledBox } from './StyledComponents';
import { useSchemaState } from './hooks';
import { getForQueryParams } from './common';
import { QueryToolIcon } from '../components/ExternalIcon';
import TerminalRoundedIcon from '@mui/icons-material/TerminalRounded';
import { WORKSPACES } from '../../../browser/static/js/constants';
/* If its the dialog */
export default function SchemaDialogView({
getInitData, viewHelperProps, loadingText, schema={}, showFooter=true,
isTabView=true, checkDirtyOnEnableSave=false, customCloseBtnName=gettext('Close'), ...props
}) {
// View helper properties
const onDataChange = props.onDataChange;
const [resetKey, setResetKey] = useState(0);
// Schema data state manager
const {schemaState, dataDispatch, reset} = useSchemaState({
schema: schema, getInitData: getInitData, immutableData: {},
viewHelperProps: viewHelperProps, onDataChange: onDataChange,
loadingText,
});
const resetView = () => {
reset();
setResetKey(Date.now());
};
// Is saving operation in progress?
const setSaving = (val) => schemaState.isSaving = val;
const setLoaderText = (val) => schemaState.setMessage(val);
// First element to be set by the FormView to set the focus after loading
// the data.
const checkIsMounted = useIsMounted();
// Notifier object.
const pgAdmin = usePgAdmin();
const Notifier = props.Notifier || pgAdmin.Browser.notifier;
useEffect(() => {
if (!props.resetKey) return;
resetView();
}, [props.resetKey]);
const onResetClick = () => {
const resetIt = () => {
resetView();
return true;
};
if (!props.confirmOnCloseReset) {
resetIt();
return;
}
Notifier.confirm(
gettext('Warning'),
gettext('Changes will be lost. Are you sure you want to reset?'),
resetIt, () => (true),
);
};
const save = (changeData) => {
props.onSave(schemaState.isNew, changeData)
.then(()=>{
if(schema.informText) {
Notifier.alert(
gettext('Warning'),
schema.informText,
);
}
}).catch((err)=>{
schemaState.setError({
name: 'apierror',
message: _.escape(parseApiError(err)),
});
}).finally(()=>{
if(checkIsMounted()) {
setSaving(false);
setLoaderText('');
}
});
};
const onSaveClick = () => {
// Do nothing when there is no change or there is an error
if (
!schemaState._changes || Object.keys(schemaState._changes).length === 0 ||
schemaState.errors.name
) return;
setSaving(true);
setLoaderText(schemaState.customLoadingText || gettext('Saving...'));
if (!schema.warningText) {
save(schemaState.changes(true));
return;
}
Notifier.confirm(
gettext('Warning'),
schema.warningText,
() => { save(schemaState.changes(true)); },
() => {
setSaving(false);
setLoaderText('');
return true;
},
);
};
const getSQLValue = () => {
// Called when SQL tab is active.
if(!schemaState.isDirty) {
return Promise.resolve('-- ' + gettext('No updates.'));
}
if(schemaState.errors.name) {
return Promise.resolve('-- ' + gettext('Definition incomplete.'));
}
const changeData = schemaState._changes;
/*
* Call the passed incoming getSQLValue func to get the SQL
* return of getSQLValue should be a promise.
*/
return props.getSQLValue(schemaState.isNew, getForQueryParams(changeData));
};
const getButtonIcon = () => {
if(props.customSaveBtnIconType == 'upload') {
return <PublishIcon />;
} else if(props.customSaveBtnIconType == 'done') {
return <DoneIcon />;
} else if(props.customSaveBtnIconType == WORKSPACES.QUERY_TOOL) {
return <QueryToolIcon />;
} else if(props.customSaveBtnIconType == WORKSPACES.PSQL_TOOL) {
return <TerminalRoundedIcon style={{width:'unset'}}/>;
}
return <SaveIcon />;
};
/* I am Groot */
return useMemo(() =>
<StyledBox>
<SchemaStateContext.Provider value={schemaState}>
<Box className='Dialog-form'>
<FormLoader/>
<FormView
viewHelperProps={viewHelperProps}
schema={schema} accessPath={[]}
dataDispatch={dataDispatch}
hasSQLTab={props.hasSQL} getSQLValue={getSQLValue}
isTabView={isTabView}
className={props.formClassName}
showError={true} resetKey={resetKey}
focusOnFirstInput={true}
/>
</Box>
{showFooter &&
<Box className='Dialog-footer'>
{
(!props.disableSqlHelp || !props.disableDialogHelp) &&
<Box>
<PgIconButton data-test='sql-help'
onClick={()=>props.onHelp(true, schemaState.isNew)}
icon={<InfoIcon />} disabled={props.disableSqlHelp}
className='Dialog-buttonMargin'
title={ gettext('SQL help for this object type.') }
/>
<PgIconButton data-test='dialog-help'
onClick={()=>props.onHelp(false, schemaState.isNew)}
icon={<HelpIcon />} disabled={props.disableDialogHelp}
title={ gettext('Help for this dialog.') }
/>
</Box>
}
<Box marginLeft='auto'>
{
Boolean(customCloseBtnName) &&
<DefaultButton data-test='Close' onClick={props.onClose}
startIcon={<CloseIcon />} className='Dialog-buttonMargin'>
{ customCloseBtnName }
</DefaultButton>
}
<ResetButton
onClick={onResetClick}
icon={<SettingsBackupRestoreIcon />}
label={ gettext('Reset') }/>
<SaveButton
onClick={onSaveClick} icon={getButtonIcon()}
label={props.customSaveBtnName || gettext('Save')}
checkDirtyOnEnableSave={checkDirtyOnEnableSave}
mode={viewHelperProps.mode}
/>
</Box>
</Box>
}
</SchemaStateContext.Provider>
</StyledBox>, [schema._id, viewHelperProps.mode, resetKey]
);
}
SchemaDialogView.propTypes = {
getInitData: PropTypes.func,
viewHelperProps: PropTypes.shape({
mode: PropTypes.string.isRequired,
serverInfo: PropTypes.shape({
type: PropTypes.string,
version: PropTypes.number,
}),
inCatalog: PropTypes.bool,
keepCid: PropTypes.bool,
}).isRequired,
loadingText: PropTypes.string,
schema: CustomPropTypes.schemaUI,
onSave: PropTypes.func,
onClose: PropTypes.func,
onHelp: PropTypes.func,
onDataChange: PropTypes.func,
confirmOnCloseReset: PropTypes.bool,
isTabView: PropTypes.bool,
hasSQL: PropTypes.bool,
getSQLValue: PropTypes.func,
disableSqlHelp: PropTypes.bool,
disableDialogHelp: PropTypes.bool,
showFooter: PropTypes.bool,
resetKey: PropTypes.any,
customSaveBtnName: PropTypes.string,
customSaveBtnIconType: PropTypes.string,
formClassName: CustomPropTypes.className,
Notifier: PropTypes.object,
checkDirtyOnEnableSave: PropTypes.bool,
customCloseBtnName: PropTypes.string,
};