form view refactored refresh

This commit is contained in:
Jan Prochazka
2021-01-14 19:08:52 +01:00
parent a21b61cc7e
commit ad4fa77e46
3 changed files with 136 additions and 47 deletions

View File

@@ -53,13 +53,24 @@ export class FormViewDisplay {
[uniqueName]: value, [uniqueName]: value,
}, },
})); }));
this.reload();
} }
removeFilter(uniqueName) { removeFilter(uniqueName) {
const reloadRequired = !!this.config.filters[uniqueName];
this.setConfig((cfg) => ({ this.setConfig((cfg) => ({
...cfg, ...cfg,
formFilterColumns: (cfg.formFilterColumns || []).filter((x) => x != uniqueName), formFilterColumns: (cfg.formFilterColumns || []).filter((x) => x != uniqueName),
filters: _.omit(cfg.filters || [], uniqueName), filters: _.omit(cfg.filters || [], uniqueName),
})); }));
if (reloadRequired) this.reload();
}
reload() {
this.setCache((cache) => ({
// ...cache,
...createGridCache(),
refreshTime: new Date().getTime(),
}));
} }
} }

View File

@@ -33,7 +33,7 @@ export class TableFormViewDisplay extends FormViewDisplay {
super(config, setConfig, cache, setCache, driver, dbinfo); super(config, setConfig, cache, setCache, driver, dbinfo);
this.gridDisplay = new TableGridDisplay(tableName, driver, config, setConfig, cache, setCache, dbinfo); this.gridDisplay = new TableGridDisplay(tableName, driver, config, setConfig, cache, setCache, dbinfo);
this.isLoadedCorrectly = this.gridDisplay.isLoadedCorrectly; this.isLoadedCorrectly = this.gridDisplay.isLoadedCorrectly && !!this.driver;
this.columns = this.gridDisplay.columns; this.columns = this.gridDisplay.columns;
this.baseTable = this.gridDisplay.baseTable; this.baseTable = this.gridDisplay.baseTable;
} }

View File

