Skip to content

Commit

Permalink
refactor(dashboard): [chart-maximize-mode]put chart full-size state i…
Browse files Browse the repository at this point in the history
…n redux (#15384)

* refactor(dashboard): [chart-maximize-mode]put full-size status to redux

* fix: ci
  • Loading branch information
stephenLYZ committed Jul 2, 2021
1 parent cbc4aa8 commit 81633e9
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 21 deletions.
5 changes: 5 additions & 0 deletions superset-frontend/src/dashboard/actions/dashboardState.js
Expand Up @@ -359,6 +359,11 @@ export function unsetFocusedFilterField(chartId, column) {
return { type: UNSET_FOCUSED_FILTER_FIELD, chartId, column };
}

export const SET_FULL_SIZE_CHART_ID = 'SET_FULL_SIZE_CHART_ID';
export function setFullSizeChartId(chartId) {
return { type: SET_FULL_SIZE_CHART_ID, chartId };
}

// Undo history ---------------------------------------------------------------
export const SET_MAX_UNDO_HISTORY_EXCEEDED = 'SET_MAX_UNDO_HISTORY_EXCEEDED';
export function setMaxUndoHistoryExceeded(maxUndoHistoryExceeded = true) {
Expand Down
Expand Up @@ -90,10 +90,12 @@ const StyledHeader = styled.div`
z-index: 2;
`;

const StyledContent = styled.div`
const StyledContent = styled.div<{
fullSizeChartId: number | null;
}>`
grid-column: 2;
grid-row: 2;
z-index: 1;
z-index: ${({ fullSizeChartId }) => (fullSizeChartId ? 1000 : 1)};
`;

const StyledDashboardContent = styled.div<{
Expand Down Expand Up @@ -147,6 +149,9 @@ const DashboardBuilder: FC<DashboardBuilderProps> = () => {
const directPathToChild = useSelector<RootState, string[]>(
state => state.dashboardState.directPathToChild,
);
const fullSizeChartId = useSelector<RootState, number | null>(
state => state.dashboardState.fullSizeChartId,
);

const handleChangeTab = ({
pathToTabIndex,
Expand Down Expand Up @@ -276,7 +281,7 @@ const DashboardBuilder: FC<DashboardBuilderProps> = () => {
)}
</DragDroppable>
</StyledHeader>
<StyledContent>
<StyledContent fullSizeChartId={fullSizeChartId}>
<div
data-test="dashboard-content"
className={cx('dashboard', editMode && 'dashboard--editing')}
Expand Down
Expand Up @@ -54,6 +54,7 @@ const propTypes = {
directPathToChild: PropTypes.arrayOf(PropTypes.string),
directPathLastUpdated: PropTypes.number,
focusedFilterScope: PropTypes.object,
fullSizeChartId: PropTypes.oneOf([PropTypes.number, null]),

// grid related
availableColumnCount: PropTypes.number.isRequired,
Expand All @@ -66,6 +67,7 @@ const propTypes = {
deleteComponent: PropTypes.func.isRequired,
updateComponents: PropTypes.func.isRequired,
handleComponentDrop: PropTypes.func.isRequired,
setFullSizeChartId: PropTypes.func.isRequired,
};

const defaultProps = {
Expand Down Expand Up @@ -189,7 +191,6 @@ class ChartHolder extends React.Component {
outlinedComponentId: null,
outlinedColumnName: null,
directPathLastUpdated: 0,
isFullSize: false,
};

this.handleChangeFocus = this.handleChangeFocus.bind(this);
Expand Down Expand Up @@ -244,7 +245,10 @@ class ChartHolder extends React.Component {
}

handleToggleFullSize() {
this.setState(prevState => ({ isFullSize: !prevState.isFullSize }));
const { component, fullSizeChartId, setFullSizeChartId } = this.props;
const { chartId } = component.meta;
const isFullSize = fullSizeChartId === chartId;
setFullSizeChartId(isFullSize ? null : chartId);
}

render() {
Expand All @@ -263,8 +267,12 @@ class ChartHolder extends React.Component {
editMode,
isComponentVisible,
dashboardId,
fullSizeChartId,
} = this.props;

const { chartId } = component.meta;
const isFullSize = fullSizeChartId === chartId;

// inherit the size of parent columns
const widthMultiple =
parentComponent.type === COLUMN_TYPE
Expand All @@ -274,7 +282,7 @@ class ChartHolder extends React.Component {
let chartWidth = 0;
let chartHeight = 0;

if (this.state.isFullSize) {
if (isFullSize) {
chartWidth = window.innerWidth - CHART_MARGIN;
chartHeight = window.innerHeight - CHART_MARGIN;
} else {
Expand All @@ -288,8 +296,6 @@ class ChartHolder extends React.Component {
);
}

const { chartId } = component.meta;

return (
<DragDroppable
component={component}
Expand Down Expand Up @@ -326,7 +332,7 @@ class ChartHolder extends React.Component {
'dashboard-component',
'dashboard-component-chart-holder',
this.state.outlinedComponentId ? 'fade-in' : 'fade-out',
this.state.isFullSize && 'full-size',
isFullSize && 'full-size',
)}
>
{!editMode && (
Expand All @@ -351,7 +357,7 @@ class ChartHolder extends React.Component {
updateSliceName={this.handleUpdateSliceName}
isComponentVisible={isComponentVisible}
handleToggleFullSize={this.handleToggleFullSize}
isFullSize={this.state.isFullSize}
isFullSize={isFullSize}
/>
{editMode && (
<HoverMenu position="top">
Expand Down
Expand Up @@ -18,7 +18,6 @@
*/

import React from 'react';
import userEvent from '@testing-library/user-event';
import { waitFor } from '@testing-library/react';
import { sliceId as chartId } from 'spec/fixtures/mockChartQueries';
import { nativeFiltersInfo } from 'spec/javascripts/dashboard/fixtures/mockNativeFilters';
Expand Down Expand Up @@ -66,6 +65,8 @@ describe('ChartHolder', () => {
isComponentVisible: true,
dashboardId: 123,
nativeFilters: nativeFiltersInfo.filters,
fullSizeChartId: chartId,
setFullSizeChartId: () => {},
};
const mockStore = getMockStore({
...initialState,
Expand All @@ -79,19 +80,12 @@ describe('ChartHolder', () => {
</Provider>,
);

it('toggle full size', async () => {
it('should render full size', async () => {
renderWrapper();

let chart = (screen.getByTestId('slice-container')
const chart = (screen.getByTestId('slice-container')
.firstChild as HTMLElement).style;
expect(chart?.width).toBe('900px');
expect(chart?.height).toBe('26px');

userEvent.click(screen.getByRole('button'));
userEvent.click(screen.getByText('Maximize chart'));

chart = (screen.getByTestId('slice-container').firstChild as HTMLElement)
.style;
await waitFor(() => expect(chart?.width).toBe('992px'));
expect(chart?.height).toBe('714px');
});
Expand Down
Expand Up @@ -35,7 +35,11 @@ import {
updateComponents,
handleComponentDrop,
} from '../actions/dashboardLayout';
import { setDirectPathToChild, setActiveTabs } from '../actions/dashboardState';
import {
setDirectPathToChild,
setActiveTabs,
setFullSizeChartId,
} from '../actions/dashboardState';

const propTypes = {
id: PropTypes.string,
Expand Down Expand Up @@ -83,6 +87,7 @@ function mapStateToProps(
activeTabs: dashboardState.activeTabs,
directPathLastUpdated: dashboardState.directPathLastUpdated,
dashboardId: dashboardInfo.id,
fullSizeChartId: dashboardState.fullSizeChartId,
};

// rows and columns need more data about their child dimensions
Expand Down Expand Up @@ -112,6 +117,7 @@ function mapDispatchToProps(dispatch) {
updateComponents,
handleComponentDrop,
setDirectPathToChild,
setFullSizeChartId,
setActiveTabs,
logEvent,
},
Expand Down
7 changes: 7 additions & 0 deletions superset-frontend/src/dashboard/reducers/dashboardState.js
Expand Up @@ -36,6 +36,7 @@ import {
SET_FOCUSED_FILTER_FIELD,
UNSET_FOCUSED_FILTER_FIELD,
SET_ACTIVE_TABS,
SET_FULL_SIZE_CHART_ID,
} from '../actions/dashboardState';
import { HYDRATE_DASHBOARD } from '../actions/hydrate';

Expand Down Expand Up @@ -165,6 +166,12 @@ export default function dashboardStateReducer(state = {}, action) {
focusedFilterField: null,
};
},
[SET_FULL_SIZE_CHART_ID]() {
return {
...state,
fullSizeChartId: action.chartId,
};
},
};

if (action.type in actionHandlers) {
Expand Down
1 change: 1 addition & 0 deletions superset-frontend/src/dashboard/types.ts
Expand Up @@ -56,6 +56,7 @@ export type DashboardState = {
editMode: boolean;
directPathToChild: string[];
activeTabs: ActiveTabs;
fullSizeChartId: number | null;
};
export type DashboardInfo = {
common: {
Expand Down

0 comments on commit 81633e9

Please sign in to comment.