mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00: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 {
|
static get styles(): CSSResult {
|
||||||
return css`
|
return css`
|
||||||
:host {
|
ha-toast {
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
|
justify-content: space-between;
|
||||||
}
|
}
|
||||||
mwc-button {
|
mwc-button {
|
||||||
color: var(--primary-color);
|
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 = (
|
export const swapCard = (
|
||||||
config: LovelaceConfig,
|
config: LovelaceConfig,
|
||||||
path1: [number, number],
|
path1: [number, number],
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { Lovelace } from "../types";
|
import { Lovelace } from "../types";
|
||||||
import { deleteCard } from "./config-util";
|
import { deleteCard, insertCard } from "./config-util";
|
||||||
import { showAlertDialog } from "../../../dialogs/generic/show-dialog-box";
|
import { showAlertDialog } from "../../../dialogs/generic/show-dialog-box";
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
import { showDeleteCardDialog } from "./card-editor/show-delete-card-dialog";
|
import { showDeleteCardDialog } from "./card-editor/show-delete-card-dialog";
|
||||||
@ -16,8 +16,12 @@ export async function confDeleteCard(
|
|||||||
cardConfig,
|
cardConfig,
|
||||||
deleteCard: async () => {
|
deleteCard: async () => {
|
||||||
try {
|
try {
|
||||||
await lovelace.saveConfig(deleteCard(lovelace.config, path));
|
const newLovelace = deleteCard(lovelace.config, path);
|
||||||
showDeleteSuccessToast(element, hass!);
|
await lovelace.saveConfig(newLovelace);
|
||||||
|
const action = async () => {
|
||||||
|
await lovelace.saveConfig(insertCard(newLovelace, path, cardConfig));
|
||||||
|
};
|
||||||
|
showDeleteSuccessToast(element, hass!, action);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
showAlertDialog(element, {
|
showAlertDialog(element, {
|
||||||
text: `Deleting failed: ${err.message}`,
|
text: `Deleting failed: ${err.message}`,
|
||||||
|
@ -524,6 +524,7 @@
|
|||||||
"cancel": "Cancel",
|
"cancel": "Cancel",
|
||||||
"delete": "Delete",
|
"delete": "Delete",
|
||||||
"close": "Close",
|
"close": "Close",
|
||||||
|
"undo": "Undo",
|
||||||
"save": "Save",
|
"save": "Save",
|
||||||
"yes": "Yes",
|
"yes": "Yes",
|
||||||
"no": "No",
|
"no": "No",
|
||||||
|
@ -1,7 +1,19 @@
|
|||||||
import { showToast } from "./toast";
|
import { showToast } from "./toast";
|
||||||
import { HomeAssistant } from "../types";
|
import { HomeAssistant } from "../types";
|
||||||
|
import { ShowToastParams } from "../managers/notification-manager";
|
||||||
|
|
||||||
export const showDeleteSuccessToast = (el: HTMLElement, hass: HomeAssistant) =>
|
export const showDeleteSuccessToast = (
|
||||||
showToast(el, {
|
el: HTMLElement,
|
||||||
|
hass: HomeAssistant,
|
||||||
|
action?: () => void
|
||||||
|
) => {
|
||||||
|
const toastParams: ShowToastParams = {
|
||||||
message: hass!.localize("ui.common.successfully_deleted"),
|
message: hass!.localize("ui.common.successfully_deleted"),
|
||||||
});
|
};
|
||||||
|
|
||||||
|
if (action) {
|
||||||
|
toastParams.action = { action, text: hass!.localize("ui.common.undo") };
|
||||||
|
}
|
||||||
|
|
||||||
|
showToast(el, toastParams);
|
||||||
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user