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

Added migrate dialog when card has no ID #2008

Merged
merged 19 commits into from
Nov 19, 2018
Merged

Added migrate dialog when card has no ID #2008

merged 19 commits into from
Nov 19, 2018

Conversation

bramkragten
Copy link
Member

@bramkragten bramkragten commented Nov 7, 2018

image

Fixes: #1954

@ghost ghost added the in progress label Nov 7, 2018
@@ -0,0 +1,83 @@
import { html, LitElement, PropertyDeclarations } from "@polymer/lit-element";
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we look into adding a Lovelace/editor Localize for stuff like this? Usually not full sentences in the translation file but this is a pretty descriptive and useful message that english may not be the best hardcoded route? Just a thought

Copy link
Member Author

Choose a reason for hiding this comment

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

Added it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

@zsarnett
Copy link
Contributor

zsarnett commented Nov 7, 2018

Fixes: #1954

@balloob
Copy link
Member

balloob commented Nov 7, 2018

I think that this should not be it's own dialog, instead I suggest it becomes part of the dialog-edit-card. For this, we should split the dialog-edit-card to get the body rendering part of it moved to it's own component.

Inside dialog-edit-card:

  • If Config has no ID: show migrate part. When user clicks migrate, continue to next step.
  • Show spinner and load YAML from backend
  • Render the edit card stuff

@balloob
Copy link
Member

balloob commented Nov 7, 2018

Thinking now, we only really need a "load yaml from backend" for the yaml editor, other editors can just use the JS config.

