You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2021/10/07 21:19:21 UTC

[GitHub] [superset] michael-s-molina commented on a change in pull request #16154: feat(dashboard): Let users re-arrange native filters

michael-s-molina commented on a change in pull request #16154:
URL: https://github.com/apache/superset/pull/16154#discussion_r724522369



##########
File path: superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/DraggableFilter.tsx
##########
@@ -0,0 +1,138 @@
+/**
+ * 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 { styled } from '@superset-ui/core';
+import React, { useRef } from 'react';
+import {
+  DragSourceMonitor,
+  DropTargetMonitor,
+  useDrag,
+  useDrop,
+  XYCoord,
+} from 'react-dnd';
+import Icons, { IconType } from 'src/components/Icons';
+
+interface TitleContainerProps {
+  readonly isDragging: boolean;
+}
+const FILTER_TYPE = 'FILTER';
+
+const Container = styled.div<TitleContainerProps>`
+  ${({ isDragging, theme }) => `
+    opacity: ${isDragging ? 0.3 : 1};
+    cursor: ${isDragging ? 'grabbing' : 'pointer'};
+    width: 100%;
+    display: flex;
+    padding:  ${theme.gridUnit}px
+`}

Review comment:
       ```suggestion
     `}
   ```

##########
File path: superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FilterTitleContainer.tsx
##########
@@ -0,0 +1,162 @@
+/**
+ * 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 from 'react';
+import { styled, t } from '@superset-ui/core';
+import Icons from 'src/components/Icons';
+import { FilterRemoval } from './types';
+import DraggableFilter from './DraggableFilter';
+
+const FilterTitle = styled.div`
+  ${({ theme }) => `
+      display: flex;
+      padding: ${theme.gridUnit * 2}px;
+      width: 100%;
+      border-radius: ${theme.borderRadius}px;
+      &.active {
+        color: ${theme.colors.grayscale.dark1};
+        border-radius: ${theme.borderRadius}px;
+        background-color: ${theme.colors.secondary.light4};
+        span, .anticon {
+          color: ${theme.colors.grayscale.dark1};
+        }
+      }
+      &:hover {
+        color: ${theme.colors.primary.light1};
+        span, .anticon {
+          color: ${theme.colors.primary.light1};
+        }
+      }
+`}

Review comment:
       ```suggestion
     `}
   ```

##########
File path: superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/DraggableFilter.tsx
##########
@@ -0,0 +1,138 @@
+/**
+ * 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 { styled } from '@superset-ui/core';
+import React, { useRef } from 'react';
+import {
+  DragSourceMonitor,
+  DropTargetMonitor,
+  useDrag,
+  useDrop,
+  XYCoord,
+} from 'react-dnd';
+import Icons, { IconType } from 'src/components/Icons';
+
+interface TitleContainerProps {
+  readonly isDragging: boolean;
+}
+const FILTER_TYPE = 'FILTER';
+
+const Container = styled.div<TitleContainerProps>`
+  ${({ isDragging, theme }) => `
+    opacity: ${isDragging ? 0.3 : 1};
+    cursor: ${isDragging ? 'grabbing' : 'pointer'};
+    width: 100%;
+    display: flex;
+    padding:  ${theme.gridUnit}px
+`}
+`;
+
+const DragIcon = styled(Icons.Drag)<IconType & { isDragging: boolean }>`
+  ${({ isDragging, theme }) => `
+    font-size: ${theme.typography.sizes.m}px;
+    margin-top: 12px;
+    cursor: ${isDragging ? 'grabbing' : 'grab'};
+    padding-left: ${theme.gridUnit}px;
+`}

Review comment:
       ```suggestion
     `}
   ```

##########
File path: superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FilterConfigPane.test.tsx
##########
@@ -0,0 +1,103 @@
+/**
+ * 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 from 'react';
+import { dashboardLayout } from 'spec/fixtures/mockDashboardLayout';
+import { buildNativeFilter } from 'spec/fixtures/mockNativeFilters';
+import { render, screen, fireEvent } from 'spec/helpers/testing-library';
+import FilterConfigPane from './FilterConfigurePane';
+
+const defaultProps = {
+  children: jest.fn(),
+  getFilterTitle: (id: string) => id,
+  onChange: jest.fn(),
+  onEdit: jest.fn(),
+  onRearrange: jest.fn(),
+  restoreFilter: jest.fn(),
+  currentFilterId: 'NATIVE_FILTER-1',
+  filterGroups: [['NATIVE_FILTER-2', 'NATIVE_FILTER-1'], ['NATIVE_FILTER-3']],
+  removedFilters: {},
+};
+const defaultState = {
+  dashboardInfo: {
+    metadata: {
+      native_filter_configuration: [
+        buildNativeFilter('NATIVE_FILTER-1', 'state', ['NATIVE_FILTER-2']),
+        buildNativeFilter('NATIVE_FILTER-2', 'country', []),
+        buildNativeFilter('NATIVE_FILTER-3', 'product', []),
+      ],
+    },
+  },
+  dashboardLayout,
+};
+
+function defaultRender(initialState: any = defaultState, props = defaultProps) {
+  return render(<FilterConfigPane {...props} />, {
+    initialState,
+    useDnd: true,
+    useRedux: true,
+  });
+}
+
+test('renders form', async () => {
+  defaultRender();
+  expect(defaultProps.children).toHaveBeenCalledTimes(3);
+});
+test('drag and drop', async () => {

Review comment:
       ```suggestion
   });
   
   test('drag and drop', async () => {
   ```

##########
File path: superset-frontend/src/dashboard/components/nativeFilters/FilterBar/FilterControls/FilterControls.tsx
##########
@@ -17,16 +17,20 @@
  * under the License.
  */
 import React, { FC, useMemo, useState } from 'react';
