mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-25 01:45:59 +00:00
save changes from form view
This commit is contained in:
@@ -92,7 +92,7 @@ function isDataCell(cell) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function FormView(props) {
|
export default function FormView(props) {
|
||||||
const { toolbarPortalRef, tabVisible, config, setConfig, onNavigate, former } = props;
|
const { toolbarPortalRef, tabVisible, config, setConfig, onNavigate, former, onSave } = props;
|
||||||
/** @type {import('dbgate-datalib').FormViewDisplay} */
|
/** @type {import('dbgate-datalib').FormViewDisplay} */
|
||||||
const formDisplay = props.formDisplay;
|
const formDisplay = props.formDisplay;
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
@@ -188,6 +188,15 @@ export default function FormView(props) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function handleSave() {
|
||||||
|
if (inplaceEditorState.cell) {
|
||||||
|
// @ts-ignore
|
||||||
|
dispatchInsplaceEditor({ type: 'shouldSave' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (onSave) onSave();
|
||||||
|
}
|
||||||
|
|
||||||
const scrollIntoView = (cell) => {
|
const scrollIntoView = (cell) => {
|
||||||
const element = cellRefs.current[`${cell[0]},${cell[1]}`];
|
const element = cellRefs.current[`${cell[0]},${cell[1]}`];
|
||||||
if (element) element.scrollIntoView();
|
if (element) element.scrollIntoView();
|
||||||
@@ -217,6 +226,12 @@ export default function FormView(props) {
|
|||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (event.keyCode == keycodes.s && event.ctrlKey) {
|
||||||
|
event.preventDefault();
|
||||||
|
handleSave();
|
||||||
|
// this.saveAndFocus();
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!event.ctrlKey &&
|
!event.ctrlKey &&
|
||||||
!event.altKey &&
|
!event.altKey &&
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { TableFormViewDisplay } from 'dbgate-datalib';
|
import { changeSetToSql, createChangeSet, TableFormViewDisplay } from 'dbgate-datalib';
|
||||||
import { findEngineDriver } from 'dbgate-tools';
|
import { findEngineDriver } from 'dbgate-tools';
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import { useConnectionInfo, useDatabaseInfo } from '../utility/metadataLoaders';
|
import { useConnectionInfo, useDatabaseInfo } from '../utility/metadataLoaders';
|
||||||
@@ -6,6 +6,11 @@ import useExtensions from '../utility/useExtensions';
|
|||||||
import FormView from './FormView';
|
import FormView from './FormView';
|
||||||
import axios from '../utility/axios';
|
import axios from '../utility/axios';
|
||||||
import ChangeSetFormer from './ChangeSetFormer';
|
import ChangeSetFormer from './ChangeSetFormer';
|
||||||
|
import ConfirmSqlModal from '../modals/ConfirmSqlModal';
|
||||||
|
import ErrorMessageModal from '../modals/ErrorMessageModal';
|
||||||
|
import { scriptToSql } from 'dbgate-sqltree';
|
||||||
|
import useModalState from '../modals/useModalState';
|
||||||
|
import useShowModal from '../modals/showModal';
|
||||||
|
|
||||||
async function loadRow(props, sql) {
|
async function loadRow(props, sql) {
|
||||||
const { conid, database } = props;
|
const { conid, database } = props;
|
||||||
@@ -27,8 +32,17 @@ async function loadRow(props, sql) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function SqlFormView(props) {
|
export default function SqlFormView(props) {
|
||||||
const { formDisplay, changeSetState, dispatchChangeSet } = props;
|
const { formDisplay, changeSetState, dispatchChangeSet, conid, database } = props;
|
||||||
const [rowData, setRowData] = React.useState(null);
|
const [rowData, setRowData] = React.useState(null);
|
||||||
|
const [reloadToken, setReloadToken] = React.useState(0);
|
||||||
|
|
||||||
|
const confirmSqlModalState = useModalState();
|
||||||
|
const [confirmSql, setConfirmSql] = React.useState('');
|
||||||
|
const showModal = useShowModal();
|
||||||
|
|
||||||
|
const changeSet = changeSetState && changeSetState.value;
|
||||||
|
const changeSetRef = React.useRef(changeSet);
|
||||||
|
changeSetRef.current = changeSet;
|
||||||
|
|
||||||
const handleLoadCurrentRow = async () => {
|
const handleLoadCurrentRow = async () => {
|
||||||
const row = await loadRow(props, formDisplay.getCurrentRowQuery());
|
const row = await loadRow(props, formDisplay.getCurrentRowQuery());
|
||||||
@@ -43,6 +57,10 @@ export default function SqlFormView(props) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
if (formDisplay) handleLoadCurrentRow();
|
||||||
|
}, [reloadToken]);
|
||||||
|
|
||||||
React.useEffect(() => {
|
React.useEffect(() => {
|
||||||
if (formDisplay && !formDisplay.isLoadedCurrentRow(rowData)) {
|
if (formDisplay && !formDisplay.isLoadedCurrentRow(rowData)) {
|
||||||
handleLoadCurrentRow();
|
handleLoadCurrentRow();
|
||||||
@@ -56,6 +74,35 @@ export default function SqlFormView(props) {
|
|||||||
formDisplay,
|
formDisplay,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
|
function handleSave() {
|
||||||
|
const script = changeSetToSql(changeSetRef.current, formDisplay.dbinfo);
|
||||||
|
const sql = scriptToSql(formDisplay.driver, script);
|
||||||
|
setConfirmSql(sql);
|
||||||
|
confirmSqlModalState.open();
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleConfirmSql() {
|
||||||
|
const resp = await axios.request({
|
||||||
|
url: 'database-connections/query-data',
|
||||||
|
method: 'post',
|
||||||
|
params: {
|
||||||
|
conid,
|
||||||
|
database,
|
||||||
|
},
|
||||||
|
data: { sql: confirmSql },
|
||||||
|
});
|
||||||
|
const { errorMessage } = resp.data || {};
|
||||||
|
if (errorMessage) {
|
||||||
|
showModal((modalState) => (
|
||||||
|
<ErrorMessageModal modalState={modalState} message={errorMessage} title="Error when saving" />
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
dispatchChangeSet({ type: 'reset', value: createChangeSet() });
|
||||||
|
setConfirmSql(null);
|
||||||
|
setReloadToken((x) => x + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// const { config, setConfig, cache, setCache, schemaName, pureName, conid, database } = props;
|
// const { config, setConfig, cache, setCache, schemaName, pureName, conid, database } = props;
|
||||||
// const { formViewKey } = config;
|
// const { formViewKey } = config;
|
||||||
|
|
||||||
@@ -84,5 +131,15 @@ export default function SqlFormView(props) {
|
|||||||
// setDisplay(newDisplay);
|
// setDisplay(newDisplay);
|
||||||
// }, [config, cache, conid, database, schemaName, pureName, dbinfo, extensions]);
|
// }, [config, cache, conid, database, schemaName, pureName, dbinfo, extensions]);
|
||||||
|
|
||||||
return <FormView {...props} rowData={rowData} onNavigate={handleNavigate} former={former} />;
|
return (
|
||||||
|
<>
|
||||||
|
<FormView {...props} rowData={rowData} onNavigate={handleNavigate} former={former} onSave={handleSave} />
|
||||||
|
<ConfirmSqlModal
|
||||||
|
modalState={confirmSqlModalState}
|
||||||
|
sql={confirmSql}
|
||||||
|
engine={formDisplay.engine}
|
||||||
|
onConfirm={handleConfirmSql}
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user