rusackas commented on a change in pull request #9051: [explore] Modal to edit 
chart properties
URL: 
https://github.com/apache/incubator-superset/pull/9051#discussion_r373241584
 
 

 ##########
 File path: superset/assets/src/explore/components/PropertiesModal.jsx
 ##########
 @@ -0,0 +1,241 @@
+/**
+ * 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, useEffect, useRef } from 'react';
+import { connect } from 'react-redux';
+import { bindActionCreators } from 'redux';
+import {
+  Button,
+  Modal,
+  Row,
+  Col,
+  FormControl,
+  FormGroup,
+} from 'react-bootstrap';
+import Dialog from 'react-bootstrap-dialog';
+import Select from 'react-select';
+import { t } from '@superset-ui/translation';
+import { SupersetClient } from '@superset-ui/connection';
+
+import { sliceUpdated } from '../actions/exploreActions';
+import getClientErrorObject from '../../utils/getClientErrorObject';
+
+function PropertiesModalWrapper({ show, onHide, animation, slice, onSave }) {
+  // The wrapper is a separate component so that hooks only run when the modal 
opens
+  return (
+    <Modal show={show} onHide={onHide} animation={animation} bsSize="large">
+      <PropertiesModal slice={slice} onHide={onHide} onSave={onSave} />
+    </Modal>
+  );
+}
+
+function PropertiesModal({ slice, onHide, onSave }) {
+  const [submitting, setSubmitting] = useState(false);
+  const errorDialog = useRef();
+
+  function showError({ error, statusText }) {
+    errorDialog.current.show({
+      title: 'Error',
+      bsSize: 'medium',
+      bsStyle: 'danger',
+      actions: [Dialog.DefaultAction('Ok', () => {}, 'btn-danger')],
+      body: error || statusText || t('An error has occurred'),
+    });
+  }
+
+  // values of form inputs
+  const [name, setName] = useState(slice.slice_name || '');
+  const [description, setDescription] = useState(slice.description || '');
+  const [cacheTimeout, setCacheTimeout] = useState(slice.cache_timeout || '');
+  const [owners, setOwners] = useState(null);
+
+  async function fetchOwners() {
+    try {
+      const res = await SupersetClient.get({
+        endpoint: `/api/v1/chart/${slice.slice_id}`,
+      });
+      setOwners(
+        res.json.result.owners.map(owner => ({
+          value: owner.id,
+          label: owner.username,
+        })),
+      );
+    } catch (res) {
+      const errObj = await getClientErrorObject(res);
+      showError(errObj);
+    }
+  }
+
+  // get the owners of this slice
+  useEffect(() => {
+    fetchOwners();
+  }, []);
+
+  // get the list of users who can own a chart
+  const [ownerOptions, setUserOptions] = useState(null);
+  useEffect(() => {
+    SupersetClient.get({
+      endpoint: `/api/v1/chart/related/owners`,
+    }).then(res => {
+      setUserOptions(
+        res.json.result.map(item => ({
+          value: item.value,
+          label: item.text,
+        })),
+      );
+    });
+  }, []);
+
+  const onSubmit = async event => {
+    event.stopPropagation();
+    event.preventDefault();
+    setSubmitting(true);
+    const payload = {
+      slice_name: name || null,
+      description: description || null,
+      cache_timeout: cacheTimeout || null,
+      owners: owners.map(o => o.value),
+    };
+    try {
+      const res = await SupersetClient.put({
+        endpoint: `/api/v1/chart/${slice.slice_id}`,
+        headers: { 'Content-Type': 'application/json' },
+        body: JSON.stringify(payload),
+      });
+      // update the redux state
+      onSave(res.json.result);
+      onHide();
+    } catch (res) {
+      const errObj = await getClientErrorObject(res);
+      showError(errObj);
+    }
+    setSubmitting(false);
+  };
+
+  return (
+    <form onSubmit={onSubmit}>
+      <Modal.Header closeButton>
+        <Modal.Title>Edit Chart Properties</Modal.Title>
+      </Modal.Header>
+      <Modal.Body>
+        <Row>
+          <Col md={6}>
+            <h3>{t('Basic Information')}</h3>
+            <FormGroup>
+              <label className="control-label" htmlFor="name">
+                {t('Name')}
+              </label>
+              <FormControl
+                name="name"
+                type="text"
+                bsSize="sm"
+                value={name}
+                onChange={event => setName(event.target.value)}
+              />
+            </FormGroup>
+            <FormGroup>
+              <label className="control-label" htmlFor="description">
+                {t('Description')}
+              </label>
+              <FormControl
+                name="description"
+                type="text"
+                componentClass="textarea"
+                bsSize="sm"
+                value={description}
+                onChange={event => setDescription(event.target.value)}
+                style={{ maxWidth: '100%' }}
 
 Review comment:
   Inline styles!? Not sure the exact issue this addresses, but we can probably 
just slap this on all FormControl components via the theme if needed, so it 
needn't be repeated in the future.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to