Skip to content

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

Add loading state to pull changes (#69380)

Nick Fitzpatrickf37171d483e7
diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncControls.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncControls.tsx
index d6f04e73d80..ec16717d0da 100644
--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncControls.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncControls.tsx
@@ -44,19 +44,17 @@ export const GitSyncControls = () => {
const { isDirty, refetch: refetchDirty } = useRemoteSyncDirtyState();
- const { data: hasRemoteChangesData, isLoading: isFetchingRemoteChanges } =
- useGetHasRemoteChangesQuery(undefined, {
- refetchOnMountOrArgChange: 10, // only refetch if the cache is more than 10 seconds stale
- skip: !combobox.dropdownOpened,
- });
+ const {
+ currentData: hasRemoteChangesData,
+ isFetching: isFetchingRemoteChanges,
+ } = useGetHasRemoteChangesQuery(undefined, {
+ refetchOnMountOrArgChange: 10, // only refetch if the cache is more than 10 seconds stale
+ skip: !combobox.dropdownOpened,
+ });
const { has_changes: hasRemoteChanges } = hasRemoteChangesData || {};
const isSwitchingBranch = !!nextBranch;
- const isLoading =
- isSyncTaskRunning ||
- isSwitchingBranch ||
- isImporting ||
- isFetchingRemoteChanges;
+ const isLoading = isSyncTaskRunning || isSwitchingBranch || isImporting;
const changeBranch = useCallback(
async (branch: string | null, isNewBranch?: boolean) => {
@@ -203,6 +201,7 @@ export const GitSyncControls = () => {
{dropdownView === "options" ? (
<GitSyncOptionsDropdown
isPullDisabled={!hasRemoteChanges}
+ isLoadingPull={isFetchingRemoteChanges}
isPushDisabled={!isDirty || isLoading}
onPullClick={handlePullClick}
onPushClick={handlePushClick}
diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncControls.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncControls.unit.spec.tsx
index 41c06bbe544..196ab2c2e85 100644
--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncControls.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncControls.unit.spec.tsx
@@ -2,7 +2,7 @@ import userEvent from "@testing-library/user-event";
import fetchMock from "fetch-mock";
import { setupRemoteSyncEndpoints } from "__support__/server-mocks";
-import { renderWithProviders, screen, waitFor } from "__support__/ui";
+import { renderWithProviders, screen, waitFor, within } from "__support__/ui";
import { GitSyncControls } from "./GitSyncControls";
import {
@@ -16,6 +16,7 @@ const setup = ({
isAdmin = true,
remoteSyncEnabled = true,
hasRemoteChanges = true,
+ hasRemoteChangesDelay = 0,
currentBranch = "main",
syncType = "read-write",
dirty = [],
@@ -24,12 +25,18 @@ const setup = ({
isAdmin?: boolean;
remoteSyncEnabled?: boolean;
hasRemoteChanges?: boolean;
+ hasRemoteChangesDelay?: number;
currentBranch?: string | null;
syncType?: "read-only" | "read-write";
dirty?: ReturnType<typeof createMockDirtyEntity>[];
branches?: string[];
} = {}) => {
- setupRemoteSyncEndpoints({ branches, dirty, hasRemoteChanges });
+ setupRemoteSyncEndpoints({
+ branches,
+ dirty,
+ hasRemoteChanges,
+ hasRemoteChangesDelay,
+ });
setupCollectionEndpoints();
setupSessionEndpoints({ remoteSyncEnabled, currentBranch, syncType });
@@ -197,14 +204,30 @@ describe("GitSyncControls", () => {
expect(getBranchButton(/main/)).toBeInTheDocument();
});
await userEvent.click(getBranchButton(/main/));
- expect(await findOption(/Pull changes/)).not.toHaveAttribute(
+ await waitFor(async () => {
+ expect(await findOption(/Pull changes/)).not.toHaveAttribute(
+ "data-combobox-disabled",
+ "true",
+ );
+ });
+ });
+
+ it("is disabled when there are no changes to pull", async () => {
+ setup({ hasRemoteChanges: false });
+
+ await waitFor(() => {
+ expect(getBranchButton(/main/)).toBeInTheDocument();
+ });
+ await userEvent.click(getBranchButton(/main/));
+ expect(await findOption(/Pull changes/)).toHaveAttribute(
"data-combobox-disabled",
"true",
);
});
- it("is disabled when there are no changes to pull", async () => {
- setup({ hasRemoteChanges: false });
+ it("is disabled when pull changes are loading", async () => {
+ jest.useFakeTimers({ advanceTimers: true });
+ setup({ hasRemoteChanges: true, hasRemoteChangesDelay: 10000 });
await waitFor(() => {
expect(getBranchButton(/main/)).toBeInTheDocument();
@@ -214,6 +237,13 @@ describe("GitSyncControls", () => {
"data-combobox-disabled",
"true",
);
+ expect(
+ await within(await findOption(/Pull changes/)).findByTestId(
+ "pull-changes-loader",
+ ),
+ ).toBeInTheDocument();
+ jest.advanceTimersByTime(10000);
+ jest.useRealTimers();
});
});
diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncOptionsDropdown.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncOptionsDropdown.tsx
index aebf506f7e2..c310843604d 100644
--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncOptionsDropdown.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncOptionsDropdown.tsx
@@ -1,9 +1,10 @@
import { t } from "ttag";
-import { Combobox, Group, Icon, Text, Tooltip } from "metabase/ui";
+import { Combobox, Group, Icon, Loader, Text, Tooltip } from "metabase/ui";
export interface GitSyncOptionsDropdownProps {
isPullDisabled: boolean;
+ isLoadingPull: boolean;
isPushDisabled: boolean;
onPullClick: VoidFunction;
onPushClick: VoidFunction;
@@ -12,6 +13,7 @@ export interface GitSyncOptionsDropdownProps {
export const GitSyncOptionsDropdown = ({
isPullDisabled,
+ isLoadingPull,
isPushDisabled,
onPullClick,
onPushClick,
@@ -40,13 +42,17 @@ export const GitSyncOptionsDropdown = ({
label={isPullDisabled ? t`No changes to pull` : t`Pull from remote`}
>
<Combobox.Option
- disabled={isPullDisabled}
+ disabled={isPullDisabled || isLoadingPull}
onClick={onPullClick}
py="sm"
value="pull"
>
<Group gap="md" wrap="nowrap">
- <Icon name="arrow_down" size={12} />
+ {isLoadingPull ? (
+ <Loader size={12} data-testid="pull-changes-loader" />
+ ) : (
+ <Icon name="arrow_down" size={12} />
+ )}
<Text>{t`Pull changes`}</Text>
</Group>
</Combobox.Option>
diff --git a/frontend/test/__support__/server-mocks/remote-sync.ts b/frontend/test/__support__/server-mocks/remote-sync.ts
index a35c79d9636..78d7666a697 100644
--- a/frontend/test/__support__/server-mocks/remote-sync.ts
+++ b/frontend/test/__support__/server-mocks/remote-sync.ts
@@ -93,12 +93,14 @@ export const setupRemoteSyncEndpoints = ({
dirty = [],
changedCollections = {},
hasRemoteChanges = false,
+ hasRemoteChangesDelay = 0,
settingsResponse = { success: true },
}: {
branches?: string[];
dirty?: RemoteSyncEntity[];
changedCollections?: Record<number, boolean>;
hasRemoteChanges?: boolean;
+ hasRemoteChangesDelay?: number;
settingsResponse?: Partial<RemoteSyncSettingsResponse>;
} = {}) => {
setupRemoteSyncBranchesEndpoint(branches);
@@ -107,7 +109,13 @@ export const setupRemoteSyncEndpoints = ({
setupRemoteSyncImportEndpoint();
setupRemoteSyncSettingsEndpoint(settingsResponse);
fetchMock.post("path:/api/ee/remote-sync/create-branch", {});
- fetchMock.get("path:/api/ee/remote-sync/has-remote-changes", {
- has_changes: hasRemoteChanges,
- });
+ fetchMock.get(
+ "path:/api/ee/remote-sync/has-remote-changes",
+ {
+ has_changes: hasRemoteChanges,
+ },
+ {
+ delay: hasRemoteChangesDelay,
+ },
+ );
};