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 2020/03/12 01:52:55 UTC

[GitHub] [incubator-superset] suddjian commented on a change in pull request #9197: [datasets] new, listview (react)

suddjian commented on a change in pull request #9197: [datasets] new, listview (react)
URL: https://github.com/apache/incubator-superset/pull/9197#discussion_r391362313
 
 

 ##########
 File path: superset-frontend/src/views/datasetList/DatasetList.tsx
 ##########
 @@ -0,0 +1,423 @@
+/**
+ * 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 { SupersetClient } from '@superset-ui/connection';
+import { t } from '@superset-ui/translation';
+import moment from 'moment';
+import PropTypes from 'prop-types';
+import React from 'react';
+// @ts-ignore
+import { Panel } from 'react-bootstrap';
+import ConfirmStatusChange from 'src/components/ConfirmStatusChange';
+import ListView from 'src/components/ListView/ListView';
+import {
+  FetchDataConfig,
+  FilterOperatorMap,
+  Filters,
+} from 'src/components/ListView/types';
+import withToasts from 'src/messageToasts/enhancers/withToasts';
+
+const PAGE_SIZE = 25;
+
+interface Props {
+  addDangerToast: (msg: string) => void;
+  addSuccessToast: (msg: string) => void;
+}
+
+interface State {
+  datasets: any[];
+  datasetCount: number;
+  loading: boolean;
+  filterOperators: FilterOperatorMap;
+  filters: Filters;
+  owners: Array<{ text: string; value: number }>;
+  databases: Array<{ text: string; value: number }>;
+  permissions: string[];
+  lastFetchDataConfig: FetchDataConfig | null;
+}
+
+interface Dataset {
+  changed_by_name: string;
+  changed_by_url: string;
+  changed_by: string;
+  changed_on: string;
+  databse_name: string;
+  explore_url: string;
+  id: number;
+  schema: string;
+  table_name: string;
+}
+
+class DatasetList extends React.PureComponent<Props, State> {
+  static propTypes = {
+    addDangerToast: PropTypes.func.isRequired,
+  };
+
+  state: State = {
+    datasetCount: 0,
+    datasets: [],
+    filterOperators: {},
+    filters: [],
+    lastFetchDataConfig: null,
+    loading: false,
+    owners: [],
+    databases: [],
+    permissions: [],
+  };
+
+  componentDidMount() {
+    Promise.all([
+      SupersetClient.get({
+        endpoint: `/api/v1/dataset/_info`,
+      }),
+      SupersetClient.get({
+        endpoint: `/api/v1/dataset/related/owners`,
+      }),
+      SupersetClient.get({
+        endpoint: `/api/v1/dataset/related/database`,
+      }),
+    ]).then(
+      ([{ json: infoJson = {} }, { json: ownersJson = {} }, { json: databasesJson = {} }]) => {
+        this.setState(
+          {
+            filterOperators: infoJson.filters,
+            owners: ownersJson.result,
+            databases: databasesJson.result,
+            permissions: infoJson.permissions,
+          },
+          this.updateFilters,
+        );
+      },
+      ([e1, e2]) => {
+        this.props.addDangerToast(
+          t('An error occurred while fetching Datasets'),
+        );
+        if (e1) {
+          console.error(e1);
+        }
+        if (e2) {
+          console.error(e2);
+        }
+      },
+    );
+  }
+
+  get canEdit() {
+    return this.hasPerm('can_edit');
+  }
+
+  get canDelete() {
+    return this.hasPerm('can_delete');
+  }
+
+  initialSort = [{ id: 'changed_on', desc: true }];
+
+  columns = [
+    {
+      Cell: ({
+        row: {
+          original: { explore_url: exploreUrl, table_name: datasetTitle },
+        },
+      }: any) => <a href={exploreUrl}>{datasetTitle}</a>,
+      Header: t('Table'),
+      accessor: 'table_name',
+    },
+    {
+      Header: t('Databse'),
+      accessor: 'database_name',
+    },
+    {
+      Cell: ({
+        row: {
+          original: {
+            changed_by_name: changedByName,
+            changed_by_url: changedByUrl,
+          },
+        },
+      }: any) => <a href={changedByUrl}>{changedByName}</a>,
+      Header: t('Changed By'),
+      accessor: 'changed_by_fk',
+    },
+    {
+      Cell: ({
+        row: {
+          original: { changed_on: changedOn },
+        },
+      }: any) => <span className="no-wrap">{moment(changedOn).fromNow()}</span>,
+      Header: t('Modified'),
+      accessor: 'changed_on',
+      sortable: true,
+    },
+    {
+      accessor: 'database',
+      hidden: true
+    },
+    {
+      accessor: 'schema',
+      hidden: true
+    },
+    {
+      accessor: 'owners',
+      hidden: true
+    },
+    {
+      accessor: 'is_sqllab_view',
+      hidden: true
+    },
+    {
+      Cell: ({ row: { state, original } }: any) => {
+        const handleDelete = () => this.handleDashboardDelete(original);
+        const handleEdit = () => this.handleDashboardEdit(original);
+        if (!this.canEdit && !this.canDelete) {
+          return null;
+        }
+        return (
+          <span
+            className={`actions ${state && state.hover ? '' : 'invisible'}`}
+          >
+            {this.canDelete && (
+              <ConfirmStatusChange
+                title={t('Please Confirm')}
+                description={
+                  <>
+                    {t('Are you sure you want to delete')}{' '}
+                    <b>{original.dashboard_title}</b>?
+                  </>
+                }
+                onConfirm={handleDelete}
+              >
+                {confirmDelete => (
+                  <span
+                    role="button"
+                    tabIndex={0}
+                    className="action-button"
+                    onClick={confirmDelete}
+                  >
+                    <i className="fa fa-trash" />
+                  </span>
+                )}
+              </ConfirmStatusChange>
+            )}
+            {this.canEdit && (
+              <span
+                role="button"
+                tabIndex={0}
+                className="action-button"
+                onClick={handleEdit}
+              >
+                <i className="fa fa-pencil" />
+              </span>
+            )}
+          </span>
+        );
+      },
+      Header: t('Actions'),
+      id: 'actions',
+    },
+  ];
+
+  hasPerm = (perm: string) => {
+    if (!this.state.permissions.length) {
+      return false;
+    }
+
+    return Boolean(this.state.permissions.find(p => p === perm));
+  };
+
+  handleDashboardEdit = ({ id }: { id: number }) => {
+    window.location.assign(`/tablemodelview/edit/${id}`);
+  };
+
+  handleDashboardDelete = ({ id, table_name: tableName }: Dataset) =>
+    SupersetClient.delete({
+      endpoint: `/api/v1/dashboard/${id}`,
 
 Review comment:
   I think you want a different endpoint here

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

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