Skip to content

Commit

Permalink
feat: sort card view by Alphabetical, Recently Modified, and Least Re…
Browse files Browse the repository at this point in the history
…cently Modified (#10601)
  • Loading branch information
Lily Kuang authored Aug 14, 2020
1 parent acb00f5 commit 03a62f1
Show file tree
Hide file tree
Showing 10 changed files with 293 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ import { act } from 'react-dom/test-utils';
import { QueryParamProvider } from 'use-query-params';
import { supersetTheme, ThemeProvider } from '@superset-ui/style';

import Button from 'src/components/Button';
import CardCollection from 'src/components/ListView/CardCollection';
import { CardSortSelect } from 'src/components/ListView/CardSortSelect';
import IndeterminateCheckbox from 'src/components/IndeterminateCheckbox';
import ListView from 'src/components/ListView/ListView';
import ListViewFilters from 'src/components/ListView/Filters';
import ListViewPagination from 'src/components/ListView/Pagination';
import Pagination from 'src/components/Pagination';
import Button from 'src/components/Button';
import TableCollection from 'src/components/ListView/TableCollection';

import waitForComponentToPaint from 'spec/helpers/waitForComponentToPaint';
import IndeterminateCheckbox from 'src/components/IndeterminateCheckbox';

function makeMockLocation(query) {
const queryStr = encodeURIComponent(query);
Expand Down Expand Up @@ -100,6 +103,14 @@ const mockedProps = {
onSelect: jest.fn(),
},
],
cardSortSelectOptions: [
{
desc: false,
id: 'something',
label: 'Alphabetical',
value: 'alphabetical',
},
],
};

const factory = (props = mockedProps) =>
Expand Down Expand Up @@ -281,6 +292,24 @@ describe('ListView', () => {
);
});

it('disable card view based on prop', async () => {
expect(wrapper.find(CardCollection).exists()).toBe(false);
expect(wrapper.find(CardSortSelect).exists()).toBe(false);
expect(wrapper.find(TableCollection).exists()).toBe(true);
});

it('enable card view based on prop', async () => {
const wrapper2 = factory({
...mockedProps,
renderCard: jest.fn(),
initialSort: [{ id: 'something' }],
});
await waitForComponentToPaint(wrapper2);
expect(wrapper2.find(CardCollection).exists()).toBe(true);
expect(wrapper2.find(CardSortSelect).exists()).toBe(true);
expect(wrapper2.find(TableCollection).exists()).toBe(false);
});

