diff --git a/src/components/ha-newsletter.ts b/src/components/ha-newsletter.ts new file mode 100644 index 0000000000..ab28a527f3 --- /dev/null +++ b/src/components/ha-newsletter.ts @@ -0,0 +1,102 @@ +import "./ha-circular-progress"; +import "@material/mwc-button/mwc-button"; +import "./ha-card"; +import "./ha-textfield"; +import { LitElement, TemplateResult, html, CSSResultGroup, css } from "lit"; +import { customElement, property, query } from "lit/decorators"; +import type { HaTextField } from "./ha-textfield"; +import { HomeAssistant } from "../types"; + +@customElement("ha-newsletter") +class HaNewsletter extends LitElement { + @property({ attribute: false }) hass!: HomeAssistant; + + @query("ha-textfield") + private _emailField?: HaTextField; + + private _requestStatus?: "inprogress" | "complete"; + + protected render(): TemplateResult { + return html` + +
+ ${this._requestStatus === "complete" + ? html`${this.hass.localize("ui.newsletter.thanks")}` + : html` + + ${this._requestStatus === "inprogress" + ? html` + + ` + : html` + + `} + `} +
+
+ `; + } + + private async _subscribe() { + if (!this._emailField?.reportValidity()) { + this._emailField!.focus(); + return; + } + + this._requestStatus = "inprogress"; + + await fetch( + `https://newsletter.home-assistant.io/subscribe?email=${ + this._emailField!.value + }` + ) + .then(() => { + this._requestStatus = "complete"; + }) + .catch((err) => { + this._requestStatus = undefined; + // eslint-disable-next-line no-console + console.error(err); + }); + } + + static get styles(): CSSResultGroup { + return css` + .newsletter { + display: flex; + flex-direction: row; + padding: 0 16px 16px; + } + + ha-textfield { + flex: 1; + display: block; + padding-right: 8px; + } + + mwc-button { + padding-top: 12px; + } + `; + } +} + +declare global { + interface HTMLElementTagNameMap { + "ha-newsletter": HaNewsletter; + } +} diff --git a/src/panels/config/dashboard/ha-config-dashboard.ts b/src/panels/config/dashboard/ha-config-dashboard.ts index 552692e1e3..a776efa613 100644 --- a/src/panels/config/dashboard/ha-config-dashboard.ts +++ b/src/panels/config/dashboard/ha-config-dashboard.ts @@ -1,3 +1,4 @@ +import "../../../components/ha-newsletter"; import { mdiCloudLock, mdiDotsVertical, mdiMagnify } from "@mdi/js"; import "@material/mwc-list/mwc-list-item"; import type { ActionDetail } from "@material/mwc-list"; @@ -134,6 +135,7 @@ class HaConfigDashboard extends LitElement { .pages=${configSections.dashboard} > `} + `; diff --git a/src/translations/en.json b/src/translations/en.json index 36998964a5..f37cacaef6 100755 --- a/src/translations/en.json +++ b/src/translations/en.json @@ -972,6 +972,13 @@ "triggered": "Triggered {name}", "dismiss": "Dismiss" }, + "newsletter": { + "newsletter": "Newsletter", + "email": "Email", + "validation": "A valid email address is required", + "subscribe": "Subscribe", + "thanks": "Thanks for subscribing!" + }, "sidebar": { "external_app_configuration": "App Configuration", "sidebar_toggle": "Sidebar Toggle",