Skip to content

Commit

Permalink
feat: show spinner on exports (#15107)
Browse files Browse the repository at this point in the history
* feat: show spinner on exports

* Set cookie only if token is passed

* Use iframe

* Small fixes

* Fix lint

* Remove stale test

* Add explicit type
  • Loading branch information
betodealmeida committed Jun 12, 2021
1 parent ff2d588 commit 53df152
Show file tree
Hide file tree
Showing 17 changed files with 167 additions and 60 deletions.
48 changes: 48 additions & 0 deletions superset-frontend/src/utils/export.ts
@@ -0,0 +1,48 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import parseCookie from 'src/utils/parseCookie';
import rison from 'rison';
import shortid from 'shortid';

export default function handleResourceExport(
resource: string,
ids: number[],
done: () => void,
interval = 200,
): void {
const token = shortid.generate();
const url = `/api/v1/${resource}/export/?q=${rison.encode(
ids,
)}&token=${token}`;

// create new iframe for export
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
iframe.src = url;
document.body.appendChild(iframe);

const timer = window.setInterval(() => {
const cookie: { [cookieId: string]: string } = parseCookie();
if (cookie[token] === 'done') {
window.clearInterval(timer);
document.body.removeChild(iframe);
done();
}
}, interval);
}
4 changes: 3 additions & 1 deletion superset-frontend/src/views/CRUD/chart/ChartCard.tsx
Expand Up @@ -29,7 +29,7 @@ import Label from 'src/components/Label';
import { Dropdown, Menu } from 'src/common/components';
import FaveStar from 'src/components/FaveStar';
import FacePile from 'src/components/FacePile';
import { handleChartDelete, handleBulkChartExport, CardStyles } from '../utils';
import { handleChartDelete, CardStyles } from '../utils';

interface ChartCardProps {
chart: Chart;
Expand All @@ -45,6 +45,7 @@ interface ChartCardProps {
chartFilter?: string;
userId?: number;
showThumbnails?: boolean;
handleBulkChartExport: (chartsToExport: Chart[]) => void;
}

export default function ChartCard({
Expand All @@ -61,6 +62,7 @@ export default function ChartCard({
favoriteStatus,
chartFilter,
userId,
handleBulkChartExport,
}: ChartCardProps) {
const canEdit = hasPerm('can_write');
const canDelete = hasPerm('can_write');
Expand Down
14 changes: 13 additions & 1 deletion superset-frontend/src/views/CRUD/chart/ChartList.tsx
Expand Up @@ -29,14 +29,14 @@ import { FeatureFlag, isFeatureEnabled } from 'src/featureFlags';
import {
createErrorHandler,
createFetchRelated,
handleBulkChartExport,
handleChartDelete,
} from 'src/views/CRUD/utils';
import {
useChartEditModal,
useFavoriteStatus,
useListViewResource,
} from 'src/views/CRUD/hooks';
import handleResourceExport from 'src/utils/export';
import ConfirmStatusChange from 'src/components/ConfirmStatusChange';
import SubMenu, { SubMenuProps } from 'src/components/Menu/SubMenu';
import FaveStar from 'src/components/FaveStar';
Expand All @@ -47,6 +47,7 @@ import ListView, {
ListViewProps,
SelectOption,
} from 'src/components/ListView';
import Loading from 'src/components/Loading';
import { getFromLocalStorage } from 'src/utils/localStorageHelpers';
import withToasts from 'src/messageToasts/enhancers/withToasts';
import PropertiesModal from 'src/explore/components/PropertiesModal';
Expand Down Expand Up @@ -156,6 +157,7 @@ function ChartList(props: ChartListProps) {

const [importingChart, showImportModal] = useState<boolean>(false);
const [passwordFields, setPasswordFields] = useState<string[]>([]);
const [preparingExport, setPreparingExport] = useState<boolean>(false);

const openChartImportModal = () => {
showImportModal(true);
Expand All @@ -177,6 +179,14 @@ function ChartList(props: ChartListProps) {
hasPerm('can_read') && isFeatureEnabled(FeatureFlag.VERSIONED_EXPORT);
const initialSort = [{ id: 'changed_on_delta_humanized', desc: true }];

const handleBulkChartExport = (chartsToExport: Chart[]) => {
const ids = chartsToExport.map(({ id }) => id);
handleResourceExport('chart', ids, () => {
setPreparingExport(false);
});
setPreparingExport(true);
};

function handleBulkChartDelete(chartsToDelete: Chart[]) {
SupersetClient.delete({
endpoint: `/api/v1/chart/?q=${rison.encode(
Expand Down Expand Up @@ -540,6 +550,7 @@ function ChartList(props: ChartListProps) {
loading={loading}
favoriteStatus={favoriteStatus[chart.id]}
saveFavoriteStatus={saveFavoriteStatus}
handleBulkChartExport={handleBulkChartExport}
/>
);
}
Expand Down Expand Up @@ -653,6 +664,7 @@ function ChartList(props: ChartListProps) {
passwordFields={passwordFields}
setPasswordFields={setPasswordFields}
/>
{preparingExport && <Loading />}
</>
);
}
Expand Down
8 changes: 3 additions & 5 deletions superset-frontend/src/views/CRUD/dashboard/DashboardCard.tsx
Expand Up @@ -19,11 +19,7 @@
import React from 'react';
import { Link, useHistory } from 'react-router-dom';
import { t } from '@superset-ui/core';
import {
handleDashboardDelete,
handleBulkDashboardExport,
CardStyles,
} from 'src/views/CRUD/utils';
import { handleDashboardDelete, CardStyles } from 'src/views/CRUD/utils';
import { isFeatureEnabled, FeatureFlag } from 'src/featureFlags';
import { Dropdown, Menu } from 'src/common/components';
import ConfirmStatusChange from 'src/components/ConfirmStatusChange';
Expand All @@ -49,6 +45,7 @@ interface DashboardCardProps {
dashboardFilter?: string;
userId?: number;
showThumbnails?: boolean;
handleBulkDashboardExport: (dashboardsToExport: Dashboard[]) => void;
}

function DashboardCard({
Expand All @@ -64,6 +61,7 @@ function DashboardCard({
favoriteStatus,
saveFavoriteStatus,
showThumbnails,
handleBulkDashboardExport,
}: DashboardCardProps) {
const history = useHistory();
const canEdit = hasPerm('can_write');
Expand Down
14 changes: 13 additions & 1 deletion superset-frontend/src/views/CRUD/dashboard/DashboardList.tsx
Expand Up @@ -25,10 +25,11 @@ import {
createFetchRelated,
createErrorHandler,
handleDashboardDelete,
handleBulkDashboardExport,
} from 'src/views/CRUD/utils';
import { useListViewResource, useFavoriteStatus } from 'src/views/CRUD/hooks';
import ConfirmStatusChange from 'src/components/ConfirmStatusChange';
import handleResourceExport from 'src/utils/export';
import Loading from 'src/components/Loading';
import SubMenu, { SubMenuProps } from 'src/components/Menu/SubMenu';
import ListView, {
ListViewProps,
Expand Down Expand Up @@ -123,6 +124,7 @@ function DashboardList(props: DashboardListProps) {

const [importingDashboard, showImportModal] = useState<boolean>(false);
const [passwordFields, setPasswordFields] = useState<string[]>([]);
const [preparingExport, setPreparingExport] = useState<boolean>(false);

const openDashboardImportModal = () => {
showImportModal(true);
Expand Down Expand Up @@ -170,6 +172,14 @@ function DashboardList(props: DashboardListProps) {
);
}

const handleBulkDashboardExport = (dashboardsToExport: Dashboard[]) => {
const ids = dashboardsToExport.map(({ id }) => id);
handleResourceExport('dashboard', ids, () => {
setPreparingExport(false);
});
setPreparingExport(true);
};

function handleBulkDashboardDelete(dashboardsToDelete: Dashboard[]) {
return SupersetClient.delete({
endpoint: `/api/v1/dashboard/?q=${rison.encode(
Expand Down Expand Up @@ -487,6 +497,7 @@ function DashboardList(props: DashboardListProps) {
openDashboardEditModal={openDashboardEditModal}
saveFavoriteStatus={saveFavoriteStatus}
favoriteStatus={favoriteStatus[dashboard.id]}
handleBulkDashboardExport={handleBulkDashboardExport}
/>
);
}
Expand Down Expand Up @@ -605,6 +616,7 @@ function DashboardList(props: DashboardListProps) {
passwordFields={passwordFields}
setPasswordFields={setPasswordFields}
/>
{preparingExport && <Loading />}
</>
);
}
Expand Down
16 changes: 12 additions & 4 deletions superset-frontend/src/views/CRUD/data/database/DatabaseList.tsx
Expand Up @@ -18,7 +18,7 @@
*/
import { SupersetClient, t, styled } from '@superset-ui/core';
import React, { useState, useMemo } from 'react';
import rison from 'rison';
import Loading from 'src/components/Loading';
import { isFeatureEnabled, FeatureFlag } from 'src/featureFlags';
import { useListViewResource } from 'src/views/CRUD/hooks';
import { createErrorHandler } from 'src/views/CRUD/utils';
Expand All @@ -30,6 +30,7 @@ import Icons from 'src/components/Icons';
import ListView, { FilterOperator, Filters } from 'src/components/ListView';
import { commonMenuData } from 'src/views/CRUD/data/common';
import ImportModelsModal from 'src/components/ImportModal/index';
import handleResourceExport from 'src/utils/export';
import DatabaseModal from './DatabaseModal';

import { DatabaseObject } from './types';
Expand Down Expand Up @@ -97,6 +98,7 @@ function DatabaseList({ addDangerToast, addSuccessToast }: DatabaseListProps) {
);
const [importingDatabase, showImportModal] = useState<boolean>(false);
const [passwordFields, setPasswordFields] = useState<string[]>([]);
const [preparingExport, setPreparingExport] = useState<boolean>(false);

const openDatabaseImportModal = () => {
showImportModal(true);
Expand Down Expand Up @@ -203,9 +205,14 @@ function DatabaseList({ addDangerToast, addSuccessToast }: DatabaseListProps) {
}

function handleDatabaseExport(database: DatabaseObject) {
return window.location.assign(
`/api/v1/database/export/?q=${rison.encode([database.id])}`,
);
if (database.id === undefined) {
return;
}

handleResourceExport('database', [database.id], () => {
setPreparingExport(false);
});
setPreparingExport(true);
}

const initialSort = [{ id: 'changed_on_delta_humanized', desc: true }];
Expand Down Expand Up @@ -469,6 +476,7 @@ function DatabaseList({ addDangerToast, addSuccessToast }: DatabaseListProps) {
passwordFields={passwordFields}
setPasswordFields={setPasswordFields}
/>
{preparingExport && <Loading />}
</>
);
}
Expand Down
17 changes: 11 additions & 6 deletions superset-frontend/src/views/CRUD/data/dataset/DatasetList.tsx
Expand Up @@ -33,11 +33,13 @@ import { useListViewResource } from 'src/views/CRUD/hooks';
import ConfirmStatusChange from 'src/components/ConfirmStatusChange';
import DatasourceModal from 'src/datasource/DatasourceModal';
import DeleteModal from 'src/components/DeleteModal';
import handleResourceExport from 'src/utils/export';
import ListView, {
ListViewProps,
Filters,
FilterOperator,
} from 'src/components/ListView';
import Loading from 'src/components/Loading';
import SubMenu, {
SubMenuProps,
ButtonProps,
Expand Down Expand Up @@ -131,6 +133,7 @@ const DatasetList: FunctionComponent<DatasetListProps> = ({

const [importingDataset, showImportModal] = useState<boolean>(false);
const [passwordFields, setPasswordFields] = useState<string[]>([]);
const [preparingExport, setPreparingExport] = useState<boolean>(false);

const openDatasetImportModal = () => {
showImportModal(true);
Expand Down Expand Up @@ -547,12 +550,13 @@ const DatasetList: FunctionComponent<DatasetListProps> = ({
);
};

const handleBulkDatasetExport = (datasetsToExport: Dataset[]) =>
window.location.assign(
`/api/v1/dataset/export/?q=${rison.encode(
datasetsToExport.map(({ id }) => id),
)}`,
);
const handleBulkDatasetExport = (datasetsToExport: Dataset[]) => {
const ids = datasetsToExport.map(({ id }) => id);
handleResourceExport('dataset', ids, () => {
setPreparingExport(false);
});
setPreparingExport(true);
};

return (
<>
Expand Down Expand Up @@ -682,6 +686,7 @@ const DatasetList: FunctionComponent<DatasetListProps> = ({
passwordFields={passwordFields}
setPasswordFields={setPasswordFields}
/>
{preparingExport && <Loading />}
</>
);
};
Expand Down
Expand Up @@ -26,7 +26,6 @@ import { render, screen, cleanup, waitFor } from 'spec/helpers/testing-library';
import userEvent from '@testing-library/user-event';
import { QueryParamProvider } from 'use-query-params';
import { act } from 'react-dom/test-utils';
import { handleBulkSavedQueryExport } from 'src/views/CRUD/utils';
import * as featureFlags from 'src/featureFlags';
import SavedQueryList from 'src/views/CRUD/data/savedquery/SavedQueryList';
import SubMenu from 'src/components/Menu/SubMenu';
Expand Down Expand Up @@ -285,14 +284,6 @@ describe('RTL', () => {
expect(exportTooltip).toBeInTheDocument();
});

it('runs handleBulkSavedQueryExport when export is clicked', () => {
// Grab Export action button and mock mouse clicking it
const exportActionButton = screen.getAllByRole('button')[18];
userEvent.click(exportActionButton);

expect(handleBulkSavedQueryExport).toHaveBeenCalled();
});

it('renders an import button in the submenu', () => {
// Grab and assert that import saved query button is visible
const importButton = screen.getByTestId('import-button');
Expand Down
Expand Up @@ -25,12 +25,12 @@ import {
createFetchRelated,
createFetchDistinct,
createErrorHandler,
handleBulkSavedQueryExport,
} from 'src/views/CRUD/utils';
import Popover from 'src/components/Popover';
import withToasts from 'src/messageToasts/enhancers/withToasts';
import { useListViewResource } from 'src/views/CRUD/hooks';
import ConfirmStatusChange from 'src/components/ConfirmStatusChange';
import handleResourceExport from 'src/utils/export';
import SubMenu, {
SubMenuProps,
ButtonProps,
Expand All @@ -40,6 +40,7 @@ import ListView, {
Filters,
FilterOperator,
} from 'src/components/ListView';
import Loading from 'src/components/Loading';
import DeleteModal from 'src/components/DeleteModal';
import ActionsBar, { ActionProps } from 'src/components/ListView/ActionsBar';
import { Tooltip } from 'src/components/Tooltip';
Expand Down Expand Up @@ -117,6 +118,7 @@ function SavedQueryList({
] = useState<SavedQueryObject | null>(null);
const [importingSavedQuery, showImportModal] = useState<boolean>(false);
const [passwordFields, setPasswordFields] = useState<string[]>([]);
const [preparingExport, setPreparingExport] = useState<boolean>(false);

const openSavedQueryImportModal = () => {
showImportModal(true);
Expand Down Expand Up @@ -238,6 +240,16 @@ function SavedQueryList({
);
};

const handleBulkSavedQueryExport = (
savedQueriesToExport: SavedQueryObject[],
) => {
const ids = savedQueriesToExport.map(({ id }) => id);
handleResourceExport('saved_query', ids, () => {
setPreparingExport(false);
});
setPreparingExport(true);
};

const handleBulkQueryDelete = (queriesToDelete: SavedQueryObject[]) => {
SupersetClient.delete({
endpoint: `/api/v1/saved_query/?q=${rison.encode(
Expand Down Expand Up @@ -542,6 +554,7 @@ function SavedQueryList({
passwordFields={passwordFields}
setPasswordFields={setPasswordFields}
/>
{preparingExport && <Loading />}
</>
);
}
Expand Down

0 comments on commit 53df152

Please sign in to comment.