@@ -12,10 +13,17 @@ export const getCardConfig = (
export const updateCardConfig = (
hass: HomeAssistant,
cardId: string,
config: any
config: string | LovelaceConfig,
configFormat: "js" | "yaml"
Copy link
Member

Choose a reason for hiding this comment

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

json. It's either json or yaml.

@@ -147,7 +147,8 @@ class HUIView extends PolymerElement {

const wrapper = document.createElement("hui-card-options");
wrapper.hass = this.hass;
wrapper.cardId = cardConfig.id;
wrapper.cardId = cardConfig.id ? String(cardConfig.id) : null;
Copy link
Member

Choose a reason for hiding this comment

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

No reason to set this, it's part of the cardConfig

@@ -28,10 +41,16 @@ export class HuiYAMLEditor extends LitElement {
`;
}

private async _loadConfig(): Promise<void> {
Copy link
Member

Choose a reason for hiding this comment

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

Yes! This was one of the confusing things. Perfect.

}
}

constructor() {
Copy link
Member

Choose a reason for hiding this comment

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

Just remove this?


set hass(value: HomeAssistant) {
this._hass = value;
if (!this.yaml || this.yaml === "") {
Copy link
Member

Choose a reason for hiding this comment

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

Don't do it here, check in updated() if cardId has changed or not. If it has, fetch yaml.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you mean shouldUpdate?

Copy link
Member

Choose a reason for hiding this comment

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

No. Only the lifecycle event updated is to be used for side effects. Side effects are things that mutate state, or start things that will mutate state (like fetching data)

import { hassLocalizeLitMixin } from "../../../mixins/lit-localize-mixin";
import { migrateConfig } from "../common/data";

export class HuiDialogMigrate extends hassLocalizeLitMixin(LitElement) {
Copy link
Member

Choose a reason for hiding this comment

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

Not being used?

};
}

public async showDialog({ hass, reloadLovelace }) {
Copy link
Member

Choose a reason for hiding this comment

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

We should change how we do this, and instead store the params as 1 object instead of many props.

public async showDialog(dialogParams: MigrateCardDialogParams) {
  this._dialogParams = dialogParams

@balloob
Copy link
Member

balloob commented Nov 11, 2018

I merged Zacs Pr, got merge conflicts now.

return {
hass: {},
};
return { hass: {} };
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure why this was changed. I dont think this is "prettier"

Copy link
Member

Choose a reason for hiding this comment

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

It is, small objects will be put on a single line.

this._createCard(configValue);
}

set value(configValue: LovelaceConfig) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We can make this set config right? To align with everything else as well as its param type

private _originalConfigYaml?: string;
private _configElement?: LovelaceCardEditor | null;
export class HuiDialogEditCard extends hassLocalizeLitMixin(LitElement) {
protected _hass?: HomeAssistant;
Copy link
Contributor

Choose a reason for hiding this comment

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

Why are we making this a _ variable when its not private?

? html`
<paper-button
@click="
${this._toggle}"
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesnt seem "prettier". As well as the reset of them down this section

}
</style>
<paper-dialog with-backdrop>
<h2>Card Configuration</h2>
<h2>
Copy link
Member

Choose a reason for hiding this comment

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

This is becoming quite messy because we're having 3 times a turnery statement on the exact same logic. To make it easier, just extract those into methods.

Preferably I would want to put it in a new custom element but that probably won't work well with the CSS of paper-dialog.

<paper-dialog with-backdrop>
  ${this._cardConfig!.id ? this._renderEditor() : this._renderMigrator()}
</paper-dialog>

@@ -936,6 +936,19 @@
"required_fields": "Fill in all required fields"
}
}
},
"lovelace": {
Copy link
Member

Choose a reason for hiding this comment

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

There already is a lovelace category, you should merge your changes into that one.

},
"lovelace": {
"editor": {
"header": "Card Configuration",
Copy link
Member

Choose a reason for hiding this comment

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

Feels weird that one dialog is "top level" of editor and one below it. I think both should be below it: edit.header and migrate.header

@ghost ghost assigned bramkragten Nov 19, 2018
}

private get _previewEl(): HuiYAMLCardPreview {
return this.shadowRoot!.querySelector("hui-yaml-card-preview")!;
public async showDialog({ hass, cardConfig, reloadLovelace }) {
Copy link
Member

Choose a reason for hiding this comment

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

For a future PR, we should just make this a single object, not explode it, and type it. That way we can export the type and make sure people call it correctly, even potentially have a helper.

public async showDialog(params: EditDialogParams) {

and then

export const showEditDialog = (element: HTMLElement, hass: HomeAssistant, cardConfig: CardConfig, reloadLoveLace: () => void) =>
  fireEvent(element, 'show-edit-card-dialog', { hass, cardConfig, reloadLovelace })

this._cardConfig = cardConfig;
this._reloadLovelace = reloadLovelace;
await this.updateComplete;
this._cardConfig!.id
Copy link
Member

Choose a reason for hiding this comment

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

this.shadowRoot.children[0].showDialog()

Copy link
Member Author

Choose a reason for hiding this comment

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

Can typescript handle that?

Copy link
Member

Choose a reason for hiding this comment

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

(this.shadowRoot.children[0] as any).showDialog()


public async openDialog(): Promise<void> {
// Wait till dialog is rendered.
await this.updateComplete;
Copy link
Member

Choose a reason for hiding this comment

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

We only have to do this if this._dialog == null ?


protected render(): TemplateResult {
return html`
<style>
Copy link
Member

Choose a reason for hiding this comment

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

I prefer having the style being included via renderStyle

?active="${this._loading}"
alt="Loading"
class="center"
style="margin-bottom: 24px;"
Copy link
Member

Choose a reason for hiding this comment

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

Use stylesheet

style="margin-bottom: 24px;"
></paper-spinner>
<paper-dialog-scrollable
class="${classMap({ hidden: this._loading! })}"
Copy link
Member

Choose a reason for hiding this comment

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

Should we hide this with CSS or just not render the whole element ?

when(
this._configElement,
() => this._configElement,
() => html`
Copy link
Member

Choose a reason for hiding this comment

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

I think that you can just use the string "loading…"

Copy link
Member

Choose a reason for hiding this comment

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

It's only important that the template returned from render is wrapped in html

<hui-card-preview .hass="${this._hass}"></hui-card-preview>
</paper-dialog-scrollable>
<div
class="paper-dialog-buttons ${classMap({ hidden: this._loading! })}"
Copy link
Member

Choose a reason for hiding this comment

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

class="${classMap(…)}" ? but maybe also not render it when we're loading ?

this._configElement.setConfig(this._configValue!.value as LovelaceConfig);
this._uiEditor = !this._uiEditor;
}
this._resizeDialog();
Copy link
Member

Choose a reason for hiding this comment

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

I think that we should only do this after await this.updateComplete ?

this._configValue!.format === "yaml"
? yaml.safeDump(this._configValue!.value)
: this._configValue!.value;
if (JSON.stringify(configValue) === JSON.stringify(this._originalConfig)) {
Copy link
Member

Choose a reason for hiding this comment

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

return JSON.stringify(configValue) !== JSON.stringify(this._originalConfig)

Copy link
Member

@balloob balloob left a comment

Choose a reason for hiding this comment

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

Looks good. A couple of minor comments but this PR is pretty much done.

@bramkragten bramkragten merged commit 0bb85bc into home-assistant:dev Nov 19, 2018
@ghost ghost removed the in progress label Nov 19, 2018
@github-actions github-actions bot locked and limited conversation to collaborators Jul 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Check and migrate incompatible LL configs
5 participants