it('Throws an exception if filter missing in columns', () => {
expect.assertions(1);
const props = {
Expand Down Expand Up @@ -377,4 +406,24 @@ describe('ListView', () => {
]
`);
});

it('calls fetchData on card view sort', async () => {
const wrapper2 = factory({
...mockedProps,
renderCard: jest.fn(),
initialSort: [{ id: 'something' }],
});

act(() => {
wrapper2.find('[data-test="card-sort-select"]').first().props().onChange({
desc: false,
id: 'something',
label: 'Alphabetical',
value: 'alphabetical',
});
});

wrapper2.update();
expect(mockedProps.fetchData).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import fetchMock from 'fetch-mock';
import { supersetTheme, ThemeProvider } from '@superset-ui/style';

import ChartList from 'src/views/CRUD/chart/ChartList';
import ConfirmStatusChange from 'src/components/ConfirmStatusChange';
import ListView from 'src/components/ListView';
import PropertiesModal from 'src/explore/components/PropertiesModal';
import ListViewCard from 'src/components/ListViewCard';
Expand All @@ -49,7 +50,7 @@ const mockCharts = [...new Array(3)].map((_, i) => ({
}));

fetchMock.get(chartsInfoEndpoint, {
permissions: ['can_list', 'can_edit'],
permissions: ['can_list', 'can_edit', 'can_delete'],
});
fetchMock.get(chartssOwnersEndpoint, {
result: [],
Expand Down Expand Up @@ -113,4 +114,9 @@ describe('ChartList', () => {
wrapper.find('[data-test="pencil"]').first().simulate('click');
expect(wrapper.find(PropertiesModal)).toExist();
});

it('delete', () => {
wrapper.find('[data-test="trash"]').first().simulate('click');
expect(wrapper.find(ConfirmStatusChange)).toExist();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import configureStore from 'redux-mock-store';
import fetchMock from 'fetch-mock';
import { supersetTheme, ThemeProvider } from '@superset-ui/style';

import ConfirmStatusChange from 'src/components/ConfirmStatusChange';
import DashboardList from 'src/views/CRUD/dashboard/DashboardList';
import ListView from 'src/components/ListView';
import PropertiesModal from 'src/dashboard/components/PropertiesModal';
import ListViewCard from 'src/components/ListViewCard';
import PropertiesModal from 'src/dashboard/components/PropertiesModal';

// store needed for withToasts(DashboardTable)
const mockStore = configureStore([thunk]);
Expand All @@ -50,7 +51,7 @@ const mockDashboards = [...new Array(3)].map((_, i) => ({
}));

fetchMock.get(dashboardsInfoEndpoint, {
permissions: ['can_list', 'can_edit'],
permissions: ['can_list', 'can_edit', 'can_delete'],
});
fetchMock.get(dashboardOwnersEndpoint, {
result: [],
Expand Down Expand Up @@ -104,4 +105,19 @@ describe('DashboardList', () => {
wrapper.find('[data-test="pencil"]').first().simulate('click');
expect(wrapper.find(PropertiesModal)).toExist();
});

it('card view edits', () => {
wrapper.find('[data-test="pencil"]').last().simulate('click');
expect(wrapper.find(PropertiesModal)).toExist();
});

it('delete', () => {
wrapper.find('[data-test="trash"]').first().simulate('click');
expect(wrapper.find(ConfirmStatusChange)).toExist();
});

it('card view delete', () => {
wrapper.find('[data-test="trash"]').last().simulate('click');
expect(wrapper.find(ConfirmStatusChange)).toExist();
});
});
114 changes: 114 additions & 0 deletions superset-frontend/src/components/ListView/CardSortSelect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/**
* 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 React, { useState } from 'react';
import { styled, withTheme, SupersetThemeProps } from '@superset-ui/style';
import { PartialThemeConfig, Select } from 'src/components/Select';
import { CardSortSelectOption, FetchDataConfig, SortColumn } from './types';
import { filterSelectStyles } from './utils';

const SortTitle = styled.label`
font-weight: bold;
line-height: 27px;
margin: 0 0.4em 0 0;
`;

const SortContainer = styled.div`
display: inline-flex;
float: right;
font-size: ${({ theme }) => theme.typography.sizes.s}px;
padding: 24px 24px 0 0;
position: relative;
top: 8px;
`;
interface CardViewSelectSortProps {
onChange: (conf: FetchDataConfig) => any;
options: Array<CardSortSelectOption>;
initialSort?: SortColumn[];
pageIndex: number;
pageSize: number;
}

interface StyledSelectProps {
onChange: (value: CardSortSelectOption) => void;
options: CardSortSelectOption[];
selectStyles: any;
theme: SupersetThemeProps['theme'];
value: CardSortSelectOption;
}

function StyledSelect({
onChange,
options,
selectStyles,
theme,
value,
}: StyledSelectProps) {
const filterSelectTheme: PartialThemeConfig = {
spacing: {
baseUnit: 1,
fontSize: theme.typography.sizes.s,
minWidth: '5em',
},
};
return (
<Select
data-test="card-sort-select"
clearable={false}
onChange={onChange}
options={options}
stylesConfig={selectStyles}
themeConfig={filterSelectTheme}
value={value}
/>
);
}

const StyledCardSortSelect = withTheme(StyledSelect);

export const CardSortSelect = ({
initialSort,
onChange,
options,
pageIndex,
pageSize,
}: CardViewSelectSortProps) => {
const defaultSort =
initialSort && options.find(({ id }) => id === initialSort[0].id);
const [selectedOption, setSelectedOption] = useState<CardSortSelectOption>(
defaultSort || options[0],
);

const handleOnChange = (selected: CardSortSelectOption) => {
setSelectedOption(selected);
const sortBy = [{ id: selected.id, desc: selected.desc }];
onChange({ pageIndex, pageSize, sortBy, filters: [] });
};

return (
<SortContainer>
<SortTitle>Sort:</SortTitle>
<StyledCardSortSelect
onChange={(value: CardSortSelectOption) => handleOnChange(value)}
options={options}
selectStyles={filterSelectStyles}
value={selectedOption}
/>
</SortContainer>
);
};
49 changes: 16 additions & 33 deletions superset-frontend/src/components/ListView/Filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ import {
Select,
PaginatedSelect,
PartialThemeConfig,
PartialStylesConfig,
} from 'src/components/Select';

import SearchInput from 'src/components/SearchInput';
import {
Filter,
Filters,
FilterValue,
Filters,
InternalFilter,
SelectOption,
} from './types';
import { filterSelectStyles } from './utils';

interface BaseFilter {
Header: string;
initialValue: any;
}
interface SelectFilterProps extends BaseFilter {
name?: string;
onSelect: (selected: any) => any;
selects: Filter['selects'];
emptyLabel?: string;
fetchSelects?: Filter['fetchSelects'];
name?: string;
onSelect: (selected: any) => any;
paginate?: boolean;
selects: Filter['selects'];
theme: SupersetThemeProps['theme'];
}

Expand All @@ -61,40 +61,23 @@ const FilterTitle = styled.label`
margin: 0 0.4em 0 0;
`;

const filterSelectStyles: PartialStylesConfig = {
container: (provider, { getValue }) => ({
...provider,
// dynamic width based on label string length
minWidth: `${Math.min(
12,
Math.max(5, 3 + getValue()[0].label.length / 2),
)}em`,
}),
control: provider => ({
...provider,
borderWidth: 0,
boxShadow: 'none',
cursor: 'pointer',
}),
};

const CLEAR_SELECT_FILTER_VALUE = 'CLEAR_SELECT_FILTER_VALUE';

function SelectFilter({
Header,
selects = [],
emptyLabel = 'None',
fetchSelects,
initialValue,
onSelect,
fetchSelects,
paginate = false,
selects = [],
theme,
}: SelectFilterProps) {
const filterSelectTheme: PartialThemeConfig = {
spacing: {
baseUnit: 2,
minWidth: '5em',
fontSize: theme.typography.sizes.s,
minWidth: '5em',
},
};

Expand Down Expand Up @@ -235,12 +218,12 @@ function UIFilters({
(
{
Header,
fetchSelects,
id,
input,
paginate,
selects,
unfilteredLabel,
fetchSelects,
paginate,
},
index,
) => {
Expand All @@ -249,24 +232,24 @@ function UIFilters({
if (input === 'select') {
return (
<StyledSelectFilter
key={id}
name={id}
Header={Header}
selects={selects}
emptyLabel={unfilteredLabel}
initialValue={initialValue}
fetchSelects={fetchSelects}
paginate={paginate}
initialValue={initialValue}
key={id}
name={id}
onSelect={(value: any) => updateFilterValue(index, value)}
paginate={paginate}
selects={selects}
/>
);
}
if (input === 'search') {
return (
<SearchFilter
key={id}
Header={Header}
initialValue={initialValue}
key={id}
onSubmit={(value: string) => updateFilterValue(index, value)}
/>
);
Expand Down
Loading

0 comments on commit 03a62f1

Please sign in to comment.