mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Prevent creating views with duplicate URL (#17871)
This commit is contained in:
parent
74be4ae20a
commit
0ffc7b59d6
@ -3,6 +3,7 @@ import {
|
|||||||
LovelaceConfig,
|
LovelaceConfig,
|
||||||
LovelaceViewConfig,
|
LovelaceViewConfig,
|
||||||
} from "../../../data/lovelace";
|
} from "../../../data/lovelace";
|
||||||
|
import type { HomeAssistant } from "../../../types";
|
||||||
|
|
||||||
export const addCard = (
|
export const addCard = (
|
||||||
config: LovelaceConfig,
|
config: LovelaceConfig,
|
||||||
@ -253,23 +254,44 @@ export const moveCard = (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const addView = (
|
export const addView = (
|
||||||
|
hass: HomeAssistant,
|
||||||
config: LovelaceConfig,
|
config: LovelaceConfig,
|
||||||
viewConfig: LovelaceViewConfig
|
viewConfig: LovelaceViewConfig
|
||||||
): LovelaceConfig => ({
|
): LovelaceConfig => {
|
||||||
...config,
|
if (viewConfig.path && config.views.some((v) => v.path === viewConfig.path)) {
|
||||||
views: config.views.concat(viewConfig),
|
throw new Error(
|
||||||
});
|
hass.localize("ui.panel.lovelace.editor.edit_view.error_same_url")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
...config,
|
||||||
|
views: config.views.concat(viewConfig),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export const replaceView = (
|
export const replaceView = (
|
||||||
|
hass: HomeAssistant,
|
||||||
config: LovelaceConfig,
|
config: LovelaceConfig,
|
||||||
viewIndex: number,
|
viewIndex: number,
|
||||||
viewConfig: LovelaceViewConfig
|
viewConfig: LovelaceViewConfig
|
||||||
): LovelaceConfig => ({
|
): LovelaceConfig => {
|
||||||
...config,
|
if (
|
||||||
views: config.views.map((origView, index) =>
|
viewConfig.path &&
|
||||||
index === viewIndex ? viewConfig : origView
|
config.views.some(
|
||||||
),
|
(v, idx) => v.path === viewConfig.path && idx !== viewIndex
|
||||||
});
|
)
|
||||||
|
) {
|
||||||
|
throw new Error(
|
||||||
|
hass.localize("ui.panel.lovelace.editor.edit_view.error_same_url")
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
...config,
|
||||||
|
views: config.views.map((origView, index) =>
|
||||||
|
index === viewIndex ? viewConfig : origView
|
||||||
|
),
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
export const swapView = (
|
export const swapView = (
|
||||||
config: LovelaceConfig,
|
config: LovelaceConfig,
|
||||||
|
@ -405,8 +405,13 @@ export class HuiDialogEditView extends LitElement {
|
|||||||
try {
|
try {
|
||||||
await lovelace.saveConfig(
|
await lovelace.saveConfig(
|
||||||
this._creatingView
|
this._creatingView
|
||||||
? addView(lovelace.config, viewConf)
|
? addView(this.hass!, lovelace.config, viewConf)
|
||||||
: replaceView(lovelace.config, this._params.viewIndex!, viewConf)
|
: replaceView(
|
||||||
|
this.hass!,
|
||||||
|
lovelace.config,
|
||||||
|
this._params.viewIndex!,
|
||||||
|
viewConf
|
||||||
|
)
|
||||||
);
|
);
|
||||||
if (this._params.saveCallback) {
|
if (this._params.saveCallback) {
|
||||||
this._params.saveCallback(
|
this._params.saveCallback(
|
||||||
@ -417,7 +422,9 @@ export class HuiDialogEditView extends LitElement {
|
|||||||
this.closeDialog();
|
this.closeDialog();
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
showAlertDialog(this, {
|
showAlertDialog(this, {
|
||||||
text: `Saving failed: ${err.message}`,
|
text: `${this.hass!.localize(
|
||||||
|
"ui.panel.lovelace.editor.edit_view.saving_failed"
|
||||||
|
)}: ${err.message}`,
|
||||||
});
|
});
|
||||||
} finally {
|
} finally {
|
||||||
this._saving = false;
|
this._saving = false;
|
||||||
|
@ -4641,7 +4641,9 @@
|
|||||||
"subview": "Subview",
|
"subview": "Subview",
|
||||||
"subview_helper": "Subviews don't appear in tabs and have a back button.",
|
"subview_helper": "Subviews don't appear in tabs and have a back button.",
|
||||||
"edit_ui": "Edit in visual editor",
|
"edit_ui": "Edit in visual editor",
|
||||||
"edit_yaml": "Edit in YAML"
|
"edit_yaml": "Edit in YAML",
|
||||||
|
"saving_failed": "Saving failed",
|
||||||
|
"error_same_url": "You cannot save a view with the same URL as a different existing view."
|
||||||
},
|
},
|
||||||
"edit_badges": {
|
"edit_badges": {
|
||||||
"view_no_badges": "Badges are not be supported by the current view type."
|
"view_no_badges": "Badges are not be supported by the current view type."
|
||||||
|
Loading…
x
Reference in New Issue
Block a user