Antonio-RiveroMartnez commented on code in PR #19244:
URL: https://github.com/apache/superset/pull/19244#discussion_r914211418


##########
superset-frontend/src/dashboard/actions/sliceEntities.js:
##########
@@ -38,96 +44,142 @@ export function fetchAllSlicesFailed(error) {
   return { type: FETCH_ALL_SLICES_FAILED, payload: { error } };
 }
 
-export function getDatasourceParameter(datasourceId, datasourceType) {
-  return `${datasourceId}__${datasourceType}`;
+export function fetchSlices(
+  userId,

Review Comment:
   Could we send these as an `Option object`? So we don't have to worry about 
the order we send it and we avoid sending `undefined` when calling it? So we 
just send what options we need and we destructure here to get them?



##########
superset-frontend/src/dashboard/actions/sliceEntities.js:
##########
@@ -38,96 +44,142 @@ export function fetchAllSlicesFailed(error) {
   return { type: FETCH_ALL_SLICES_FAILED, payload: { error } };
 }
 
-export function getDatasourceParameter(datasourceId, datasourceType) {
-  return `${datasourceId}__${datasourceType}`;
+export function fetchSlices(
+  userId,
+  excludeFilterBox,
+  dispatch,
+  filter_value,
+  sortColumn = 'changed_on',
+  slices = {},
+) {
+  const additional_filters = filter_value
+    ? [{ col: 'slice_name', opr: 'chart_all_text', value: filter_value }]
+    : [];
+
+  const cloneSlices = { ...slices };
+
+  return SupersetClient.get({
+    endpoint: `/api/v1/chart/?q=${rison.encode({
+      columns: [
+        'changed_on_delta_humanized',
+        'changed_on_utc',
+        'datasource_id',
+        'datasource_type',
+        'datasource_url',
+        'datasource_name_text',
+        'description_markeddown',
+        'description',
+        'id',
+        'params',
+        'slice_name',
+        'url',
+        'viz_type',
+      ],
+      filters: [
+        { col: 'owners', opr: 'rel_m_m', value: userId },
+        ...additional_filters,
+      ],
+      page_size: FETCH_SLICES_PAGE_SIZE,
+      order_column:
+        sortColumn === 'changed_on' ? 'changed_on_delta_humanized' : 
sortColumn,
+      order_direction: sortColumn === 'changed_on' ? 'desc' : 'asc',
+    })}`,
+  })
+    .then(({ json }) => {
+      let { result } = json;
+      // disable add filter_box viz to dashboard
+      if (excludeFilterBox) {
+        result = result.filter(slice => slice.viz_type !== 'filter_box');
+      }
+      result.forEach(slice => {
+        let form_data = JSON.parse(slice.params);
+        form_data = {
+          ...form_data,
+          // force using datasource stored in relational table prop
+          datasource:
+            getDatasourceParameter(
+              slice.datasource_id,
+              slice.datasource_type,
+            ) || form_data.datasource,
+        };
+        cloneSlices[slice.id] = {
+          slice_id: slice.id,
+          slice_url: slice.url,
+          slice_name: slice.slice_name,
+          form_data,
+          datasource_name: slice.datasource_name_text,
+          datasource_url: slice.datasource_url,
+          datasource_id: slice.datasource_id,
+          datasource_type: slice.datasource_type,
+          changed_on: new Date(slice.changed_on_utc).getTime(),
+          description: slice.description,
+          description_markdown: slice.description_markeddown,
+          viz_type: slice.viz_type,
+          modified: slice.changed_on_delta_humanized,
+          changed_on_humanized: slice.changed_on_delta_humanized,
+        };
+      });
+
+      return dispatch(setAllSlices(cloneSlices));
+    })
+    .catch(errorResponse =>
+      getClientErrorObject(errorResponse).then(({ error }) => {
+        dispatch(
+          fetchAllSlicesFailed(error || t('Could not fetch all saved charts')),
+        );
+        dispatch(
+          addDangerToast(
+            t('Sorry there was an error fetching saved charts: ') + error,
+          ),
+        );
+      }),
+    );
 }
 
-const FETCH_SLICES_PAGE_SIZE = 200;
 export function fetchAllSlices(userId, excludeFilterBox = false) {
   return (dispatch, getState) => {
     const { sliceEntities } = getState();
     if (sliceEntities.lastUpdated === 0) {
       dispatch(fetchAllSlicesStarted());
-
-      return SupersetClient.get({
-        endpoint: `/api/v1/chart/?q=${rison.encode({
-          columns: [
-            'changed_on_delta_humanized',
-            'changed_on_utc',
-            'datasource_id',
-            'datasource_type',
-            'datasource_url',
-            'datasource_name_text',
-            'description_markeddown',
-            'description',
-            'id',
-            'params',
-            'slice_name',
-            'url',
-            'viz_type',
-          ],
-          filters: [{ col: 'owners', opr: 'rel_m_m', value: userId }],
-          page_size: FETCH_SLICES_PAGE_SIZE,
-          order_column: 'changed_on_delta_humanized',
-          order_direction: 'desc',
-        })}`,
-      })
-        .then(({ json }) => {
-          const slices = {};
-          let { result } = json;
-          // disable add filter_box viz to dashboard
-          if (excludeFilterBox) {
-            result = result.filter(slice => slice.viz_type !== 'filter_box');
-          }
-          result.forEach(slice => {
-            let form_data = JSON.parse(slice.params);
-            form_data = {
-              ...form_data,
-              // force using datasource stored in relational table prop
-              datasource:
-                getDatasourceParameter(
-                  slice.datasource_id,
-                  slice.datasource_type,
-                ) || form_data.datasource,
-            };
-            slices[slice.id] = {
-              slice_id: slice.id,
-              slice_url: slice.url,
-              slice_name: slice.slice_name,
-              form_data,
-              datasource_name: slice.datasource_name_text,
-              datasource_url: slice.datasource_url,
-              datasource_id: slice.datasource_id,
-              datasource_type: slice.datasource_type,
-              changed_on: new Date(slice.changed_on_utc).getTime(),
-              description: slice.description,
-              description_markdown: slice.description_markeddown,
-              viz_type: slice.viz_type,
-              modified: slice.changed_on_delta_humanized,
-              changed_on_humanized: slice.changed_on_delta_humanized,
-            };
-          });
-
-          return dispatch(setAllSlices(slices));
-        })
-        .catch(
-          errorResponse =>
-            console.log(errorResponse) ||
-            getClientErrorObject(errorResponse).then(({ error }) => {
-              dispatch(
-                fetchAllSlicesFailed(
-                  error || t('Could not fetch all saved charts'),
-                ),
-              );
-              dispatch(
-                addDangerToast(
-                  t('Sorry there was an error fetching saved charts: ') + 
error,
-                ),
-              );
-            }),
-        );
+      return fetchSlices(userId, excludeFilterBox, dispatch, undefined);
     }
 
     return dispatch(setAllSlices(sliceEntities.slices));
   };
 }
+
+export function fetchSortedSlices(
+  userId,
+  excludeFilterBox = false,
+  order_column,
+) {
+  return dispatch => {
+    dispatch(fetchAllSlicesStarted());
+    return fetchSlices(
+      userId,
+      excludeFilterBox,
+      dispatch,
+      undefined,

Review Comment:
   If passed as an `Option object` there's no need to worry about order or 
sending undefined.



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

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to