mirror of
https://github.com/home-assistant/frontend
synced 2024-09-28 00:43:28 +02:00
Delete Card Action: Undo in Toast (#5405)
* Adding insert card and delete undo * Fix Toast style * Localize * Optional * Comments
This commit is contained in:
parent
81d2334f48
commit
0f2eae4091
@ -82,9 +82,10 @@ class NotificationManager extends LitElement {
|
||||
|
||||
static get styles(): CSSResult {
|
||||
return css`
|
||||
:host {
|
||||
ha-toast {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
}
|
||||
mwc-button {
|
||||
color: var(--primary-color);
|
||||
|
@ -119,6 +119,40 @@ export const deleteCard = (
|
||||
};
|
||||
};
|
||||
|
||||
export const insertCard = (
|
||||
config: LovelaceConfig,
|
||||
path: [number, number],
|
||||
cardConfig: LovelaceCardConfig
|
||||
) => {
|
||||
const [viewIndex, cardIndex] = path;
|
||||
const views: LovelaceViewConfig[] = [];
|
||||
|
||||
config.views.forEach((viewConf, index) => {
|
||||
if (index !== viewIndex) {
|
||||
views.push(config.views[index]);
|
||||
return;
|
||||
}
|
||||
|
||||
const cards = viewConf.cards
|
||||
? [
|
||||
...viewConf.cards.slice(0, cardIndex),
|
||||
cardConfig,
|
||||
...viewConf.cards.slice(cardIndex),
|
||||
]
|
||||
: [cardConfig];
|
||||
|
||||
views.push({
|
||||
...viewConf,
|
||||
cards,
|
||||
});
|
||||
});
|
||||
|
||||
return {
|
||||
...config,
|
||||
views,
|
||||
};
|
||||
};
|
||||
|
||||
export const swapCard = (
|
||||
config: LovelaceConfig,
|
||||
path1: [number, number],
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Lovelace } from "../types";
|
||||
import { deleteCard } from "./config-util";
|
||||
import { deleteCard, insertCard } from "./config-util";
|
||||
import { showAlertDialog } from "../../../dialogs/generic/show-dialog-box";
|
||||
import { HomeAssistant } from "../../../types";
|
||||
import { showDeleteCardDialog } from "./card-editor/show-delete-card-dialog";
|
||||
@ -16,8 +16,12 @@ export async function confDeleteCard(
|
||||
cardConfig,
|
||||
deleteCard: async () => {
|
||||
try {
|
||||
await lovelace.saveConfig(deleteCard(lovelace.config, path));
|
||||
showDeleteSuccessToast(element, hass!);
|
||||
const newLovelace = deleteCard(lovelace.config, path);
|
||||
await lovelace.saveConfig(newLovelace);
|
||||
const action = async () => {
|
||||
await lovelace.saveConfig(insertCard(newLovelace, path, cardConfig));
|
||||
};
|
||||
showDeleteSuccessToast(element, hass!, action);
|
||||
} catch (err) {
|
||||
showAlertDialog(element, {
|
||||
text: `Deleting failed: ${err.message}`,
|
||||
|
@ -524,6 +524,7 @@
|
||||
"cancel": "Cancel",
|
||||
"delete": "Delete",
|
||||
"close": "Close",
|
||||
"undo": "Undo",
|
||||
"save": "Save",
|
||||
"yes": "Yes",
|
||||
"no": "No",
|
||||
|
@ -1,7 +1,19 @@
|
||||
import { showToast } from "./toast";
|
||||
import { HomeAssistant } from "../types";
|
||||
import { ShowToastParams } from "../managers/notification-manager";
|
||||
|
||||
export const showDeleteSuccessToast = (el: HTMLElement, hass: HomeAssistant) =>
|
||||
showToast(el, {
|
||||
export const showDeleteSuccessToast = (
|
||||
el: HTMLElement,
|
||||
hass: HomeAssistant,
|
||||
action?: () => void
|
||||
) => {
|
||||
const toastParams: ShowToastParams = {
|
||||
message: hass!.localize("ui.common.successfully_deleted"),
|
||||
});
|
||||
};
|
||||
|
||||
if (action) {
|
||||
toastParams.action = { action, text: hass!.localize("ui.common.undo") };
|
||||
}
|
||||
|
||||
showToast(el, toastParams);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user