@@ -33,6 +33,7 @@ async function loadRow(props, sql) {
} }
export default function SqlFormView(props) { export default function SqlFormView(props) {
// console.log('SqlFormView', props);
const { const {
formDisplay, formDisplay,
changeSetState, changeSetState,
@@ -42,11 +43,11 @@ export default function SqlFormView(props) {
onReferenceSourceChanged, onReferenceSourceChanged,
refReloadToken, refReloadToken,
} = props; } = props;
const [rowData, setRowData] = React.useState(null); // const [rowData, setRowData] = React.useState(null);
const [reloadToken, setReloadToken] = React.useState(0); // const [reloadToken, setReloadToken] = React.useState(0);
const [rowCountInfo, setRowCountInfo] = React.useState(null); // const [rowCountInfo, setRowCountInfo] = React.useState(null);
const [isLoading, setIsLoading] = React.useState(false); // const [isLoading, setIsLoading] = React.useState(false);
const loadedFiltersRef = React.useRef(''); // const loadedFiltersRef = React.useRef('');
const confirmSqlModalState = useModalState(); const confirmSqlModalState = useModalState();
const [confirmSql, setConfirmSql] = React.useState(''); const [confirmSql, setConfirmSql] = React.useState('');
@@ -56,69 +57,146 @@ export default function SqlFormView(props) {
const changeSetRef = React.useRef(changeSet); const changeSetRef = React.useRef(changeSet);
changeSetRef.current = changeSet; changeSetRef.current = changeSet;
const [loadProps, setLoadProps] = React.useState({
isLoadingData: false,
isLoadedData: false,
rowData: null,
isLoadingCount: false,
isLoadedCount: false,
loadedTime: new Date().getTime(),
allRowCount: null,
rowCountBefore: null,
errorMessage: null,
});
const {
isLoadingData,
rowData,
isLoadedData,
isLoadingCount,
isLoadedCount,
loadedTime,
allRowCount,
rowCountBefore,
errorMessage,
} = loadProps;
const handleLoadCurrentRow = async () => { const handleLoadCurrentRow = async () => {
let isLoaded = false; if (isLoadingData) return;
let isLoadedRow = false;
if (formDisplay.config.formViewKey) { if (formDisplay.config.formViewKey) {
setIsLoading(true); setLoadProps((oldLoadProps) => ({
...oldLoadProps,
isLoadingData: true,
}));
const row = await loadRow(props, formDisplay.getCurrentRowQuery()); const row = await loadRow(props, formDisplay.getCurrentRowQuery());
setIsLoading(false); setLoadProps((oldLoadProps) => ({
setRowData(row); ...oldLoadProps,
isLoaded = !!row; isLoadingData: false,
isLoadedData: true,
rowData: row,
loadedTime: new Date().getTime(),
}));
isLoadedRow = !!row;
} }
if (!isLoaded) { if (!isLoadedRow) {
await handleNavigate('first'); await handleNavigate('first');
} }
}; };
const handleLoadRowCount = async () => { const handleLoadRowCount = async () => {
setLoadProps((oldLoadProps) => ({
...oldLoadProps,
isLoadingCount: true,
}));
const countRow = await loadRow(props, formDisplay.getCountQuery()); const countRow = await loadRow(props, formDisplay.getCountQuery());
const countBeforeRow = await loadRow(props, formDisplay.getBeforeCountQuery()); const countBeforeRow = await loadRow(props, formDisplay.getBeforeCountQuery());
if (countRow && countBeforeRow) { setLoadProps((oldLoadProps) => ({
setRowCountInfo({ ...oldLoadProps,
allRowCount: parseInt(countRow.count), isLoadedCount: true,
rowCountBefore: parseInt(countBeforeRow.count), isLoadingCount: false,
}); allRowCount: countRow ? parseInt(countRow.count) : null,
} rowCountBefore: countBeforeRow ? parseInt(countBeforeRow.count) : null,
}));
}; };
const handleNavigate = async (command) => { const handleNavigate = async (command) => {
setIsLoading(true); setLoadProps((oldLoadProps) => ({
...oldLoadProps,
isLoadingData: true,
}));
const row = await loadRow(props, formDisplay.navigateRowQuery(command)); const row = await loadRow(props, formDisplay.navigateRowQuery(command));
setIsLoading(false);
setRowData(row);
if (row) { if (row) {
formDisplay.navigate(row); formDisplay.navigate(row);
} }
setLoadProps((oldLoadProps) => ({
...oldLoadProps,
isLoadingData: false,
isLoadedData: true,
isLoadedCount: false,
allRowCount: null,
rowCountBefore: null,
rowData: row,
loadedTime: new Date().getTime(),
}));
}; };
React.useEffect(() => { React.useEffect(() => {
if (onReferenceSourceChanged && rowData) onReferenceSourceChanged([rowData]); if (onReferenceSourceChanged && rowData) onReferenceSourceChanged([rowData]);
}, [rowData, refReloadToken]); }, [rowData, refReloadToken]);
React.useEffect(() => {
loadedFiltersRef.current = formDisplay ? stableStringify(formDisplay.config) : null;
}, [rowData]);
React.useEffect(() => {
if (formDisplay) handleLoadCurrentRow();
setRowCountInfo(null);
handleLoadRowCount();
}, [reloadToken]);
React.useEffect(() => { React.useEffect(() => {
if (!formDisplay.isLoadedCorrectly) return; if (!formDisplay.isLoadedCorrectly) return;
if (!isLoadedData && !isLoadingData) handleLoadCurrentRow();
if (isLoadedData && !isLoadingCount && !isLoadedCount) handleLoadRowCount();
});
if ( // React.useEffect(() => {
formDisplay && // loadedFiltersRef.current = formDisplay ? stableStringify(formDisplay.config) : null;
(!formDisplay.isLoadedCurrentRow(rowData) || // }, [rowData]);
loadedFiltersRef.current != stableStringify(formDisplay.config.filters))
) { // React.useEffect(() => {
handleLoadCurrentRow(); // if (formDisplay) handleLoadCurrentRow();
// setRowCountInfo(null);
// handleLoadRowCount();
// }, [reloadToken]);
// React.useEffect(() => {
// if (!formDisplay.isLoadedCorrectly) return;
// if (
// formDisplay &&
// (!formDisplay.isLoadedCurrentRow(rowData) ||
// loadedFiltersRef.current != stableStringify(formDisplay.config.filters))
// ) {
// handleLoadCurrentRow();
// }
// setRowCountInfo(null);
// handleLoadRowCount();
// }, [formDisplay]);
const reload = () => {
setLoadProps({
isLoadingData: false,
isLoadedData: false,
isLoadingCount: false,
isLoadedCount: false,
rowData: null,
loadedTime: new Date().getTime(),
allRowCount: null,
rowCountBefore: null,
errorMessage: null,
});
};
React.useEffect(() => {
if (props.masterLoadedTime && props.masterLoadedTime > loadedTime) {
formDisplay.reload();
} }
setRowCountInfo(null); if (formDisplay.cache.refreshTime > loadedTime) {
handleLoadRowCount(); reload();
}, [formDisplay]); }
});
const former = React.useMemo(() => new ChangeSetFormer(rowData, changeSetState, dispatchChangeSet, formDisplay), [ const former = React.useMemo(() => new ChangeSetFormer(rowData, changeSetState, dispatchChangeSet, formDisplay), [
rowData, rowData,
@@ -135,7 +213,6 @@ export default function SqlFormView(props) {
} }
async function handleConfirmSql() { async function handleConfirmSql() {
setIsLoading(true);
const resp = await axios.request({ const resp = await axios.request({
url: 'database-connections/query-data', url: 'database-connections/query-data',
method: 'post', method: 'post',
@@ -145,7 +222,6 @@ export default function SqlFormView(props) {
}, },
data: { sql: confirmSql }, data: { sql: confirmSql },
}); });
setIsLoading(false);
const { errorMessage } = resp.data || {}; const { errorMessage } = resp.data || {};
if (errorMessage) { if (errorMessage) {
showModal((modalState) => ( showModal((modalState) => (
@@ -154,7 +230,8 @@ export default function SqlFormView(props) {
} else { } else {
dispatchChangeSet({ type: 'reset', value: createChangeSet() }); dispatchChangeSet({ type: 'reset', value: createChangeSet() });
setConfirmSql(null); setConfirmSql(null);
setReloadToken((x) => x + 1); formDisplay.reload();
// setReloadToken((x) => x + 1);
} }
} }
@@ -194,13 +271,14 @@ export default function SqlFormView(props) {
onNavigate={handleNavigate} onNavigate={handleNavigate}
former={former} former={former}
onSave={handleSave} onSave={handleSave}
isLoading={isLoading} isLoading={isLoadingData}
onReload={() => setReloadToken((x) => x + 1)} onReload={() => formDisplay.reload()}
onReconnect={async () => { onReconnect={async () => {
await axios.post('database-connections/refresh', { conid, database }); await axios.post('database-connections/refresh', { conid, database });
setReloadToken((x) => x + 1); formDisplay.reload();
}} }}
{...rowCountInfo} allRowCount={allRowCount}
rowCountBefore={rowCountBefore}
/> />
<ConfirmSqlModal <ConfirmSqlModal
modalState={confirmSqlModalState} modalState={confirmSqlModalState}