Edit Person button in the Person "more info" dialog (fixes #4706) (#7208)

Co-authored-by: Zack Barett <zackbarett@hey.com>
This commit is contained in:
Spencer Williams 2020-10-08 10:40:55 -04:00 committed by GitHub
parent 426a7ac8dd
commit 0405adcd16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 13 deletions

View File

@ -40,7 +40,14 @@ import "./ha-more-info-logbook";
import "./controls/more-info-default"; import "./controls/more-info-default";
const DOMAINS_NO_INFO = ["camera", "configurator"]; const DOMAINS_NO_INFO = ["camera", "configurator"];
/**
* Entity domains that should be editable *if* they have an id present;
* {@see shouldShowEditIcon}.
* */
const EDITABLE_DOMAINS_WITH_ID = ["scene", "automation"]; const EDITABLE_DOMAINS_WITH_ID = ["scene", "automation"];
/**
* Entity Domains that should always be editable; {@see shouldShowEditIcon}.
* */
const EDITABLE_DOMAINS = ["script"]; const EDITABLE_DOMAINS = ["script"];
export interface MoreInfoDialogParams { export interface MoreInfoDialogParams {
@ -73,6 +80,20 @@ export class MoreInfoDialog extends LitElement {
fireEvent(this, "dialog-closed", { dialog: this.localName }); fireEvent(this, "dialog-closed", { dialog: this.localName });
} }
protected shouldShowEditIcon(domain, stateObj): boolean {
if (EDITABLE_DOMAINS_WITH_ID.includes(domain) && stateObj.attributes.id) {
return true;
}
if (EDITABLE_DOMAINS.includes(domain)) {
return true;
}
if (domain === "person" && stateObj.attributes.editable !== "false") {
return true;
}
return false;
}
protected updated(changedProperties) { protected updated(changedProperties) {
if (!this.hass || !this._entityId || !changedProperties.has("_entityId")) { if (!this.hass || !this._entityId || !changedProperties.has("_entityId")) {
return; return;
@ -137,10 +158,7 @@ export class MoreInfoDialog extends LitElement {
</mwc-icon-button> </mwc-icon-button>
` `
: ""} : ""}
${this.hass.user!.is_admin && ${this.shouldShowEditIcon(domain, stateObj)
((EDITABLE_DOMAINS_WITH_ID.includes(domain) &&
stateObj.attributes.id) ||
EDITABLE_DOMAINS.includes(domain))
? html` ? html`
<mwc-icon-button <mwc-icon-button
slot="actionItems" slot="actionItems"
@ -283,14 +301,12 @@ export class MoreInfoDialog extends LitElement {
private _gotoEdit() { private _gotoEdit() {
const stateObj = this.hass.states[this._entityId!]; const stateObj = this.hass.states[this._entityId!];
const domain = computeDomain(this._entityId!); const domain = computeDomain(this._entityId!);
navigate( let idToPassThroughUrl = stateObj.entity_id;
this, if (EDITABLE_DOMAINS_WITH_ID.includes(domain) || domain === "person") {
`/config/${domain}/edit/${ idToPassThroughUrl = stateObj.attributes.id;
EDITABLE_DOMAINS_WITH_ID.includes(domain) }
? stateObj.attributes.id
: stateObj.entity_id navigate(this, `/config/${domain}/edit/${idToPassThroughUrl}`);
}`
);
this.closeDialog(); this.closeDialog();
} }

View File

@ -23,7 +23,10 @@ import {
updatePerson, updatePerson,
} from "../../../data/person"; } from "../../../data/person";
import { fetchUsers, User } from "../../../data/user"; import { fetchUsers, User } from "../../../data/user";
import { showConfirmationDialog } from "../../../dialogs/generic/show-dialog-box"; import {
showConfirmationDialog,
showAlertDialog,
} from "../../../dialogs/generic/show-dialog-box";
import "../../../layouts/hass-loading-screen"; import "../../../layouts/hass-loading-screen";
import "../../../layouts/hass-tabs-subpage"; import "../../../layouts/hass-tabs-subpage";
import { HomeAssistant, Route } from "../../../types"; import { HomeAssistant, Route } from "../../../types";
@ -158,6 +161,31 @@ class HaConfigPerson extends LitElement {
this._configItems = personData.config.sort((ent1, ent2) => this._configItems = personData.config.sort((ent1, ent2) =>
compare(ent1.name, ent2.name) compare(ent1.name, ent2.name)
); );
this._openDialogIfPersonSpecifiedInRoute();
}
private _openDialogIfPersonSpecifiedInRoute() {
if (!this.route.path.includes("/edit/")) {
return;
}
const routeSegments = this.route.path.split("/edit/");
const personId = routeSegments.length > 1 ? routeSegments[1] : null;
if (!personId) {
return;
}
const personToEdit = this._storageItems!.find((p) => p.id === personId);
if (personToEdit) {
this._openDialog(personToEdit);
} else {
showAlertDialog(this, {
title: this.hass?.localize(
"ui.panel.config.person.person_not_found_title"
),
text: this.hass?.localize("ui.panel.config.person.person_not_found"),
});
}
} }
private _createPerson() { private _createPerson() {

View File

@ -1697,6 +1697,8 @@
"add_person": "Add Person", "add_person": "Add Person",
"confirm_delete": "Are you sure you want to delete this person?", "confirm_delete": "Are you sure you want to delete this person?",
"confirm_delete2": "All devices belonging to this person will become unassigned.", "confirm_delete2": "All devices belonging to this person will become unassigned.",
"person_not_found_title": "Person Not Found",
"person_not_found": "We couldn't find the person you were trying to edit.",
"detail": { "detail": {
"new_person": "New Person", "new_person": "New Person",
"name": "Name", "name": "Name",