Skip to content

Commit

Permalink
chore: Homepage cleanup (apache#14823)
Browse files Browse the repository at this point in the history
* initial commit

* update welcome

* fix lint

* add enum

* more choring

* fix lint

* redo logic for api stick api calls

* fix test

* fix chart test

* lint fix and remove unused code

* fix flicker and add suggestions

* lint

* fix test

* add suggestions

* add suggestions and fix test

* revert packagelock

* fix space
  • Loading branch information
pkdotson authored Jun 12, 2021
1 parent 53df152 commit 8e6a5a6
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 82 deletions.
23 changes: 23 additions & 0 deletions superset-frontend/src/views/CRUD/storageKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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.
*/

// storage keys for welcome page sticky tabs..
export const HOMEPAGE_CHART_FILTER = 'homepage_chart_filter';
export const HOMEPAGE_ACTIVITY_FILTER = 'homepage_activity_filter';
export const HOMEPAGE_DASHBOARD_FILTER = 'homepage_dashboard_filter';
5 changes: 5 additions & 0 deletions superset-frontend/src/views/CRUD/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export type FavoriteStatus = {
[id: number]: boolean;
};

export enum TableTabTypes {
FAVORITE = 'Favorite',
MINE = 'Mine',
}

export type Filters = {
col: string;
opr: string;
Expand Down
13 changes: 7 additions & 6 deletions superset-frontend/src/views/CRUD/welcome/ActivityTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import { ReactWrapper } from 'enzyme';
import { Provider } from 'react-redux';
import fetchMock from 'fetch-mock';
import thunk from 'redux-thunk';
import waitForComponentToPaint from 'spec/helpers/waitForComponentToPaint';
import configureStore from 'redux-mock-store';
import ActivityTable from 'src/views/CRUD/welcome/ActivityTable';

Expand Down Expand Up @@ -82,7 +81,7 @@ describe('ActivityTable', () => {
activityData: mockData,
setActiveChild: jest.fn(),
user: { userId: '1' },
loading: false,
loadedCount: 3,
};

let wrapper: ReactWrapper;
Expand Down Expand Up @@ -113,19 +112,21 @@ describe('ActivityTable', () => {
handler({} as any);
}
});
await waitForComponentToPaint(wrapper);
const dashboardCall = fetchMock.calls(/dashboard\/\?q/);
const chartCall = fetchMock.calls(/chart\/\?q/);
expect(chartCall).toHaveLength(1);
expect(dashboardCall).toHaveLength(1);
// waitforcomponenttopaint does not work here in this instance...
setTimeout(() => {
expect(chartCall).toHaveLength(1);
expect(dashboardCall).toHaveLength(1);
});
});
it('show empty state if there is no data', () => {
const activityProps = {
activeChild: 'Created',
activityData: {},
setActiveChild: jest.fn(),
user: { userId: '1' },
loading: false,
loadedCount: 3,
};
const wrapper = mount(
<Provider store={store}>
Expand Down
49 changes: 27 additions & 22 deletions superset-frontend/src/views/CRUD/welcome/ActivityTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ import { setInLocalStorage } from 'src/utils/localStorageHelpers';
import Loading from 'src/components/Loading';
import ListViewCard from 'src/components/ListViewCard';
import SubMenu from 'src/components/Menu/SubMenu';
import { mq, CardStyles, getEditedObjects } from 'src/views/CRUD/utils';
import { HOMEPAGE_ACTIVITY_FILTER } from 'src/views/CRUD/storageKeys';
import { Chart } from 'src/types/Chart';
import { Dashboard, SavedQueryObject } from 'src/views/CRUD/types';
import { mq, CardStyles, getEditedObjects } from 'src/views/CRUD/utils';

import { ActivityData } from './Welcome';
import EmptyState from './EmptyState';
Expand All @@ -51,6 +52,12 @@ interface RecentDashboard extends RecentActivity {
item_type: 'dashboard';
}

enum SetTabType {
EDITED = 'Edited',
CREATED = 'Created',
VIEWED = 'Viewed',
EXAMPLE = 'Examples',
}
/**
* Recent activity objects fetched by `getRecentAcitivtyObjs`.
*/
Expand All @@ -68,6 +75,7 @@ interface ActivityProps {
activeChild: string;
setActiveChild: (arg0: string) => void;
activityData: ActivityData;
loadedCount: number;
}

const ActivityContainer = styled.div`
Expand Down Expand Up @@ -161,43 +169,41 @@ export default function ActivityTable({
setActiveChild,
activityData,
user,
loadedCount,
}: ActivityProps) {
const [editedObjs, setEditedObjs] = useState<Array<ActivityData>>();
const [loadingState, setLoadingState] = useState(false);

useEffect(() => {
if (activeChild === 'Edited') {
setLoadingState(true);
getEditedObjects(user.userId).then(r => {
setEditedObjs([...r.editedChart, ...r.editedDash]);
setLoadingState(false);
});
}
}, []);

const getEditedCards = () => {
setLoadingState(true);
getEditedObjects(user.userId).then(r => {
setEditedObjs([...r.editedChart, ...r.editedDash]);
setLoadingState(false);
});
};

useEffect(() => {
if (activeChild === 'Edited') {
setLoadingState(true);
getEditedCards();
}
}, [activeChild]);

const tabs = [
{
name: 'Edited',
label: t('Edited'),
onClick: () => {
setActiveChild('Edited');
setInLocalStorage('activity', { activity: 'Edited' });
getEditedCards();
setInLocalStorage(HOMEPAGE_ACTIVITY_FILTER, SetTabType.EDITED);
},
},
{
name: 'Created',
label: t('Created'),
onClick: () => {
setActiveChild('Created');
setInLocalStorage('activity', { activity: 'Created' });
setInLocalStorage(HOMEPAGE_ACTIVITY_FILTER, SetTabType.CREATED);
},
},
];
Expand All @@ -208,7 +214,7 @@ export default function ActivityTable({
label: t('Viewed'),
onClick: () => {
setActiveChild('Viewed');
setInLocalStorage('activity', { activity: 'Viewed' });
setInLocalStorage(HOMEPAGE_ACTIVITY_FILTER, SetTabType.VIEWED);
},
});
} else {
Expand All @@ -217,7 +223,7 @@ export default function ActivityTable({
label: t('Examples'),
onClick: () => {
setActiveChild('Examples');
setInLocalStorage('activity', { activity: 'Examples' });
setInLocalStorage(HOMEPAGE_ACTIVITY_FILTER, SetTabType.EXAMPLE);
},
});
}
Expand Down Expand Up @@ -246,16 +252,15 @@ export default function ActivityTable({
);
},
);
if (loadingState && !editedObjs) {

const doneFetching = loadedCount < 3;

if ((loadingState && !editedObjs) || doneFetching) {
return <Loading position="inline" />;
}
return (
<>
<SubMenu
activeChild={activeChild}
// eslint-disable-next-line react/no-children-prop
tabs={tabs}
/>
<SubMenu activeChild={activeChild} tabs={tabs} />
{activityData[activeChild]?.length > 0 ||
(activeChild === 'Edited' && editedObjs && editedObjs.length > 0) ? (
<ActivityContainer>{renderActivity()}</ActivityContainer>
Expand Down
12 changes: 11 additions & 1 deletion superset-frontend/src/views/CRUD/welcome/ChartTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,21 @@ describe('ChartTable', () => {
}
});
await waitForComponentToPaint(wrapper);
expect(fetchMock.calls(chartsEndpoint)).toHaveLength(1);
expect(fetchMock.calls(chartsEndpoint)).toHaveLength(3);
expect(wrapper.find('ChartCard')).toExist();
});

it('display EmptyState if there is no data', async () => {
await act(async () => {
wrapper = mount(
<ChartTable
chartFilter="Mine"
user={{ userId: '2' }}
mine={[]}
store={store}
/>,
);
});
expect(wrapper.find('EmptyState')).toExist();
});
});
37 changes: 18 additions & 19 deletions superset-frontend/src/views/CRUD/welcome/ChartTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ import {
} from 'src/utils/localStorageHelpers';
import withToasts from 'src/messageToasts/enhancers/withToasts';
import { useHistory } from 'react-router-dom';
import { TableTabTypes } from 'src/views/CRUD/types';
import PropertiesModal from 'src/explore/components/PropertiesModal';
import { User } from 'src/types/bootstrapTypes';
import { CardContainer } from 'src/views/CRUD/utils';
import { HOMEPAGE_CHART_FILTER } from 'src/views/CRUD/storageKeys';
import ChartCard from 'src/views/CRUD/chart/ChartCard';
import Chart from 'src/types/Chart';
import handleResourceExport from 'src/utils/export';
import Loading from 'src/components/Loading';
import ErrorBoundary from 'src/components/ErrorBoundary';
import SubMenu from 'src/components/Menu/SubMenu';
import EmptyState from './EmptyState';
import { CardContainer } from '../utils';

const PAGE_SIZE = 3;

Expand All @@ -60,6 +62,9 @@ function ChartTable({
showThumbnails,
}: ChartTableProps) {
const history = useHistory();
const filterStore = getFromLocalStorage(HOMEPAGE_CHART_FILTER, null);
const initialFilter = filterStore || TableTabTypes.MINE;

const {
state: { loading, resourceCollection: charts, bulkSelectEnabled },
setResourceCollection: setCharts,
Expand All @@ -71,12 +76,11 @@ function ChartTable({
t('chart'),
addDangerToast,
true,
mine,
initialFilter === 'Favorite' ? [] : mine,
[],
false,
);

useEffect(() => {});
const chartIds = useMemo(() => charts.map(c => c.id), [charts]);
const [saveFavoriteStatus, favoriteStatus] = useFavoriteStatus(
'chart',
Expand All @@ -90,15 +94,12 @@ function ChartTable({
closeChartEditModal,
} = useChartEditModal(setCharts, charts);

const [chartFilter, setChartFilter] = useState('Mine');
const [chartFilter, setChartFilter] = useState(initialFilter);
const [preparingExport, setPreparingExport] = useState<boolean>(false);

useEffect(() => {
const filter = getFromLocalStorage('chart', null);
if (!filter) {
setChartFilter('Mine');
} else setChartFilter(filter.tab);
}, []);
getData(chartFilter);
}, [chartFilter]);

const handleBulkChartExport = (chartsToExport: Chart[]) => {
const ids = chartsToExport.map(({ id }) => id);
Expand Down Expand Up @@ -159,20 +160,18 @@ function ChartTable({
{
name: 'Favorite',
label: t('Favorite'),
onClick: () =>
getData('Favorite').then(() => {
setChartFilter('Favorite');
setInLocalStorage('chart', { tab: 'Favorite' });
}),
onClick: () => {
setChartFilter('Favorite');
setInLocalStorage(HOMEPAGE_CHART_FILTER, TableTabTypes.FAVORITE);
},
},
{
name: 'Mine',
label: t('Mine'),
onClick: () =>
getData('Mine').then(() => {
setChartFilter('Mine');
setInLocalStorage('chart', { tab: 'Mine' });
}),
onClick: () => {
setChartFilter('Mine');
setInLocalStorage(HOMEPAGE_CHART_FILTER, TableTabTypes.MINE);
},
},
]}
buttons={[
Expand Down
Loading

0 comments on commit 8e6a5a6

Please sign in to comment.