mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-30 04:36:36 +00:00
Use ha-form for onboarding-create-user (#10604)
This commit is contained in:
parent
a6b98fc3c3
commit
b969db0c0f
@ -1,5 +1,4 @@
|
|||||||
import "@material/mwc-button";
|
import "@material/mwc-button";
|
||||||
import "@polymer/paper-input/paper-input";
|
|
||||||
import { genClientId } from "home-assistant-js-websocket";
|
import { genClientId } from "home-assistant-js-websocket";
|
||||||
import {
|
import {
|
||||||
css,
|
css,
|
||||||
@ -9,168 +8,116 @@ import {
|
|||||||
PropertyValues,
|
PropertyValues,
|
||||||
TemplateResult,
|
TemplateResult,
|
||||||
} from "lit";
|
} from "lit";
|
||||||
import { customElement, property, state } from "lit/decorators";
|
import { customElement, property, query, state } from "lit/decorators";
|
||||||
import { fireEvent } from "../common/dom/fire_event";
|
import { fireEvent } from "../common/dom/fire_event";
|
||||||
import { LocalizeFunc } from "../common/translations/localize";
|
import { LocalizeFunc } from "../common/translations/localize";
|
||||||
|
import "../components/ha-form/ha-form";
|
||||||
|
import type { HaForm } from "../components/ha-form/ha-form";
|
||||||
|
import { HaFormDataContainer, HaFormSchema } from "../components/ha-form/types";
|
||||||
import { onboardUserStep } from "../data/onboarding";
|
import { onboardUserStep } from "../data/onboarding";
|
||||||
import { PolymerChangedEvent } from "../polymer-types";
|
import { PolymerChangedEvent } from "../polymer-types";
|
||||||
|
|
||||||
|
const CREATE_USER_SCHEMA: HaFormSchema[] = [
|
||||||
|
{ type: "string", name: "name", required: true },
|
||||||
|
{ type: "string", name: "username", required: true },
|
||||||
|
{ type: "string", name: "password", required: true },
|
||||||
|
{ type: "string", name: "password_confirm", required: true },
|
||||||
|
];
|
||||||
|
|
||||||
@customElement("onboarding-create-user")
|
@customElement("onboarding-create-user")
|
||||||
class OnboardingCreateUser extends LitElement {
|
class OnboardingCreateUser extends LitElement {
|
||||||
@property() public localize!: LocalizeFunc;
|
@property() public localize!: LocalizeFunc;
|
||||||
|
|
||||||
@property() public language!: string;
|
@property() public language!: string;
|
||||||
|
|
||||||
@state() private _name = "";
|
|
||||||
|
|
||||||
@state() private _username = "";
|
|
||||||
|
|
||||||
@state() private _password = "";
|
|
||||||
|
|
||||||
@state() private _passwordConfirm = "";
|
|
||||||
|
|
||||||
@state() private _loading = false;
|
@state() private _loading = false;
|
||||||
|
|
||||||
@state() private _errorMsg?: string = undefined;
|
@state() private _errorMsg?: string;
|
||||||
|
|
||||||
|
@state() private _formError: Record<string, string> = {};
|
||||||
|
|
||||||
|
@state() private _newUser: HaFormDataContainer = {};
|
||||||
|
|
||||||
|
@query("ha-form", true) private _form?: HaForm;
|
||||||
|
|
||||||
protected render(): TemplateResult {
|
protected render(): TemplateResult {
|
||||||
return html`
|
return html`
|
||||||
<p>
|
<p>${this.localize("ui.panel.page-onboarding.intro")}</p>
|
||||||
${this.localize("ui.panel.page-onboarding.intro")}
|
<p>${this.localize("ui.panel.page-onboarding.user.intro")}</p>
|
||||||
</p>
|
|
||||||
|
|
||||||
<p>
|
${this._errorMsg
|
||||||
${this.localize("ui.panel.page-onboarding.user.intro")}
|
? html`<ha-alert alert-type="error">${this._errorMsg}</ha-alert>`
|
||||||
</p>
|
: ""}
|
||||||
|
|
||||||
${
|
<ha-form
|
||||||
this._errorMsg
|
.computeLabel=${this._computeLabel(this.localize)}
|
||||||
? html`
|
.data=${this._newUser}
|
||||||
<p class="error">
|
.disabled=${this._loading}
|
||||||
${this.localize(
|
.error=${this._formError}
|
||||||
`ui.panel.page-onboarding.user.error.${this._errorMsg}`
|
.schema=${CREATE_USER_SCHEMA}
|
||||||
) || this._errorMsg}
|
|
||||||
</p>
|
|
||||||
`
|
|
||||||
: ""
|
|
||||||
}
|
|
||||||
|
|
||||||
<form>
|
|
||||||
<paper-input
|
|
||||||
name="name"
|
|
||||||
label=${this.localize("ui.panel.page-onboarding.user.data.name")}
|
|
||||||
.value=${this._name}
|
|
||||||
@value-changed=${this._handleValueChanged}
|
@value-changed=${this._handleValueChanged}
|
||||||
required
|
></ha-form>
|
||||||
auto-validate
|
|
||||||
autocapitalize='on'
|
|
||||||
.errorMessage=${this.localize(
|
|
||||||
"ui.panel.page-onboarding.user.required_field"
|
|
||||||
)}
|
|
||||||
@blur=${this._maybePopulateUsername}
|
|
||||||
></paper-input>
|
|
||||||
|
|
||||||
<paper-input
|
<mwc-button
|
||||||
name="username"
|
raised
|
||||||
label=${this.localize("ui.panel.page-onboarding.user.data.username")}
|
@click=${this._submitForm}
|
||||||
value=${this._username}
|
.disabled=${this._loading ||
|
||||||
@value-changed=${this._handleValueChanged}
|
!this._newUser.name ||
|
||||||
required
|
!this._newUser.username ||
|
||||||
auto-validate
|
!this._newUser.password}
|
||||||
autocapitalize='none'
|
>
|
||||||
.errorMessage=${this.localize(
|
${this.localize("ui.panel.page-onboarding.user.create_account")}
|
||||||
"ui.panel.page-onboarding.user.required_field"
|
</mwc-button>
|
||||||
)}
|
`;
|
||||||
></paper-input>
|
|
||||||
|
|
||||||
<paper-input
|
|
||||||
name="password"
|
|
||||||
label=${this.localize("ui.panel.page-onboarding.user.data.password")}
|
|
||||||
value=${this._password}
|
|
||||||
@value-changed=${this._handleValueChanged}
|
|
||||||
required
|
|
||||||
type='password'
|
|
||||||
auto-validate
|
|
||||||
.errorMessage=${this.localize(
|
|
||||||
"ui.panel.page-onboarding.user.required_field"
|
|
||||||
)}
|
|
||||||
></paper-input>
|
|
||||||
|
|
||||||
<paper-input
|
|
||||||
name="passwordConfirm"
|
|
||||||
label=${this.localize(
|
|
||||||
"ui.panel.page-onboarding.user.data.password_confirm"
|
|
||||||
)}
|
|
||||||
value=${this._passwordConfirm}
|
|
||||||
@value-changed=${this._handleValueChanged}
|
|
||||||
required
|
|
||||||
type='password'
|
|
||||||
.invalid=${
|
|
||||||
this._password !== "" &&
|
|
||||||
this._passwordConfirm !== "" &&
|
|
||||||
this._passwordConfirm !== this._password
|
|
||||||
}
|
|
||||||
.errorMessage=${this.localize(
|
|
||||||
"ui.panel.page-onboarding.user.error.password_not_match"
|
|
||||||
)}
|
|
||||||
></paper-input>
|
|
||||||
|
|
||||||
<p class="action">
|
|
||||||
<mwc-button
|
|
||||||
raised
|
|
||||||
@click=${this._submitForm}
|
|
||||||
.disabled=${this._loading}
|
|
||||||
>
|
|
||||||
${this.localize("ui.panel.page-onboarding.user.create_account")}
|
|
||||||
</mwc-button>
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
</form>
|
|
||||||
`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected firstUpdated(changedProps: PropertyValues) {
|
protected firstUpdated(changedProps: PropertyValues) {
|
||||||
super.firstUpdated(changedProps);
|
super.firstUpdated(changedProps);
|
||||||
setTimeout(
|
setTimeout(() => this._form?.focus(), 100);
|
||||||
() => this.shadowRoot!.querySelector("paper-input")!.focus(),
|
|
||||||
100
|
|
||||||
);
|
|
||||||
this.addEventListener("keypress", (ev) => {
|
this.addEventListener("keypress", (ev) => {
|
||||||
if (ev.keyCode === 13) {
|
if (
|
||||||
|
ev.keyCode === 13 &&
|
||||||
|
this._newUser.name &&
|
||||||
|
this._newUser.username &&
|
||||||
|
this._newUser.password &&
|
||||||
|
this._newUser.password_confirm
|
||||||
|
) {
|
||||||
this._submitForm(ev);
|
this._submitForm(ev);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private _handleValueChanged(ev: PolymerChangedEvent<string>): void {
|
private _computeLabel(localize) {
|
||||||
const name = (ev.target as any).name;
|
return (schema: HaFormSchema) =>
|
||||||
this[`_${name}`] = ev.detail.value;
|
localize(`ui.panel.page-onboarding.user.data.${schema.name}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
private _handleValueChanged(
|
||||||
|
ev: PolymerChangedEvent<HaFormDataContainer>
|
||||||
|
): void {
|
||||||
|
this._newUser = ev.detail.value;
|
||||||
|
this._maybePopulateUsername();
|
||||||
|
this._formError.password_confirm =
|
||||||
|
this._newUser.password !== this._newUser.password_confirm
|
||||||
|
? this.localize(
|
||||||
|
"ui.panel.page-onboarding.user.error.password_not_match"
|
||||||
|
)
|
||||||
|
: "";
|
||||||
}
|
}
|
||||||
|
|
||||||
private _maybePopulateUsername(): void {
|
private _maybePopulateUsername(): void {
|
||||||
if (this._username) {
|
if (!this._newUser.name || this._newUser.name === this._newUser.username) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const parts = this._name.split(" ");
|
const parts = String(this._newUser.name).split(" ");
|
||||||
|
|
||||||
if (parts.length) {
|
if (parts.length) {
|
||||||
this._username = parts[0].toLowerCase();
|
this._newUser.username = parts[0].toLowerCase();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async _submitForm(ev): Promise<void> {
|
private async _submitForm(ev): Promise<void> {
|
||||||
ev.preventDefault();
|
ev.preventDefault();
|
||||||
if (!this._name || !this._username || !this._password) {
|
|
||||||
this._errorMsg = "required_fields";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (this._password !== this._passwordConfirm) {
|
|
||||||
this._errorMsg = "password_not_match";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
this._loading = true;
|
this._loading = true;
|
||||||
this._errorMsg = "";
|
this._errorMsg = "";
|
||||||
|
|
||||||
@ -179,9 +126,9 @@ class OnboardingCreateUser extends LitElement {
|
|||||||
|
|
||||||
const result = await onboardUserStep({
|
const result = await onboardUserStep({
|
||||||
client_id: clientId,
|
client_id: clientId,
|
||||||
name: this._name,
|
name: String(this._newUser.name),
|
||||||
username: this._username,
|
username: String(this._newUser.username),
|
||||||
password: this._password,
|
password: String(this._newUser.password),
|
||||||
language: this.language,
|
language: this.language,
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -199,13 +146,11 @@ class OnboardingCreateUser extends LitElement {
|
|||||||
|
|
||||||
static get styles(): CSSResultGroup {
|
static get styles(): CSSResultGroup {
|
||||||
return css`
|
return css`
|
||||||
.error {
|
mwc-button {
|
||||||
color: red;
|
margin: 32px 0 0;
|
||||||
}
|
|
||||||
|
|
||||||
.action {
|
|
||||||
margin: 32px 0 16px;
|
|
||||||
text-align: center;
|
text-align: center;
|
||||||
|
display: block;
|
||||||
|
text-align: right;
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
@ -3924,7 +3924,6 @@
|
|||||||
},
|
},
|
||||||
"create_account": "Create Account",
|
"create_account": "Create Account",
|
||||||
"error": {
|
"error": {
|
||||||
"required_fields": "Fill in all required fields",
|
|
||||||
"password_not_match": "Passwords don't match"
|
"password_not_match": "Passwords don't match"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user