Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: alert & reports active toggle optimistic update #20402

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we display a toast for the user like "An error occurred while changing the active state"?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are. The updateResource comes from the useSingleViewResource hook, which accepts a function to handle errors and we're passing addDangerToast to it.

}
},
[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