Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Delete the reference loading slice (#80137)

Romeo Van Snick5d77c90c36a0
diff --git a/frontend/src/metabase/reference/databases/DatabaseDetail.tsx b/frontend/src/metabase/reference/databases/DatabaseDetail.tsx
index 5c3301c5ed5..d92172eb4b7 100644
--- a/frontend/src/metabase/reference/databases/DatabaseDetail.tsx
+++ b/frontend/src/metabase/reference/databases/DatabaseDetail.tsx
@@ -1,5 +1,6 @@
import cx from "classnames";
import { useFormik } from "formik";
+import { useState } from "react";
import { t } from "ttag";
import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
@@ -16,10 +17,8 @@ import type { User } from "metabase-types/api";
import type { ReferenceRouteProps, StateWithReference } from "../selectors";
import {
getDatabase,
- getError,
getIsEditing,
getIsFormulaExpanded,
- getLoading,
getUser,
} from "../selectors";
import type { BaseDetailFormFields, StubbedDatabase } from "../types";
@@ -38,9 +37,6 @@ const mapStateToProps = (
return {
entity,
metadataFields: fields,
- loading: getLoading(state),
- // naming this 'error' will conflict with redux form
- loadingError: getError(state),
user: getUser(state),
isEditing: getIsEditing(state),
isFormulaExpanded: getIsFormulaExpanded(state),
@@ -63,7 +59,7 @@ interface DatabaseDetailProps {
loading?: boolean;
loadingError?: unknown;
// The action handler in reference.ts types its own props parameter.
- onSubmit: (fields: DatabaseDetailFormFields, props: any) => void;
+ onSubmit: (fields: DatabaseDetailFormFields, props: any) => Promise<void>;
}
const DatabaseDetail = (props: DatabaseDetailProps) => {
@@ -79,6 +75,8 @@ const DatabaseDetail = (props: DatabaseDetailProps) => {
onSubmit,
} = props;
+ const [saveError, setSaveError] = useState<unknown>(null);
+
const {
isSubmitting,
getFieldProps,
@@ -87,8 +85,14 @@ const DatabaseDetail = (props: DatabaseDetailProps) => {
handleReset,
} = useFormik<DatabaseDetailFormFields>({
initialValues: {},
- onSubmit: (fields): void => {
- onSubmit(fields, { ...props, resetForm: handleReset });
+ onSubmit: async (fields): Promise<void> => {
+ setSaveError(null);
+ try {
+ await onSubmit(fields, { ...props, resetForm: handleReset });
+ } catch (error) {
+ console.error(error);
+ setSaveError(error);
+ }
},
});
@@ -123,8 +127,8 @@ const DatabaseDetail = (props: DatabaseDetailProps) => {
nameFormField={getFormField("name")}
/>
<LoadingAndErrorWrapper
- loading={!loadingError && loading}
- error={loadingError}
+ loading={!loadingError && !saveError && (loading || isSubmitting)}
+ error={saveError ?? loadingError}
>
{() => (
<div className={CS.wrapper}>
diff --git a/frontend/src/metabase/reference/databases/DatabaseDetailContainer.tsx b/frontend/src/metabase/reference/databases/DatabaseDetailContainer.tsx
index c1c7d43827e..88fa349c30d 100644
--- a/frontend/src/metabase/reference/databases/DatabaseDetailContainer.tsx
+++ b/frontend/src/metabase/reference/databases/DatabaseDetailContainer.tsx
@@ -8,10 +8,9 @@ import { connect, useSelector } from "metabase/redux";
import { SidebarLayout } from "metabase/reference/components/SidebarLayout";
import DatabaseDetail from "metabase/reference/databases/DatabaseDetail";
import * as actions from "metabase/reference/reference";
-import { useReferenceFetchState } from "metabase/reference/use-reference-fetch-state";
import { useLocation, useParams } from "metabase/router";
-import type { ClearStateProps, FetchProps } from "../reference";
+import type { ClearStateProps } from "../reference";
import {
type ReferenceRouteParams,
getDatabase,
@@ -25,7 +24,7 @@ const mapDispatchToProps = {
...actions,
};
-interface DatabaseDetailContainerProps extends FetchProps, ClearStateProps {}
+type DatabaseDetailContainerProps = ClearStateProps;
function DatabaseDetailContainer(props: DatabaseDetailContainerProps) {
const { pathname } = useLocation();
@@ -40,7 +39,6 @@ function DatabaseDetailContainer(props: DatabaseDetailContainerProps) {
id: databaseId,
skip_fields: true,
});
- useReferenceFetchState({ isFetching, error });
useEffect(() => {
const pathnameChanged =
@@ -56,7 +54,11 @@ function DatabaseDetailContainer(props: DatabaseDetailContainerProps) {
style={isEditing ? { paddingTop: "43px" } : {}}
sidebar={<DatabaseSidebar database={database} />}
>
- <DatabaseDetail params={params} />
+ <DatabaseDetail
+ params={params}
+ loading={isFetching}
+ loadingError={error}
+ />
</SidebarLayout>
);
}
diff --git a/frontend/src/metabase/reference/databases/DatabaseList.tsx b/frontend/src/metabase/reference/databases/DatabaseList.tsx
index 6ae242fe5bf..9541844ed34 100644
--- a/frontend/src/metabase/reference/databases/DatabaseList.tsx
+++ b/frontend/src/metabase/reference/databases/DatabaseList.tsx
@@ -14,7 +14,6 @@ import type { NormalizedDatabase } from "metabase-types/api";
import ReferenceHeader from "../components/ReferenceHeader";
import type { StateWithReference } from "../selectors";
-import { getError, getLoading } from "../selectors";
interface DatabaseListProps {
entities: Record<string, NormalizedDatabase>;
@@ -24,8 +23,6 @@ interface DatabaseListProps {
const mapStateToProps = (state: StateWithReference) => ({
entities: getDatabases(state),
- loading: getLoading(state),
- loadingError: getError(state),
});
class DatabaseList extends Component<DatabaseListProps> {
diff --git a/frontend/src/metabase/reference/databases/DatabaseListContainer.tsx b/frontend/src/metabase/reference/databases/DatabaseListContainer.tsx
index 31bad453a68..1379d2ab9f9 100644
--- a/frontend/src/metabase/reference/databases/DatabaseListContainer.tsx
+++ b/frontend/src/metabase/reference/databases/DatabaseListContainer.tsx
@@ -9,23 +9,21 @@ import { SidebarLayout } from "metabase/reference/components/SidebarLayout";
import DatabaseList from "metabase/reference/databases/DatabaseList";
import BaseSidebar from "metabase/reference/guide/BaseSidebar";
import * as actions from "metabase/reference/reference";
-import { useReferenceFetchState } from "metabase/reference/use-reference-fetch-state";
import { useLocation } from "metabase/router";
-import type { ClearStateProps, FetchProps } from "../reference";
+import type { ClearStateProps } from "../reference";
const mapDispatchToProps = {
...actions,
};
-interface DatabaseListContainerProps extends FetchProps, ClearStateProps {}
+type DatabaseListContainerProps = ClearStateProps;
function DatabaseListContainer(props: DatabaseListContainerProps) {
const { pathname } = useLocation();
const previousPathname = usePrevious(pathname);
const { isFetching, error } = useListDatabasesQuery({ include: "tables" });
- useReferenceFetchState({ isFetching, error });
useEffect(() => {
const pathnameChanged =
@@ -40,7 +38,7 @@ function DatabaseListContainer(props: DatabaseListContainerProps) {
className={cx(CS.flexFull, CS.relative)}
sidebar={<BaseSidebar />}
>
- <DatabaseList />
+ <DatabaseList loading={isFetching} loadingError={error} />
</SidebarLayout>
);
}
diff --git a/frontend/src/metabase/reference/databases/FieldDetail.tsx b/frontend/src/metabase/reference/databases/FieldDetail.tsx
index 4b3acf4fe5b..856b4ff21ed 100644
--- a/frontend/src/metabase/reference/databases/FieldDetail.tsx
+++ b/frontend/src/metabase/reference/databases/FieldDetail.tsx
@@ -1,5 +1,6 @@
import cx from "classnames";
import { useFormik } from "formik";
+import { useState } from "react";
import { t } from "ttag";
import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
@@ -19,17 +20,16 @@ import type { FieldId, User } from "metabase-types/api";
import type { ReferenceRouteProps, StateWithReference } from "../selectors";
import {
getDatabase,
- getError,
getField,
getIsEditing,
getIsFormulaExpanded,
- getLoading,
getTable,
getUser,
} from "../selectors";
import type {
BaseDetailFormFields,
FieldFormFieldsValues,
+ ReferenceLoadingProps,
StubbedDatabase,
StubbedField,
StubbedTable,
@@ -99,9 +99,6 @@ const mapStateToProps = (
field: entity,
table: getTable(state, props),
database: getDatabase(state, props),
- loading: getLoading(state),
- // naming this 'error' will conflict with redux form
- loadingError: getError(state),
user: getUser(state),
isEditing: getIsEditing(state),
isFormulaExpanded: getIsFormulaExpanded(state),
@@ -128,7 +125,7 @@ interface FieldDetailProps {
loadingError?: unknown;
metadata: Metadata;
- onSubmit: (fields: FieldDetailFormFields, props: any) => void;
+ onSubmit: (fields: FieldDetailFormFields, props: any) => Promise<void>;
}
const FieldDetail = (props: FieldDetailProps) => {
@@ -146,6 +143,8 @@ const FieldDetail = (props: FieldDetailProps) => {
onSubmit,
} = props;
+ const [saveError, setSaveError] = useState<unknown>(null);
+
const {
isSubmitting,
getFieldProps,
@@ -154,8 +153,14 @@ const FieldDetail = (props: FieldDetailProps) => {
handleReset,
} = useFormik<FieldDetailFormFields>({
initialValues: {},
- onSubmit: (fields): void => {
- onSubmit(fields, { ...props, resetForm: handleReset });
+ onSubmit: async (fields): Promise<void> => {
+ setSaveError(null);
+ try {
+ await onSubmit(fields, { ...props, resetForm: handleReset });
+ } catch (error) {
+ console.error(error);
+ setSaveError(error);
+ }
},
});
@@ -190,8 +195,8 @@ const FieldDetail = (props: FieldDetailProps) => {
nameFormField={getFormField("name")}
/>
<LoadingAndErrorWrapper
- loading={!loadingError && loading}
- error={loadingError}
+ loading={!loadingError && !saveError && (loading || isSubmitting)}
+ error={saveError ?? loadingError}
>
{() => (
<div className={CS.wrapper}>
@@ -288,7 +293,8 @@ const FieldDetail = (props: FieldDetailProps) => {
// `metadata` is read here but selected by the container. Naming it keeps that
// contract type-checked.
type FieldDetailOwnProps = ReferenceRouteProps &
- Pick<FieldDetailProps, "metadata">;
+ Pick<FieldDetailProps, "metadata"> &
+ ReferenceLoadingProps;
// eslint-disable-next-line import/no-default-export -- deprecated usage
export default connect(
diff --git a/frontend/src/metabase/reference/databases/FieldDetailContainer.tsx b/frontend/src/metabase/reference/databases/FieldDetailContainer.tsx
index f875e3d5455..6b5082c406a 100644
--- a/frontend/src/metabase/reference/databases/FieldDetailContainer.tsx
+++ b/frontend/src/metabase/reference/databases/FieldDetailContainer.tsx
@@ -12,7 +12,7 @@ import { useReferenceFetch } from "metabase/reference/use-reference-fetch-state"
import { useLocation, useParams } from "metabase/router";
import { getMetadata } from "metabase/selectors/metadata";
-import type { ClearStateProps, FetchProps } from "../reference";
+import type { ClearStateProps } from "../reference";
import {
type ReferenceRouteParams,
getDatabase,
@@ -28,7 +28,7 @@ const mapDispatchToProps = {
...actions,
};
-interface FieldDetailContainerProps extends FetchProps, ClearStateProps {}
+type FieldDetailContainerProps = ClearStateProps;
function FieldDetailContainer(props: FieldDetailContainerProps) {
const { pathname } = useLocation();
@@ -44,7 +44,9 @@ function FieldDetailContainer(props: FieldDetailContainerProps) {
// `FieldDetail` reads `metadata` but doesn't select it itself.
const metadata = useSelector(getMetadata);
- useReferenceFetch(() => fetchTableData(dispatch, tableId));
+ const { loading, loadingError } = useReferenceFetch(() =>
+ fetchTableData(dispatch, tableId),
+ );
useEffect(() => {
const pathnameChanged =
@@ -60,7 +62,12 @@ function FieldDetailContainer(props: FieldDetailContainerProps) {
style={isEditing ? { paddingTop: "43px" } : {}}
sidebar={<FieldSidebar database={database} table={table} field={field} />}
>
- <FieldDetail params={params} metadata={metadata} />
+ <FieldDetail
+ params={params}
+ metadata={metadata}
+ loading={loading}
+ loadingError={loadingError}
+ />
</SidebarLayout>
);
}
diff --git a/frontend/src/metabase/reference/databases/FieldList.tsx b/frontend/src/metabase/reference/databases/FieldList.tsx
index 8a980e88db3..4c67b0fd56f 100644
--- a/frontend/src/metabase/reference/databases/FieldList.tsx
+++ b/frontend/src/metabase/reference/databases/FieldList.tsx
@@ -1,5 +1,6 @@
import cx from "classnames";
import { useFormik } from "formik";
+import { useState } from "react";
import { t } from "ttag";
import { EmptyState } from "metabase/common/components/EmptyState";
@@ -24,14 +25,16 @@ import type {
import type { ReferenceRouteProps, StateWithReference } from "../selectors";
import {
- getError,
getFieldsByTable,
getIsEditing,
- getLoading,
getTable,
getUser,
} from "../selectors";
-import type { FieldFormFieldsValues, StubbedTable } from "../types";
+import type {
+ FieldFormFieldsValues,
+ ReferenceLoadingProps,
+ StubbedTable,
+} from "../types";
type FieldListFormFields = Record<string, FieldFormFieldsValues>;
@@ -50,8 +53,6 @@ const mapStateToProps = (
return {
table: getTable(state, props),
entities: data,
- loading: getLoading(state),
- loadingError: getError(state),
user: getUser(state),
isEditing: getIsEditing(state),
};
@@ -97,6 +98,8 @@ const FieldList = (props: FieldListProps) => {
onSubmit,
} = props;
+ const [saveError, setSaveError] = useState<unknown>(null);
+
const {
isSubmitting,
getFieldProps,
@@ -105,8 +108,14 @@ const FieldList = (props: FieldListProps) => {
handleReset,
} = useFormik<FieldListFormFields>({
initialValues: {},
- onSubmit: (fields): void => {
- onSubmit(entities, fields, { ...props, resetForm: handleReset });
+ onSubmit: async (fields): Promise<void> => {
+ setSaveError(null);
+ try {
+ await onSubmit(entities, fields, { ...props, resetForm: handleReset });
+ } catch (error) {
+ console.error(error);
+ setSaveError(error);
+ }
},
});
@@ -147,8 +156,8 @@ const FieldList = (props: FieldListProps) => {
startEditing={startEditing}
/>
<LoadingAndErrorWrapper
- loading={!loadingError && loading}
- error={loadingError}
+ loading={!loadingError && !saveError && (loading || isSubmitting)}
+ error={saveError ?? loadingError}
>
{() =>
Object.keys(entities).length > 0 ? (
@@ -220,4 +229,11 @@ export default connect(
mapStateToProps,
mapDispatchToProps,
// Unjustified type cast. FIXME
-)(FieldList as unknown as React.ComponentType);
+)(
+ // `connect` cannot match its inferred props against this component's own
+ // props, because the `actions` spread in `mapDispatchToProps` is untyped.
+ // The cast restores the props a caller actually passes.
+ FieldList as unknown as React.ComponentType<
+ ReferenceRouteProps & ReferenceLoadingProps
+ >,
+);
diff --git a/frontend/src/metabase/reference/databases/FieldListContainer.tsx b/frontend/src/metabase/reference/databases/FieldListContainer.tsx
index 361b70f854b..1ecf096fbc2 100644
--- a/frontend/src/metabase/reference/databases/FieldListContainer.tsx
+++ b/frontend/src/metabase/reference/databases/FieldListContainer.tsx
@@ -11,7 +11,7 @@ import * as actions from "metabase/reference/reference";
import { useReferenceFetch } from "metabase/reference/use-reference-fetch-state";
import { useLocation, useParams } from "metabase/router";
-import type { ClearStateProps, FetchProps } from "../reference";
+import type { ClearStateProps } from "../reference";
import {
type ReferenceRouteParams,
getDatabase,
@@ -26,7 +26,7 @@ const mapDispatchToProps = {
...actions,
};
-interface FieldListContainerProps extends FetchProps, ClearStateProps {}
+type FieldListContainerProps = ClearStateProps;
function FieldListContainer(props: FieldListContainerProps) {
const { pathname } = useLocation();
@@ -39,7 +39,9 @@ function FieldListContainer(props: FieldListContainerProps) {
const tableId = useSelector((state) => getTableId(state, { params }));
const isEditing = useSelector(getIsEditing);
- useReferenceFetch(() => fetchTableData(dispatch, tableId));
+ const { loading, loadingError } = useReferenceFetch(() =>
+ fetchTableData(dispatch, tableId),
+ );
useEffect(() => {
const pathnameChanged =
@@ -55,7 +57,11 @@ function FieldListContainer(props: FieldListContainerProps) {
style={isEditing ? { paddingTop: "43px" } : {}}
sidebar={<TableSidebar database={database} table={table} />}
>
- <FieldList params={params} />
+ <FieldList
+ params={params}
+ loading={loading}
+ loadingError={loadingError}
+ />
</SidebarLayout>
);
}
diff --git a/frontend/src/metabase/reference/databases/TableDetail.tsx b/frontend/src/metabase/reference/databases/TableDetail.tsx
index 7e114e48e99..36f009825fe 100644
--- a/frontend/src/metabase/reference/databases/TableDetail.tsx
+++ b/frontend/src/metabase/reference/databases/TableDetail.tsx
@@ -1,5 +1,6 @@
import cx from "classnames";
import { useFormik } from "formik";
+import { useState } from "react";
import { t } from "ttag";
import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
@@ -21,15 +22,17 @@ import type { User } from "metabase-types/api";
import type { ReferenceRouteProps, StateWithReference } from "../selectors";
import {
- getError,
getHasSingleSchema,
getIsEditing,
getIsFormulaExpanded,
- getLoading,
getTable,
getUser,
} from "../selectors";
-import type { BaseDetailFormFields, StubbedTable } from "../types";
+import type {
+ BaseDetailFormFields,
+ ReferenceLoadingProps,
+ StubbedTable,
+} from "../types";
import { getQuestionUrl } from "../utils";
interface TableDetailFormFields extends BaseDetailFormFields {
@@ -72,9 +75,6 @@ const mapStateToProps = (
table: getTable(state, props),
metadataFields: fields,
metadata: getMetadata(state),
- loading: getLoading(state),
- // naming this 'error' will conflict with redux form
- loadingError: getError(state),
user: getUser(state),
isEditing: getIsEditing(state),
hasSingleSchema: getHasSingleSchema(state, props),
@@ -101,7 +101,7 @@ interface TableDetailProps {
loadingError?: unknown;
metadata: Metadata;
- onSubmit: (fields: TableDetailFormFields, props: any) => void;
+ onSubmit: (fields: TableDetailFormFields, props: any) => Promise<void>;
}
const TableDetail = (props: TableDetailProps) => {
@@ -120,6 +120,8 @@ const TableDetail = (props: TableDetailProps) => {
onSubmit,
} = props;
+ const [saveError, setSaveError] = useState<unknown>(null);
+
const {
isSubmitting,
getFieldProps,
@@ -128,8 +130,14 @@ const TableDetail = (props: TableDetailProps) => {
handleReset,
} = useFormik<TableDetailFormFields>({
initialValues: {},
- onSubmit: (fields): void => {
- onSubmit(fields, { ...props, resetForm: handleReset });
+ onSubmit: async (fields): Promise<void> => {
+ setSaveError(null);
+ try {
+ await onSubmit(fields, { ...props, resetForm: handleReset });
+ } catch (error) {
+ console.error(error);
+ setSaveError(error);
+ }
},
});
@@ -169,8 +177,8 @@ const TableDetail = (props: TableDetailProps) => {
nameFormField={getFormField("name")}
/>
<LoadingAndErrorWrapper
- loading={!loadingError && loading}
- error={loadingError}
+ loading={!loadingError && !saveError && (loading || isSubmitting)}
+ error={saveError ?? loadingError}
>
{() => (
<div className={CS.wrapper}>
@@ -244,4 +252,11 @@ export default connect(
mapStateToProps,
mapDispatchToProps,
// Unjustified type cast. FIXME
-)(TableDetail as unknown as React.ComponentType);
+)(
+ // `connect` cannot match its inferred props against this component's own
+ // props, because the `actions` spread in `mapDispatchToProps` is untyped.
+ // The cast restores the props a caller actually passes.
+ TableDetail as unknown as React.ComponentType<
+ ReferenceRouteProps & ReferenceLoadingProps
+ >,
+);
diff --git a/frontend/src/metabase/reference/databases/TableDetailContainer.tsx b/frontend/src/metabase/reference/databases/TableDetailContainer.tsx
index 0380f17b722..955cda89a20 100644
--- a/frontend/src/metabase/reference/databases/TableDetailContainer.tsx
+++ b/frontend/src/metabase/reference/databases/TableDetailContainer.tsx
@@ -8,10 +8,9 @@ import { connect, useSelector } from "metabase/redux";
import { SidebarLayout } from "metabase/reference/components/SidebarLayout";
import TableDetail from "metabase/reference/databases/TableDetail";
import * as actions from "metabase/reference/reference";
-import { useReferenceFetchState } from "metabase/reference/use-reference-fetch-state";
import { useLocation, useParams } from "metabase/router";
-import type { ClearStateProps, FetchProps } from "../reference";
+import type { ClearStateProps } from "../reference";
import {
type ReferenceRouteParams,
getDatabase,
@@ -26,7 +25,7 @@ const mapDispatchToProps = {
...actions,
};
-interface TableDetailContainerProps extends FetchProps, ClearStateProps {}
+type TableDetailContainerProps = ClearStateProps;
function TableDetailContainer(props: TableDetailContainerProps) {
const { pathname } = useLocation();
@@ -42,7 +41,6 @@ function TableDetailContainer(props: TableDetailContainerProps) {
id: databaseId,
skip_fields: true,
});
- useReferenceFetchState({ isFetching, error });
useEffect(() => {
const pathnameChanged =
@@ -58,7 +56,7 @@ function TableDetailContainer(props: TableDetailContainerProps) {
style={isEditing ? { paddingTop: "43px" } : {}}
sidebar={<TableSidebar database={database} table={table} />}
>
- <TableDetail params={params} />
+ <TableDetail params={params} loading={isFetching} loadingError={error} />
</SidebarLayout>
);
}
diff --git a/frontend/src/metabase/reference/databases/TableList.tsx b/frontend/src/metabase/reference/databases/TableList.tsx
index 3fd9d2cb28d..672cb16e85d 100644
--- a/frontend/src/metabase/reference/databases/TableList.tsx
+++ b/frontend/src/metabase/reference/databases/TableList.tsx
@@ -16,11 +16,10 @@ import ReferenceHeader from "../components/ReferenceHeader";
import type { ReferenceRouteProps, StateWithReference } from "../selectors";
import {
getDatabase,
- getError,
getHasSingleSchema,
- getLoading,
getTablesByDatabase,
} from "../selectors";
+import type { ReferenceLoadingProps } from "../types";
const emptyStateData = {
get message() {
@@ -36,8 +35,6 @@ const mapStateToProps = (
database: getDatabase(state, props),
entities: getTablesByDatabase(state, props),
hasSingleSchema: getHasSingleSchema(state, props),
- loading: getLoading(state),
- loadingError: getError(state),
});
interface TableLike {
@@ -149,4 +146,11 @@ class TableList extends Component<TableListProps> {
export default connect(
mapStateToProps,
// Unjustified type cast. FIXME
-)(TableList as unknown as React.ComponentType);
+)(
+ // `connect` cannot match its inferred props against this component's own
+ // props, because the `actions` spread in `mapDispatchToProps` is untyped.
+ // The cast restores the props a caller actually passes.
+ TableList as unknown as React.ComponentType<
+ ReferenceRouteProps & ReferenceLoadingProps
+ >,
+);
diff --git a/frontend/src/metabase/reference/databases/TableListContainer.tsx b/frontend/src/metabase/reference/databases/TableListContainer.tsx
index 964638e13c0..739f1aef4ce 100644
--- a/frontend/src/metabase/reference/databases/TableListContainer.tsx
+++ b/frontend/src/metabase/reference/databases/TableListContainer.tsx
@@ -8,10 +8,9 @@ import { connect, useSelector } from "metabase/redux";
import { SidebarLayout } from "metabase/reference/components/SidebarLayout";
import TableList from "metabase/reference/databases/TableList";
import * as actions from "metabase/reference/reference";
-import { useReferenceFetchState } from "metabase/reference/use-reference-fetch-state";
import { useLocation, useParams } from "metabase/router";
-import type { ClearStateProps, FetchProps } from "../reference";
+import type { ClearStateProps } from "../reference";
import {
type ReferenceRouteParams,
getDatabase,
@@ -25,7 +24,7 @@ const mapDispatchToProps = {
...actions,
};
-interface TableListContainerProps extends FetchProps, ClearStateProps {}
+type TableListContainerProps = ClearStateProps;
function TableListContainer(props: TableListContainerProps) {
const { pathname } = useLocation();
@@ -40,7 +39,6 @@ function TableListContainer(props: TableListContainerProps) {
id: databaseId,
skip_fields: true,
});
- useReferenceFetchState({ isFetching, error });
useEffect(() => {
const pathnameChanged =
@@ -56,7 +54,7 @@ function TableListContainer(props: TableListContainerProps) {
style={isEditing ? { paddingTop: "43px" } : {}}
sidebar={<DatabaseSidebar database={database} />}
>
- <TableList params={params} />
+ <TableList params={params} loading={isFetching} loadingError={error} />
</SidebarLayout>
);
}
diff --git a/frontend/src/metabase/reference/databases/TableQuestions.tsx b/frontend/src/metabase/reference/databases/TableQuestions.tsx
index 15f8c636e1c..6cc4496c483 100644
--- a/frontend/src/metabase/reference/databases/TableQuestions.tsx
+++ b/frontend/src/metabase/reference/databases/TableQuestions.tsx
@@ -18,13 +18,8 @@ import type { Card } from "metabase-types/api";
import ReferenceHeader from "../components/ReferenceHeader";
import type { ReferenceRouteProps, StateWithReference } from "../selectors";
-import {
- getError,
- getLoading,
- getTable,
- getTableQuestions,
-} from "../selectors";
-import type { StubbedTable } from "../types";
+import { getTable, getTableQuestions } from "../selectors";
+import type { ReferenceLoadingProps, StubbedTable } from "../types";
import { getQuestionUrl } from "../utils";
const emptyStateData = (table: StubbedTable, metadata: Metadata) => {
@@ -46,8 +41,6 @@ const mapStateToProps = (
) => ({
table: getTable(state, props),
entities: getTableQuestions(state, props),
- loading: getLoading(state),
- loadingError: getError(state),
metadata: getMetadata(state),
});
@@ -112,4 +105,11 @@ class TableQuestions extends Component<TableQuestionsProps> {
export default connect(
mapStateToProps,
// Unjustified type cast. FIXME
-)(TableQuestions as unknown as React.ComponentType);
+)(
+ // `connect` cannot match its inferred props against this component's own
+ // props, because the `actions` spread in `mapDispatchToProps` is untyped.
+ // The cast restores the props a caller actually passes.
+ TableQuestions as unknown as React.ComponentType<
+ ReferenceRouteProps & ReferenceLoadingProps
+ >,
+);
diff --git a/frontend/src/metabase/reference/databases/TableQuestionsContainer.tsx b/frontend/src/metabase/reference/databases/TableQuestionsContainer.tsx
index 3519b7df3c4..84693a9e3c7 100644
--- a/frontend/src/metabase/reference/databases/TableQuestionsContainer.tsx
+++ b/frontend/src/metabase/reference/databases/TableQuestionsContainer.tsx
@@ -8,10 +8,9 @@ import { connect, useSelector } from "metabase/redux";
import { SidebarLayout } from "metabase/reference/components/SidebarLayout";
import TableQuestions from "metabase/reference/databases/TableQuestions";
import * as actions from "metabase/reference/reference";
-import { useReferenceFetchState } from "metabase/reference/use-reference-fetch-state";
import { useLocation, useParams } from "metabase/router";
-import type { ClearStateProps, FetchProps } from "../reference";
+import type { ClearStateProps } from "../reference";
import {
type ReferenceRouteParams,
getDatabase,
@@ -26,7 +25,7 @@ const mapDispatchToProps = {
...actions,
};
-interface TableQuestionsContainerProps extends FetchProps, ClearStateProps {}
+type TableQuestionsContainerProps = ClearStateProps;
function TableQuestionsContainer(props: TableQuestionsContainerProps) {
const { pathname } = useLocation();
@@ -43,10 +42,6 @@ function TableQuestionsContainer(props: TableQuestionsContainerProps) {
const { isFetching: isFetchingCards, error: cardsError } = useListCardsQuery(
{},
);
- useReferenceFetchState({
- isFetching: isFetchingMetadata || isFetchingCards,
- error: metadataError ?? cardsError,
- });
useEffect(() => {
const pathnameChanged =
@@ -62,7 +57,11 @@ function TableQuestionsContainer(props: TableQuestionsContainerProps) {
style={isEditing ? { paddingTop: "43px" } : {}}
sidebar={<TableSidebar database={database} table={table} />}
>
- <TableQuestions params={params} />
+ <TableQuestions
+ params={params}
+ loading={isFetchingMetadata || isFetchingCards}
+ loadingError={metadataError ?? cardsError}
+ />
</SidebarLayout>
);
}
diff --git a/frontend/src/metabase/reference/reference.ts b/frontend/src/metabase/reference/reference.ts
index c6e2c92fb2f..920b87eb7fb 100644
--- a/frontend/src/metabase/reference/reference.ts
+++ b/frontend/src/metabase/reference/reference.ts
@@ -4,10 +4,6 @@ import { createAction, handleActions } from "metabase/redux";
import { filterUntouchedFields, isEmptyObject } from "./utils";
-export const SET_ERROR = "metabase/reference/SET_ERROR";
-export const CLEAR_ERROR = "metabase/reference/CLEAR_ERROR";
-export const START_LOADING = "metabase/reference/START_LOADING";
-export const END_LOADING = "metabase/reference/END_LOADING";
export const START_EDITING = "metabase/reference/START_EDITING";
export const END_EDITING = "metabase/reference/END_EDITING";
export const EXPAND_FORMULA = "metabase/reference/EXPAND_FORMULA";
@@ -15,14 +11,6 @@ export const COLLAPSE_FORMULA = "metabase/reference/COLLAPSE_FORMULA";
export const SHOW_DASHBOARD_MODAL = "metabase/reference/SHOW_DASHBOARD_MODAL";
export const HIDE_DASHBOARD_MODAL = "metabase/reference/HIDE_DASHBOARD_MODAL";
-export const setError = createAction(SET_ERROR);
-
-export const clearError = createAction(CLEAR_ERROR);
-
-export const startLoading = createAction(START_LOADING);
-
-export const endLoading = createAction(END_LOADING);
-
export const startEditing = createAction(START_EDITING);
export const endEditing = createAction(END_EDITING);
@@ -36,14 +24,7 @@ export const showDashboardModal = createAction(SHOW_DASHBOARD_MODAL);
export const hideDashboardModal = createAction(HIDE_DASHBOARD_MODAL);
-export interface FetchProps {
- clearError: () => void;
- startLoading: () => void;
- endLoading: () => void;
- setError: (error: unknown) => void;
-}
-
-interface UpdateProps extends FetchProps {
+interface UpdateProps {
resetForm: () => void;
endEditing: () => void;
entity: Record<string, unknown>;
@@ -51,8 +32,6 @@ interface UpdateProps extends FetchProps {
export interface ClearStateProps {
endEditing: () => void;
- endLoading: () => void;
- clearError: () => void;
collapseFormula: () => void;
}
@@ -68,39 +47,32 @@ interface UpdateEntityProps extends UpdateProps {
// components where the old code re-used the same component
export const clearState = (props: ClearStateProps) => {
props.endEditing();
- props.endLoading();
- props.clearError();
props.collapseFormula();
};
// This is called on the success or failure of a form triggered update
const resetForm = (props: UpdateProps) => {
props.resetForm();
- props.endLoading();
props.endEditing();
};
-// Update actions. These drive the same loading state during a save, through
-// props rather than dispatch. Fetching goes through `useReferenceFetch`.
+// Update actions. Failures reach the caller, which reports them next to the
+// form. Progress is formik's `isSubmitting`.
const updateDataWrapper = (
props: UpdateProps,
fn: (entity: Record<string, unknown>) => Promise<unknown>,
) => {
return async (fields: Record<string, unknown>) => {
- props.clearError();
- props.startLoading();
try {
const editedFields = filterUntouchedFields(fields, props.entity);
if (!isEmptyObject(editedFields)) {
const newEntity = { ...props.entity, ...editedFields };
await fn(newEntity);
}
- } catch (error) {
- console.error(error);
- props.setError(error);
+ } finally {
+ resetForm(props);
}
- resetForm(props);
};
};
@@ -145,7 +117,6 @@ export const rUpdateFields = (
props: UpdateFieldsProps,
) => {
return async () => {
- props.startLoading();
try {
const updatedFields = Object.keys(formFields)
.map((fieldId) => ({
@@ -159,26 +130,19 @@ export const rUpdateFields = (
.map(({ field, formField }) => ({ ...field, ...formField }));
await Promise.all(updatedFields.map(props.updateField));
- } catch (error) {
- props.setError(error);
- console.error(error);
+ } finally {
+ resetForm(props);
}
-
- resetForm(props);
};
};
interface ReferenceState {
- error: unknown;
- isLoading: boolean;
isEditing: boolean;
isFormulaExpanded: boolean;
isDashboardModalOpen: boolean;
}
const initialState: ReferenceState = {
- error: null,
- isLoading: false,
isEditing: false,
isFormulaExpanded: false,
isDashboardModalOpen: false,
@@ -186,19 +150,6 @@ const initialState: ReferenceState = {
// eslint-disable-next-line import/no-default-export -- deprecated usage
export default handleActions(
{
- [SET_ERROR]: {
- throw: (state: ReferenceState, { payload }: { payload: unknown }) =>
- assoc(state, "error", payload),
- },
- [CLEAR_ERROR]: {
- next: (state: ReferenceState) => assoc(state, "error", null),
- },
- [START_LOADING]: {
- next: (state: ReferenceState) => assoc(state, "isLoading", true),
- },
- [END_LOADING]: {
- next: (state: ReferenceState) => assoc(state, "isLoading", false),
- },
[START_EDITING]: {
next: (state: ReferenceState) => assoc(state, "isEditing", true),
},
diff --git a/frontend/src/metabase/reference/segments/SegmentDetail.tsx b/frontend/src/metabase/reference/segments/SegmentDetail.tsx
index 59580e0b4de..7e5133381e0 100644
--- a/frontend/src/metabase/reference/segments/SegmentDetail.tsx
+++ b/frontend/src/metabase/reference/segments/SegmentDetail.tsx
@@ -1,5 +1,6 @@
import cx from "classnames";
import { useFormik } from "formik";
+import { useState } from "react";
import { t } from "ttag";
import { Link } from "metabase/common/components/Link";
@@ -25,16 +26,15 @@ import type { User } from "metabase-types/api";
import S from "../components/Detail.module.css";
import type { ReferenceRouteProps, StateWithReference } from "../selectors";
import {
- getError,
getIsEditing,
getIsFormulaExpanded,
- getLoading,
getSegment,
getTable,
getUser,
} from "../selectors";
import type {
BaseDetailFormFields,
+ ReferenceLoadingProps,
StubbedSegment,
StubbedTable,
} from "../types";
@@ -86,9 +86,6 @@ const mapStateToProps = (
table: getTable(state, props),
metadataFields: fields,
metadata: getMetadata(state),
- loading: getLoading(state),
- // naming this 'error' will conflict with redux form
- loadingError: getError(state),
user: getUser(state),
isEditing: getIsEditing(state),
isFormulaExpanded: getIsFormulaExpanded(state),
@@ -121,7 +118,7 @@ interface SegmentDetailProps {
loadingError?: unknown;
metadata: Metadata;
- onSubmit: (fields: SegmentDetailFormFields, props: any) => void;
+ onSubmit: (fields: SegmentDetailFormFields, props: any) => Promise<void>;
}
const SegmentDetail = (props: SegmentDetailProps) => {
@@ -142,6 +139,8 @@ const SegmentDetail = (props: SegmentDetailProps) => {
onSubmit,
} = props;
+ const [saveError, setSaveError] = useState<unknown>(null);
+
const {
isSubmitting,
getFieldProps,
@@ -152,8 +151,14 @@ const SegmentDetail = (props: SegmentDetailProps) => {
validate,
initialValues: {},
initialErrors: validate({}),
- onSubmit: (fields): void => {
- onSubmit(fields, { ...props, resetForm: handleReset });
+ onSubmit: async (fields): Promise<void> => {
+ setSaveError(null);
+ try {
+ await onSubmit(fields, { ...props, resetForm: handleReset });
+ } catch (error) {
+ console.error(error);
+ setSaveError(error);
+ }
},
});
@@ -196,8 +201,8 @@ const SegmentDetail = (props: SegmentDetailProps) => {
/>
)}
<LoadingAndErrorWrapper
- loading={!loadingError && loading}
- error={loadingError}
+ loading={!loadingError && !saveError && (loading || isSubmitting)}
+ error={saveError ?? loadingError}
>
{() => (
<div className={CS.wrapper}>
@@ -304,4 +309,11 @@ export default connect(
mapStateToProps,
mapDispatchToProps,
// Unjustified type cast. FIXME
-)(SegmentDetail as unknown as React.ComponentType);
+)(
+ // `connect` cannot match its inferred props against this component's own
+ // props, because the `actions` spread in `mapDispatchToProps` is untyped.
+ // The cast restores the props a caller actually passes.
+ SegmentDetail as unknown as React.ComponentType<
+ ReferenceRouteProps & ReferenceLoadingProps
+ >,
+);
diff --git a/frontend/src/metabase/reference/segments/SegmentDetailContainer.tsx b/frontend/src/metabase/reference/segments/SegmentDetailContainer.tsx
index d0865078114..fe17cc8ff1a 100644
--- a/frontend/src/metabase/reference/segments/SegmentDetailContainer.tsx
+++ b/frontend/src/metabase/reference/segments/SegmentDetailContainer.tsx
@@ -11,7 +11,7 @@ import SegmentDetail from "metabase/reference/segments/SegmentDetail";
import { useReferenceFetch } from "metabase/reference/use-reference-fetch-state";
import { useLocation, useParams } from "metabase/router";
-import type { ClearStateProps, FetchProps } from "../reference";
+import type { ClearStateProps } from "../reference";
import {
type ReferenceRouteParams,
getIsEditing,
@@ -26,7 +26,7 @@ const mapDispatchToProps = {
...actions,
};
-interface SegmentDetailContainerProps extends FetchProps, ClearStateProps {}
+type SegmentDetailContainerProps = ClearStateProps;
function SegmentDetailContainer(props: SegmentDetailContainerProps) {
const { pathname } = useLocation();
@@ -39,7 +39,9 @@ function SegmentDetailContainer(props: SegmentDetailContainerProps) {
const segmentId = useSelector((state) => getSegmentId(state, { params }));
const isEditing = useSelector(getIsEditing);
- useReferenceFetch(() => fetchSegmentDetailData(dispatch, segmentId));
+ const { loading, loadingError } = useReferenceFetch(() =>
+ fetchSegmentDetailData(dispatch, segmentId),
+ );
useEffect(() => {
const pathnameChanged =
@@ -55,7 +57,11 @@ function SegmentDetailContainer(props: SegmentDetailContainerProps) {
style={isEditing ? { paddingTop: "43px" } : {}}
sidebar={<SegmentSidebar segment={segment} user={user} />}
>
- <SegmentDetail params={params} />
+ <SegmentDetail
+ params={params}
+ loading={loading}
+ loadingError={loadingError}
+ />
</SidebarLayout>
);
}
diff --git a/frontend/src/metabase/reference/segments/SegmentFieldDetail.tsx b/frontend/src/metabase/reference/segments/SegmentFieldDetail.tsx
index 1bf3e547359..66a1a7aed3c 100644
--- a/frontend/src/metabase/reference/segments/SegmentFieldDetail.tsx
+++ b/frontend/src/metabase/reference/segments/SegmentFieldDetail.tsx
@@ -1,5 +1,6 @@
import cx from "classnames";
import { useFormik } from "formik";
+import { useState } from "react";
import { t } from "ttag";
import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
@@ -20,17 +21,16 @@ import type { FieldId, User } from "metabase-types/api";
import type { ReferenceRouteProps, StateWithReference } from "../selectors";
import {
- getError,
getFieldBySegment,
getIsEditing,
getIsFormulaExpanded,
- getLoading,
getTable,
getUser,
} from "../selectors";
import type {
BaseDetailFormFields,
FieldFormFieldsValues,
+ ReferenceLoadingProps,
StubbedField,
StubbedTable,
} from "../types";
@@ -84,9 +84,6 @@ const mapStateToProps = (
return {
entity,
table: getTable(state, props),
- loading: getLoading(state),
- // naming this 'error' will conflict with redux form
- loadingError: getError(state),
user: getUser(state),
isEditing: getIsEditing(state),
isFormulaExpanded: getIsFormulaExpanded(state),
@@ -112,7 +109,7 @@ interface SegmentFieldDetailProps {
loadingError?: unknown;
metadata: Metadata;
- onSubmit: (fields: SegmentFieldDetailFormFields, props: any) => void;
+ onSubmit: (fields: SegmentFieldDetailFormFields, props: any) => Promise<void>;
}
const SegmentFieldDetail = (props: SegmentFieldDetailProps) => {
@@ -130,6 +127,8 @@ const SegmentFieldDetail = (props: SegmentFieldDetailProps) => {
onSubmit,
} = props;
+ const [saveError, setSaveError] = useState<unknown>(null);
+
const {
isSubmitting,
getFieldProps,
@@ -138,8 +137,14 @@ const SegmentFieldDetail = (props: SegmentFieldDetailProps) => {
handleReset,
} = useFormik<SegmentFieldDetailFormFields>({
initialValues: {},
- onSubmit: (fields): void => {
- onSubmit(fields, { ...props, resetForm: handleReset });
+ onSubmit: async (fields): Promise<void> => {
+ setSaveError(null);
+ try {
+ await onSubmit(fields, { ...props, resetForm: handleReset });
+ } catch (error) {
+ console.error(error);
+ setSaveError(error);
+ }
},
});
@@ -174,8 +179,8 @@ const SegmentFieldDetail = (props: SegmentFieldDetailProps) => {
nameFormField={getFormField("name")}
/>
<LoadingAndErrorWrapper
- loading={!loadingError && loading}
- error={loadingError}
+ loading={!loadingError && !saveError && (loading || isSubmitting)}
+ error={saveError ?? loadingError}
>
{() => (
<div className={CS.wrapper}>
@@ -259,4 +264,11 @@ export default connect(
mapStateToProps,
mapDispatchToProps,
// Unjustified type cast. FIXME
-)(SegmentFieldDetail as unknown as React.ComponentType);
+)(
+ // `connect` cannot match its inferred props against this component's own
+ // props, because the `actions` spread in `mapDispatchToProps` is untyped.
+ // The cast restores the props a caller actually passes.
+ SegmentFieldDetail as unknown as React.ComponentType<
+ ReferenceRouteProps & ReferenceLoadingProps
+ >,
+);
diff --git a/frontend/src/metabase/reference/segments/SegmentFieldDetailContainer.tsx b/frontend/src/metabase/reference/segments/SegmentFieldDetailContainer.tsx
index 2410981e621..4804547af5f 100644
--- a/frontend/src/metabase/reference/segments/SegmentFieldDetailContainer.tsx
+++ b/frontend/src/metabase/reference/segments/SegmentFieldDetailContainer.tsx
@@ -11,7 +11,7 @@ import SegmentFieldDetail from "metabase/reference/segments/SegmentFieldDetail";
import { useReferenceFetch } from "metabase/reference/use-reference-fetch-state";
import { useLocation, useParams } from "metabase/router";
-import type { ClearStateProps, FetchProps } from "../reference";
+import type { ClearStateProps } from "../reference";
import {
type ReferenceRouteParams,
getField,
@@ -26,8 +26,7 @@ const mapDispatchToProps = {
...actions,
};
-interface SegmentFieldDetailContainerProps
- extends FetchProps, ClearStateProps {}
+type SegmentFieldDetailContainerProps = ClearStateProps;
function SegmentFieldDetailContainer(props: SegmentFieldDetailContainerProps) {
const { pathname } = useLocation();
@@ -40,7 +39,9 @@ function SegmentFieldDetailContainer(props: SegmentFieldDetailContainerProps) {
const field = useSelector((state) => getField(state, { params }));
const isEditing = useSelector(getIsEditing);
- useReferenceFetch(() => fetchSegmentFieldsData(dispatch, segmentId));
+ const { loading, loadingError } = useReferenceFetch(() =>
+ fetchSegmentFieldsData(dispatch, segmentId),
+ );
useEffect(() => {
const pathnameChanged =
@@ -56,7 +57,11 @@ function SegmentFieldDetailContainer(props: SegmentFieldDetailContainerProps) {
style={isEditing ? { paddingTop: "43px" } : {}}
sidebar={<SegmentFieldSidebar segment={segment} field={field} />}
>
- <SegmentFieldDetail params={params} />
+ <SegmentFieldDetail
+ params={params}
+ loading={loading}
+ loadingError={loadingError}
+ />
</SidebarLayout>
);
}
diff --git a/frontend/src/metabase/reference/segments/SegmentFieldList.tsx b/frontend/src/metabase/reference/segments/SegmentFieldList.tsx
index cea0278a65d..12545922d07 100644
--- a/frontend/src/metabase/reference/segments/SegmentFieldList.tsx
+++ b/frontend/src/metabase/reference/segments/SegmentFieldList.tsx
@@ -1,5 +1,6 @@
import cx from "classnames";
import { useFormik } from "formik";
+import { useState } from "react";
import { t } from "ttag";
import { EmptyState } from "metabase/common/components/EmptyState";
@@ -26,10 +27,8 @@ import type {
import type { ReferenceRouteProps, StateWithReference } from "../selectors";
import {
- getError,
getFieldsBySegment,
getIsEditing,
- getLoading,
getSegment,
getUser,
} from "../selectors";
@@ -56,8 +55,6 @@ const mapStateToProps = (
return {
segment: getSegment(state, props),
entities: data,
- loading: getLoading(state),
- loadingError: getError(state),
user: getUser(state),
isEditing: getIsEditing(state),
};
@@ -103,6 +100,8 @@ const SegmentFieldList = (props: SegmentFieldListProps) => {
onSubmit,
} = props;
+ const [saveError, setSaveError] = useState<unknown>(null);
+
const {
isSubmitting,
getFieldProps,
@@ -111,8 +110,17 @@ const SegmentFieldList = (props: SegmentFieldListProps) => {
handleReset,
} = useFormik<SegmentFieldListFormFields>({
initialValues: {},
- onSubmit: (fields): void => {
- onSubmit?.(entities, fields, { ...props, resetForm: handleReset });
+ onSubmit: async (fields): Promise<void> => {
+ setSaveError(null);
+ try {
+ await onSubmit?.(entities, fields, {
+ ...props,
+ resetForm: handleReset,
+ });
+ } catch (error) {
+ console.error(error);
+ setSaveError(error);
+ }
},
});
@@ -148,8 +156,8 @@ const SegmentFieldList = (props: SegmentFieldListProps) => {
startEditing={startEditing}
/>
<LoadingAndErrorWrapper
- loading={!loadingError && loading}
- error={loadingError}
+ loading={!loadingError && !saveError && (loading || isSubmitting)}
+ error={saveError ?? loadingError}
>
{() =>
Object.keys(entities).length > 0 ? (
@@ -215,7 +223,7 @@ const SegmentFieldList = (props: SegmentFieldListProps) => {
// `table` is read here but selected by the container. Naming it keeps that
// contract type-checked.
type SegmentFieldListOwnProps = ReferenceRouteProps &
- Pick<SegmentFieldListProps, "table">;
+ Pick<SegmentFieldListProps, "table" | "loading" | "loadingError">;
// eslint-disable-next-line import/no-default-export -- deprecated usage
export default connect(
diff --git a/frontend/src/metabase/reference/segments/SegmentFieldListContainer.tsx b/frontend/src/metabase/reference/segments/SegmentFieldListContainer.tsx
index 7a8e2f2980d..24a9762cf77 100644
--- a/frontend/src/metabase/reference/segments/SegmentFieldListContainer.tsx
+++ b/frontend/src/metabase/reference/segments/SegmentFieldListContainer.tsx
@@ -11,7 +11,7 @@ import SegmentFieldList from "metabase/reference/segments/SegmentFieldList";
import { useReferenceFetch } from "metabase/reference/use-reference-fetch-state";
import { useLocation, useParams } from "metabase/router";
-import type { ClearStateProps, FetchProps } from "../reference";
+import type { ClearStateProps } from "../reference";
import {
type ReferenceRouteParams,
getIsEditing,
@@ -27,7 +27,7 @@ const mapDispatchToProps = {
...actions,
};
-interface SegmentFieldListContainerProps extends FetchProps, ClearStateProps {}
+type SegmentFieldListContainerProps = ClearStateProps;
function SegmentFieldListContainer(props: SegmentFieldListContainerProps) {
const { pathname } = useLocation();
@@ -42,7 +42,9 @@ function SegmentFieldListContainer(props: SegmentFieldListContainerProps) {
// `SegmentFieldList` reads `table.db_id` but doesn't select the table itself.
const table = useSelector((state) => getTable(state, { params }));
- useReferenceFetch(() => fetchSegmentFieldsData(dispatch, segmentId));
+ const { loading, loadingError } = useReferenceFetch(() =>
+ fetchSegmentFieldsData(dispatch, segmentId),
+ );
useEffect(() => {
const pathnameChanged =
@@ -58,7 +60,12 @@ function SegmentFieldListContainer(props: SegmentFieldListContainerProps) {
style={isEditing ? { paddingTop: "43px" } : {}}
sidebar={<SegmentSidebar segment={segment} user={user} />}
>
- <SegmentFieldList params={params} table={table} />
+ <SegmentFieldList
+ params={params}
+ table={table}
+ loading={loading}
+ loadingError={loadingError}
+ />
</SidebarLayout>
);
}
diff --git a/frontend/src/metabase/reference/segments/SegmentList/SegmentList.tsx b/frontend/src/metabase/reference/segments/SegmentList/SegmentList.tsx
index b24baebc876..011ddfaddea 100644
--- a/frontend/src/metabase/reference/segments/SegmentList/SegmentList.tsx
+++ b/frontend/src/metabase/reference/segments/SegmentList/SegmentList.tsx
@@ -15,7 +15,6 @@ import { getDocsUrl } from "metabase/selectors/settings";
import { getShowMetabaseLinks } from "metabase/selectors/whitelabel";
import ReferenceHeader from "../../components/ReferenceHeader";
-import { getError, getLoading } from "../../selectors";
const emptyStateData = {
get title() {
@@ -34,13 +33,17 @@ const emptyStateData = {
};
interface SegmentListProps {
+ loading?: boolean;
+ loadingError?: unknown;
style?: CSSProperties;
}
-export function SegmentList({ style }: SegmentListProps) {
+export function SegmentList({
+ style,
+ loading,
+ loadingError,
+}: SegmentListProps) {
const entities = useSelector(getShallowSegments);
- const loading = useSelector(getLoading);
- const loadingError = useSelector(getError);
const adminLink = useSelector((state) =>
getDocsUrl(state, {
page: "data-modeling/segments",
diff --git a/frontend/src/metabase/reference/segments/SegmentListContainer.tsx b/frontend/src/metabase/reference/segments/SegmentListContainer.tsx
index ef940c7fc25..d096ee3ccd7 100644
--- a/frontend/src/metabase/reference/segments/SegmentListContainer.tsx
+++ b/frontend/src/metabase/reference/segments/SegmentListContainer.tsx
@@ -12,14 +12,14 @@ import { SegmentList } from "metabase/reference/segments/SegmentList";
import { useReferenceFetch } from "metabase/reference/use-reference-fetch-state";
import { useLocation } from "metabase/router";
-import type { ClearStateProps, FetchProps } from "../reference";
+import type { ClearStateProps } from "../reference";
import { getIsEditing } from "../selectors";
const mapDispatchToProps = {
...actions,
};
-interface SegmentListContainerProps extends FetchProps, ClearStateProps {}
+type SegmentListContainerProps = ClearStateProps;
function SegmentListContainer(props: SegmentListContainerProps) {
const { pathname } = useLocation();
@@ -28,7 +28,9 @@ function SegmentListContainer(props: SegmentListContainerProps) {
const dispatch = useDispatch();
const isEditing = useSelector(getIsEditing);
- useReferenceFetch(() => fetchSegmentListData(dispatch));
+ const { loading, loadingError } = useReferenceFetch(() =>
+ fetchSegmentListData(dispatch),
+ );
useEffect(() => {
const pathnameChanged =
@@ -44,7 +46,7 @@ function SegmentListContainer(props: SegmentListContainerProps) {
style={isEditing ? { paddingTop: "43px" } : {}}
sidebar={<BaseSidebar />}
>
- <SegmentList />
+ <SegmentList loading={loading} loadingError={loadingError} />
</SidebarLayout>
);
}
diff --git a/frontend/src/metabase/reference/segments/SegmentQuestionsContainer.tsx b/frontend/src/metabase/reference/segments/SegmentQuestionsContainer.tsx
index affd91dc8d7..bed71679d9b 100644
--- a/frontend/src/metabase/reference/segments/SegmentQuestionsContainer.tsx
+++ b/frontend/src/metabase/reference/segments/SegmentQuestionsContainer.tsx
@@ -14,7 +14,7 @@ import { SegmentQuestions } from "metabase/reference/segments/SegmentQuestions";
import { useReferenceFetch } from "metabase/reference/use-reference-fetch-state";
import { useLocation, useParams } from "metabase/router";
-import type { ClearStateProps, FetchProps } from "../reference";
+import type { ClearStateProps } from "../reference";
import {
type ReferenceRouteParams,
getIsEditing,
@@ -31,7 +31,7 @@ const mapDispatchToProps = {
...actions,
};
-interface SegmentQuestionsContainerProps extends FetchProps, ClearStateProps {
+interface SegmentQuestionsContainerProps extends ClearStateProps {
fetchQuestions: () => Promise<unknown>;
}
diff --git a/frontend/src/metabase/reference/segments/SegmentRevisions.tsx b/frontend/src/metabase/reference/segments/SegmentRevisions.tsx
index 3349a3384b2..b27a9809d2c 100644
--- a/frontend/src/metabase/reference/segments/SegmentRevisions.tsx
+++ b/frontend/src/metabase/reference/segments/SegmentRevisions.tsx
@@ -20,14 +20,8 @@ import type {
import ReferenceHeader from "../components/ReferenceHeader";
import type { ReferenceRouteProps, StateWithReference } from "../selectors";
-import {
- getError,
- getLoading,
- getSegment,
- getSegmentRevisions,
- getUser,
-} from "../selectors";
-import type { StubbedSegment } from "../types";
+import { getSegment, getSegmentRevisions, getUser } from "../selectors";
+import type { ReferenceLoadingProps, StubbedSegment } from "../types";
const emptyStateData = {
get message() {
@@ -44,8 +38,6 @@ const mapStateToProps = (
segment: getSegment(state, props),
tables: getTables(state),
user: getUser(state),
- loading: getLoading(state),
- loadingError: getError(state),
};
};
@@ -139,4 +131,11 @@ class SegmentRevisions extends Component<SegmentRevisionsProps> {
export default connect(
mapStateToProps,
// Unjustified type cast. FIXME
-)(SegmentRevisions as unknown as React.ComponentType);
+)(
+ // `connect` cannot match its inferred props against this component's own
+ // props, because the `actions` spread in `mapDispatchToProps` is untyped.
+ // The cast restores the props a caller actually passes.
+ SegmentRevisions as unknown as React.ComponentType<
+ ReferenceRouteProps & ReferenceLoadingProps
+ >,
+);
diff --git a/frontend/src/metabase/reference/segments/SegmentRevisionsContainer.tsx b/frontend/src/metabase/reference/segments/SegmentRevisionsContainer.tsx
index d0c58728f0c..5629e533c52 100644
--- a/frontend/src/metabase/reference/segments/SegmentRevisionsContainer.tsx
+++ b/frontend/src/metabase/reference/segments/SegmentRevisionsContainer.tsx
@@ -11,7 +11,7 @@ import SegmentRevisions from "metabase/reference/segments/SegmentRevisions";
import { useReferenceFetch } from "metabase/reference/use-reference-fetch-state";
import { useLocation, useParams } from "metabase/router";
-import type { ClearStateProps, FetchProps } from "../reference";
+import type { ClearStateProps } from "../reference";
import {
type ReferenceRouteParams,
getIsEditing,
@@ -26,7 +26,7 @@ const mapDispatchToProps = {
...actions,
};
-interface SegmentRevisionsContainerProps extends FetchProps, ClearStateProps {}
+type SegmentRevisionsContainerProps = ClearStateProps;
function SegmentRevisionsContainer(props: SegmentRevisionsContainerProps) {
const { pathname } = useLocation();
@@ -39,7 +39,9 @@ function SegmentRevisionsContainer(props: SegmentRevisionsContainerProps) {
const segmentId = useSelector((state) => getSegmentId(state, { params }));
const isEditing = useSelector(getIsEditing);
- useReferenceFetch(() => fetchSegmentRevisionsData(dispatch, segmentId));
+ const { loading, loadingError } = useReferenceFetch(() =>
+ fetchSegmentRevisionsData(dispatch, segmentId),
+ );
useEffect(() => {
const pathnameChanged =
@@ -55,7 +57,11 @@ function SegmentRevisionsContainer(props: SegmentRevisionsContainerProps) {
style={isEditing ? { paddingTop: "43px" } : {}}
sidebar={<SegmentSidebar segment={segment} user={user} />}
>
- <SegmentRevisions params={params} />
+ <SegmentRevisions
+ params={params}
+ loading={loading}
+ loadingError={loadingError}
+ />
</SidebarLayout>
);
}
diff --git a/frontend/src/metabase/reference/selectors.ts b/frontend/src/metabase/reference/selectors.ts
index 2377d045bc4..5f576e304d2 100644
--- a/frontend/src/metabase/reference/selectors.ts
+++ b/frontend/src/metabase/reference/selectors.ts
@@ -32,8 +32,6 @@ export interface ReferenceRouteProps {
}
interface ReferenceSliceState {
- isLoading: boolean;
- error: unknown;
isEditing: boolean;
isFormulaExpanded: boolean;
}
@@ -133,14 +131,6 @@ export const getTableQuestions = createSelector(
},
);
-export const getLoading = (state: State) =>
- // Unjustified type cast. FIXME
- (state as StateWithReference).reference.isLoading;
-
-export const getError = (state: State) =>
- // Unjustified type cast. FIXME
- (state as StateWithReference).reference.error;
-
export const getHasSingleSchema = createSelector(
[getTablesByDatabase],
(tables) => {
diff --git a/frontend/src/metabase/reference/types.ts b/frontend/src/metabase/reference/types.ts
index 44b25e44c7c..a013d72192e 100644
--- a/frontend/src/metabase/reference/types.ts
+++ b/frontend/src/metabase/reference/types.ts
@@ -52,3 +52,9 @@ export interface FieldFormFieldsValues {
settings?: FieldFormattingSettings;
[key: string]: unknown;
}
+
+/** Fetch state, passed from a container to the page it renders. */
+export type ReferenceLoadingProps = {
+ loading?: boolean;
+ loadingError?: unknown;
+};
diff --git a/frontend/src/metabase/reference/use-reference-fetch-state.ts b/frontend/src/metabase/reference/use-reference-fetch-state.ts
index 86c5d58afb0..8053c5f478c 100644
--- a/frontend/src/metabase/reference/use-reference-fetch-state.ts
+++ b/frontend/src/metabase/reference/use-reference-fetch-state.ts
@@ -1,60 +1,21 @@
-import { useEffect, useRef } from "react";
+import { useEffect, useRef, useState } from "react";
-import { useDispatch } from "metabase/redux";
+type ReferenceFetchState = {
+ loading: boolean;
+ loadingError: unknown;
+};
-import { clearError, endLoading, setError, startLoading } from "./reference";
+const INITIAL: ReferenceFetchState = { loading: true, loadingError: null };
/**
- * The reference module keeps its own page-level loading and error state, which
- * its presentational components read from the store. These two hooks are the
- * only bridge between a fetch and that state.
+ * Tracks a one-shot fetch for the reference pages whose data comes from a
+ * thunk rather than an RTK Query hook.
*
- * Both dispatch the start during render rather than from an effect. The
- * children read `loading` from the store, so it has to be true before their
- * first render. From an effect, even `useLayoutEffect`, the tree commits once
- * with no data and the reference header lays out wrong. See DEV-2430.
+ * Starts as loading, so the page renders its loading state on the first pass
+ * rather than committing once with no data. See DEV-2430.
*/
-function useFetchStart() {
- const dispatch = useDispatch();
- const hasStarted = useRef(false);
-
- if (!hasStarted.current) {
- hasStarted.current = true;
- dispatch(clearError());
- dispatch(startLoading());
- }
-}
-
-/** Drives the page state from an RTK Query result. */
-export function useReferenceFetchState({
- isFetching,
- error,
-}: {
- isFetching: boolean;
- error: unknown;
-}) {
- const dispatch = useDispatch();
- useFetchStart();
-
- useEffect(() => {
- if (isFetching) {
- return;
- }
- if (error) {
- dispatch(setError(error));
- }
- dispatch(endLoading());
- }, [dispatch, isFetching, error]);
-}
-
-/**
- * Drives the page state from a one-shot async fetch, for the pages whose data
- * still comes from a thunk. Runs once on mount, matching the render-phase
- * dispatch it replaced.
- */
-export function useReferenceFetch(run: () => unknown) {
- const dispatch = useDispatch();
- useFetchStart();
+export function useReferenceFetch(run: () => unknown): ReferenceFetchState {
+ const [state, setState] = useState<ReferenceFetchState>(INITIAL);
const runRef = useRef(run);
runRef.current = run;
@@ -64,20 +25,22 @@ export function useReferenceFetch(run: () => unknown) {
// `dispatch` of a thunk is typed as `unknown`, and some of these fetches
// are synchronous, so normalise before attaching handlers.
Promise.resolve(runRef.current())
- .catch((error: unknown) => {
+ .then(() => {
if (!isCancelled) {
- console.error(error);
- dispatch(setError(error));
+ setState({ loading: false, loadingError: null });
}
})
- .finally(() => {
+ .catch((error: unknown) => {
if (!isCancelled) {
- dispatch(endLoading());
+ console.error(error);
+ setState({ loading: false, loadingError: error });
}
});
return () => {
isCancelled = true;
};
- }, [dispatch]);
+ }, []);
+
+ return state;
}