-import { DataMask, styled, t } from '@superset-ui/core';
 import { css } from '@emotion/react';
-import * as portals from 'react-reverse-portal';
-import { DataMaskStateWithId } from 'src/dataMask/types';
+import { DataMask, styled, t } from '@superset-ui/core';
+import {
+  createHtmlPortalNode,
+  InPortal,
+  OutPortal,
+} from 'react-reverse-portal';
 import { Collapse } from 'src/common/components';
+import { DataMaskStateWithId } from 'src/dataMask/types';
+import { useDashboardHasTabs, useSelectFiltersInScope } from '../../state';
+import { Filter } from '../../types';

Review comment:
       ```suggestion
   import {
     useDashboardHasTabs,
     useSelectFiltersInScope,
   } from 'src/dashboard/components/nativeFilters/state';
   import { Filter } from 'src/dashboard/components/nativeFilters/types';
   ```

##########
File path: superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FilterConfigurePane.tsx
##########
@@ -0,0 +1,94 @@
+/**
+ * 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 { styled } from '@superset-ui/core';
+import React from 'react';
+import FilterTitlePane from './FilterTitlePane';
+import { FilterRemoval } from './types';
+
+interface Props {
+  children: (filterId: string) => React.ReactNode;
+  getFilterTitle: (filterId: string) => string;
+  onChange: (activeKey: string) => void;
+  onEdit: (filterId: string, action: 'add' | 'remove') => void;
+  onRearrange: (dragIndex: number, targetIndex: number) => void;
+  restoreFilter: (id: string) => void;
+  currentFilterId: string;
+  filterGroups: string[][];
+  removedFilters: Record<string, FilterRemoval>;
+}
+
+const Container = styled.div`
+  display: flex;
+  height: 100%;
+`;
+const ContentHolder = styled.div`

Review comment:
       ```suggestion
   `;
   
   const ContentHolder = styled.div`
   ```

##########
File path: superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigForm/FiltersConfigForm.tsx
##########
@@ -92,19 +92,25 @@ import {
 } from '../FiltersConfigModal';
 import DatasetSelect from './DatasetSelect';
 
-const { TabPane } = Tabs;
+const TabPane = styled(Tabs.TabPane)`
+  padding: ${({ theme }) => theme.gridUnit * 4}px 0px;
+`;
 
 const StyledContainer = styled.div`
+  ${({ theme }) => `
   display: flex;
   flex-direction: row-reverse;
   justify-content: space-between;
+  padding: 0px ${theme.gridUnit * 4}px;
+`}

Review comment:
       ```suggestion
     ${({ theme }) => `
       display: flex;
       flex-direction: row-reverse;
       justify-content: space-between;
       padding: 0px ${theme.gridUnit * 4}px;
     `}
   ```

##########
File path: superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FilterConfigurePane.tsx
##########
@@ -0,0 +1,94 @@
+/**
+ * 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 { styled } from '@superset-ui/core';
+import React from 'react';
+import FilterTitlePane from './FilterTitlePane';
+import { FilterRemoval } from './types';
+
+interface Props {
+  children: (filterId: string) => React.ReactNode;
+  getFilterTitle: (filterId: string) => string;
+  onChange: (activeKey: string) => void;
+  onEdit: (filterId: string, action: 'add' | 'remove') => void;
+  onRearrange: (dragIndex: number, targetIndex: number) => void;
+  restoreFilter: (id: string) => void;
+  currentFilterId: string;
+  filterGroups: string[][];
+  removedFilters: Record<string, FilterRemoval>;
+}
+
+const Container = styled.div`
+  display: flex;
+  height: 100%;
+`;
+const ContentHolder = styled.div`
+  flex-grow: 3;
+  margin-left: ${({ theme }) => theme.gridUnit * -1 - 1};
+`;
+const TitlesContainer = styled.div`
+  width: 270px;
+  border-right: 1px solid ${({ theme }) => theme.colors.grayscale.light2};
+`;
+
+const FiltureConfigurePane: React.FC<Props> = ({
+  getFilterTitle,
+  onChange,
+  onEdit,
+  onRearrange,
+  restoreFilter,
+  children,
+  currentFilterId,
+  filterGroups,
+  removedFilters,
+}) => {
+  const active = filterGroups.flat().filter(id => id === currentFilterId)[0];
+  return (
+    <Container>
+      <TitlesContainer>
+        <FilterTitlePane
+          currentFilterId={currentFilterId}
+          filterGroups={filterGroups}
+          getFilterTitle={getFilterTitle}
+          onChange={onChange}
+          onEdit={onEdit}
+          onRearrage={onRearrange}
+          onRemove={(id: string) => onEdit(id, 'remove')}
+          removedFilters={removedFilters}
+          restoreFilter={restoreFilter}
+        />
+      </TitlesContainer>
+      <ContentHolder>
+        {filterGroups.flat().map(id => (
+          <div
+            key={id}
+            style={{
+              height: '100%',
+              overflowY: 'scroll',

Review comment:
       ```suggestion
                 overflowY: 'auto',
   ```

##########
File path: superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FilterConfigurePane.tsx
##########
@@ -0,0 +1,94 @@
+/**
+ * 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 { styled } from '@superset-ui/core';
+import React from 'react';
+import FilterTitlePane from './FilterTitlePane';
+import { FilterRemoval } from './types';
+
+interface Props {
+  children: (filterId: string) => React.ReactNode;
+  getFilterTitle: (filterId: string) => string;
+  onChange: (activeKey: string) => void;
+  onEdit: (filterId: string, action: 'add' | 'remove') => void;
+  onRearrange: (dragIndex: number, targetIndex: number) => void;
+  restoreFilter: (id: string) => void;
+  currentFilterId: string;
+  filterGroups: string[][];
+  removedFilters: Record<string, FilterRemoval>;
+}
+
+const Container = styled.div`
+  display: flex;
+  height: 100%;
+`;
+const ContentHolder = styled.div`
+  flex-grow: 3;
+  margin-left: ${({ theme }) => theme.gridUnit * -1 - 1};
+`;
+const TitlesContainer = styled.div`

Review comment:
       ```suggestion
   `;
   
   const TitlesContainer = styled.div`
   ```

##########
File path: superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FilterConfigPane.test.tsx
##########
@@ -0,0 +1,103 @@
+/**
+ * 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 from 'react';
+import { dashboardLayout } from 'spec/fixtures/mockDashboardLayout';
+import { buildNativeFilter } from 'spec/fixtures/mockNativeFilters';
+import { render, screen, fireEvent } from 'spec/helpers/testing-library';
+import FilterConfigPane from './FilterConfigurePane';
+
+const defaultProps = {

Review comment:
       These new tests are generating async-related warnings. We should make sure that new tests don't throw warnings.

##########
File path: superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FiltersConfigModal.tsx
##########
@@ -249,28 +279,69 @@ export function FiltersConfigModal({
 
   const handleCancel = () => {
     const changed = form.getFieldValue('changed');
-    if (unsavedFiltersIds.length > 0 || form.isFieldsTouched() || changed) {
+    const initialOrder = buildFilterGroup(getInitialFilterHierarchy()).flat();
+    const didChangeOrder =
+      orderedFilters.flat().length !== initialOrder.length ||
+      orderedFilters.flat().some((val, index) => val !== initialOrder[index]);
+    if (
+      unsavedFiltersIds.length > 0 ||
+      form.isFieldsTouched() ||
+      changed ||
+      didChangeOrder
+    ) {
       setSaveAlertVisible(true);
     } else {
       handleConfirmCancel();
     }
   };
+  const onRearrage = (dragIndex: number, targetIndex: number) => {
+    const newOrderedFilter = orderedFilters.map(group => [...group]);
+
+    const removed = newOrderedFilter.splice(dragIndex, 1)[0];
+    newOrderedFilter.splice(targetIndex, 0, removed);
+    setOrderedFilters(newOrderedFilter);
+  };
+  const handleFilterHierarchyChange = useCallback(
+    (filterId: string, parentFilter?: { value: string; label: string }) => {
+      const index = filterHierarchy.findIndex(item => item.id === filterId);
+      const newState = [...filterHierarchy];
+      newState.splice(index, 1, {
+        id: filterId,
+        parentId: parentFilter ? parentFilter.value : null,
+      });
+      setFilterHierarchy(newState);
+      setOrderedFilters(buildFilterGroup(newState));
+    },
+    [setFilterHierarchy, setOrderedFilters, filterHierarchy],
+  );
 
   const onValuesChange = useMemo(
-    () =>
-      debounce((changes: any, values: NativeFiltersForm) => {

Review comment:
       We shouldn't remove the `debounce` here.

##########
File path: superset-frontend/src/dashboard/components/nativeFilters/FiltersConfigModal/FilterTitleContainer.tsx
##########
@@ -0,0 +1,162 @@
+/**
+ * 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 from 'react';
+import { styled, t } from '@superset-ui/core';
+import Icons from 'src/components/Icons';
+import { FilterRemoval } from './types';
+import DraggableFilter from './DraggableFilter';
+
+const FilterTitle = styled.div`
+  ${({ theme }) => `
+      display: flex;
+      padding: ${theme.gridUnit * 2}px;
+      width: 100%;
+      border-radius: ${theme.borderRadius}px;
+      &.active {
+        color: ${theme.colors.grayscale.dark1};
+        border-radius: ${theme.borderRadius}px;
+        background-color: ${theme.colors.secondary.light4};
+        span, .anticon {
+          color: ${theme.colors.grayscale.dark1};
+        }
+      }
+      &:hover {
+        color: ${theme.colors.primary.light1};
+        span, .anticon {
+          color: ${theme.colors.primary.light1};
+        }
+      }
+`}
+`;
+
+const StyledTrashIcon = styled(Icons.Trash)`
+  color: ${({ theme }) => theme.colors.grayscale.light3};
+`;
+const Container = styled.div`

Review comment:
       ```suggestion
   `;
   
   const Container = styled.div`
   ```




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org