Skip to content

Commit

Permalink
fix: alert & reports active toggle optimistic update (#20402)
Browse files Browse the repository at this point in the history
  • Loading branch information
diegomedina248 authored Jun 23, 2022
1 parent a169b60 commit 4dc3044
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
39 changes: 29 additions & 10 deletions superset-frontend/src/views/CRUD/alert/AlertList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import React, { useState, useMemo, useEffect } from 'react';
import React, { useState, useMemo, useEffect, useCallback } from 'react';
import { useHistory } from 'react-router-dom';
import { t, SupersetClient, makeApi, styled } from '@superset-ui/core';
import moment from 'moment';
Expand Down Expand Up @@ -109,6 +109,7 @@ function AlertList({
},
hasPerm,
fetchData,
setResourceCollection,
refreshData,
toggleBulkSelect,
} = useListViewResource<AlertObject>(
Expand Down Expand Up @@ -188,14 +189,32 @@ function AlertList({

const initialSort = [{ id: 'name', desc: true }];

const toggleActive = (data: AlertObject, checked: boolean) => {
if (data && data.id) {
const update_id = data.id;
updateResource(update_id, { active: checked }).then(() => {
refreshData();
});
}
};
const toggleActive = useCallback(
(data: AlertObject, checked: boolean) => {
if (data && data.id) {
const update_id = data.id;
const original = [...alerts];

setResourceCollection(
original.map(alert => {
if (alert?.id === data.id) {
return {
...alert,
active: checked,
};
}

return alert;
}),
);

updateResource(update_id, { active: checked }, false, false)
.then()
.catch(() => setResourceCollection(original));
}
},
[alerts, setResourceCollection, updateResource],
);

const columns = useMemo(
() => [
Expand Down Expand Up @@ -357,7 +376,7 @@ function AlertList({
size: 'xl',
},
],
[canDelete, canEdit, isReportEnabled],
[canDelete, canEdit, isReportEnabled, toggleActive],
);

const subMenuButtons: SubMenuProps['buttons'] = [];
Expand Down
15 changes: 10 additions & 5 deletions superset-frontend/src/views/CRUD/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,13 @@ export function useSingleViewResource<D extends object = any>(
);

const updateResource = useCallback(
(resourceID: number, resource: D, hideToast = false) => {
(resourceID: number, resource: D, hideToast = false, setLoading = true) => {
// Set loading state
updateState({
loading: true,
});
if (setLoading) {
updateState({
loading: true,
});
}

return SupersetClient.put({
endpoint: `/api/v1/${resourceName}/${resourceID}`,
Expand Down Expand Up @@ -354,11 +356,14 @@ export function useSingleViewResource<D extends object = any>(
}),
)
.finally(() => {
updateState({ loading: false });
if (setLoading) {
updateState({ loading: false });
}
});
},
[handleErrorMsg, resourceName, resourceLabel],
);

const clearError = () =>
updateState({
error: null,
Expand Down

0 comments on commit 4dc3044

Please sign in to comment.