Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

UI updates: Manager user page form validation improvements

Rich Turner3a83f69378d4
diff --git a/manager/src/web/shared/locales/en/or.json b/manager/src/web/shared/locales/en/or.json
index ab238ada0..956ed4cb7 100644
--- a/manager/src/web/shared/locales/en/or.json
+++ b/manager/src/web/shared/locales/en/or.json
@@ -459,5 +459,9 @@
"title": {
"indexedItem": "Item {{index}}"
}
- }
+ },
+ "createUserFailed": "Create user failed",
+ "saveUserFailed": "Save user failed",
+ "invalidUsername": "Username must be 3-255 alpha numerical characters",
+ "invalidEmail": "Invalid email address"
}
diff --git a/ui/app/manager/src/pages/page-users.ts b/ui/app/manager/src/pages/page-users.ts
index 39f3f5348..c1a387fee 100644
--- a/ui/app/manager/src/pages/page-users.ts
+++ b/ui/app/manager/src/pages/page-users.ts
@@ -11,7 +11,7 @@ import {OrIcon} from "@openremote/or-icon";
import {InputType, OrInputChangedEvent, OrMwcInput} from "@openremote/or-mwc-components/or-mwc-input";
import {OrMwcDialog, showDialog, showOkCancelDialog} from "@openremote/or-mwc-components/or-mwc-dialog";
import {showSnackbar} from "@openremote/or-mwc-components/or-mwc-snackbar";
-import {GenericAxiosResponse} from "@openremote/rest";
+import {AxiosError, isAxiosError, GenericAxiosResponse} from "@openremote/rest";
import {OrAssetTreeSelectionEvent} from "@openremote/or-asset-tree";
const tableStyle = require("@material/data-table/dist/mdc.data-table.css");
@@ -315,13 +315,20 @@ class PageUsers<S extends AppStateKeyed> extends Page<S> {
private async _createUpdateUser(user: UserModel) {
+ if (!!user.username) {
+ return;
+ }
+
if (user.password === "") {
// Means a validation failure shouldn't get here
return;
}
+ const isUpdate = !!user.id;
+
try {
const response = await manager.rest.api.UserResource.createUpdate(manager.displayRealm, user);
+
// Ensure user ID is set
user.id = response.data.id;
@@ -333,6 +340,14 @@ class PageUsers<S extends AppStateKeyed> extends Page<S> {
await this._updateRoles(user, false);
await this._updateRoles(user, true);
await this._updateUserAssetLinks(user);
+ } catch (e) {
+ if (isAxiosError(e)) {
+ console.error((isUpdate ? "save user failed" : "create user failed") + ": response = " + e.response.statusText);
+
+ if (e.response.status === 400) {
+ showSnackbar(undefined, i18next.t(isUpdate ? "saveUserFailed" : "createUserFailed"), i18next.t("dismiss"));
+ }
+ }
} finally {
this.loadUsers();
}
@@ -630,21 +645,28 @@ class PageUsers<S extends AppStateKeyed> extends Page<S> {
}));
}
- protected _onPasswordChanged(user: UserModel, suffix: string) {
+ protected onUserChanged(e: OrInputChangedEvent, suffix: string) {
+ // Don't have form-associated custom element support in lit at time of writing which would be the way to go here
+ const formElement = (e.target as HTMLElement).parentElement;
+ const saveBtn = this.shadowRoot.getElementById("savebtn-" + suffix) as OrMwcInput;
+ if (formElement) {
+ const saveDisabled = Array.from(formElement.children).filter(e => e instanceof OrMwcInput).some(input => !(input as OrMwcInput).valid);
+ saveBtn.disabled = saveDisabled;
+ }
+ }
+
+ protected _onPasswordChanged(user: UserModel, suffix: string) {
const passwordComponent = this.shadowRoot.getElementById("password-" + suffix) as OrMwcInput;
const repeatPasswordComponent = this.shadowRoot.getElementById("repeatPassword-" + suffix) as OrMwcInput;
- const saveBtn = this.shadowRoot.getElementById("savebtn-" + suffix) as OrMwcInput;
if (repeatPasswordComponent.value !== passwordComponent.value) {
const error = i18next.t("passwordMismatch");
repeatPasswordComponent.setCustomValidity(error);
- saveBtn.disabled = true;
user.password = "";
} else {
repeatPasswordComponent.setCustomValidity(undefined);
user.password = passwordComponent.value;
- saveBtn.disabled = false;
}
}
@@ -710,27 +732,28 @@ class PageUsers<S extends AppStateKeyed> extends Page<S> {
<!-- user details -->
<or-mwc-input ?readonly="${!!user.id || readonly}"
.label="${i18next.t("username")}"
- .type="${InputType.TEXT}" min="1" required
- .value="${user.username}"
- @or-mwc-input-changed="${(e: OrInputChangedEvent) => user.username = e.detail.value}"></or-mwc-input>
+ .type="${InputType.TEXT}" minLength="3" maxLength="255" required
+ pattern="[a-zA-Z0-9-_]+"
+ .value="${user.username}" .validationMessage="${i18next.t("invalidUsername")}"
+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => {user.username = e.detail.value; this.onUserChanged(e, suffix)}}"></or-mwc-input>
<or-mwc-input ?readonly="${readonly}"
class="${isServiceUser ? "hidden" : ""}"
.label="${i18next.t("email")}"
- .type="${InputType.EMAIL}" min="1"
- .value="${user.email}"
- @or-mwc-input-changed="${(e: OrInputChangedEvent) => user.email = e.detail.value}"></or-mwc-input>
+ .type="${InputType.EMAIL}"
+ .value="${user.email}" .validationMessage="${i18next.t("invalidEmail")}"
+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => {user.email = e.detail.value; this.onUserChanged(e, suffix)}}"></or-mwc-input>
<or-mwc-input ?readonly="${readonly}"
class="${isServiceUser ? "hidden" : ""}"
.label="${i18next.t("firstName")}"
- .type="${InputType.TEXT}" min="1"
+ .type="${InputType.TEXT}" minLength="1"
.value="${user.firstName}"
- @or-mwc-input-changed="${(e: OrInputChangedEvent) => user.firstName = e.detail.value}"></or-mwc-input>
+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => {user.firstName = e.detail.value; this.onUserChanged(e, suffix)}}"></or-mwc-input>
<or-mwc-input ?readonly="${readonly}"
class="${isServiceUser ? "hidden" : ""}"
.label="${i18next.t("surname")}"
- .type="${InputType.TEXT}" min="1"
+ .type="${InputType.TEXT}" minLength="1"
.value="${user.lastName}"
- @or-mwc-input-changed="${(e: OrInputChangedEvent) => user.lastName = e.detail.value}"></or-mwc-input>
+ @or-mwc-input-changed="${(e: OrInputChangedEvent) => {user.lastName = e.detail.value; this.onUserChanged(e, suffix)}}"></or-mwc-input>
<!-- password -->
<h5>${i18next.t("password")}</h5>
@@ -749,14 +772,16 @@ class PageUsers<S extends AppStateKeyed> extends Page<S> {
.label="${i18next.t("password")}"
.type="${InputType.PASSWORD}" min="1"
@or-mwc-input-changed="${(e: OrInputChangedEvent) => {
- this._onPasswordChanged(user, suffix)
+ this._onPasswordChanged(user, suffix);
+ this.onUserChanged(e, suffix);
}}"></or-mwc-input>
<or-mwc-input id="repeatPassword-${suffix}"
helperPersistent ?readonly="${readonly}"
.label="${i18next.t("repeatPassword")}"
.type="${InputType.PASSWORD}" min="1"
@or-mwc-input-changed="${(e: OrInputChangedEvent) => {
- this._onPasswordChanged(user, suffix)
+ this._onPasswordChanged(user, suffix);
+ this.onUserChanged(e, suffix);
}}"></or-mwc-input>
`}
</div>
diff --git a/ui/component/or-mwc-components/src/or-mwc-input.ts b/ui/component/or-mwc-components/src/or-mwc-input.ts
index 79f97245f..daf54f7f7 100644
--- a/ui/component/or-mwc-components/src/or-mwc-input.ts
+++ b/ui/component/or-mwc-components/src/or-mwc-input.ts
@@ -513,7 +513,7 @@ const style = css`
::-webkit-clear-button {display: none;}
::-webkit-inner-spin-button { display: none; }
- ::-webkit-datetime-edit { padding: 0em;}
+ ::-webkit-datetime-edit { padding: 0em; }
::-webkit-datetime-edit-text { padding: 0; }
.mdc-text-field--focused:not(.mdc-text-field--disabled) .mdc-floating-label {
@@ -714,7 +714,7 @@ export class OrMwcInput extends LitElement {
@property({type: Boolean})
public helperPersistent: boolean = false;
- @property({type: String})
+ @property({type: String, attribute: true})
public validationMessage?: string;
@property({type: Boolean})
@@ -743,6 +743,8 @@ export class OrMwcInput extends LitElement {
protected _tempValue: any;
@state()
protected isUiValid = true;
+ @state()
+ protected errorMessage?: string;
disconnectedCallback(): void {
super.disconnectedCallback();
@@ -800,6 +802,7 @@ export class OrMwcInput extends LitElement {
}
}
+
protected render() {
if (this.type) {
@@ -807,9 +810,9 @@ export class OrMwcInput extends LitElement {
const showLabel = !this.fullWidth && this.label;
let outlined = !this.fullWidth && this.outlined;
let hasHelper = !!this.helperText;
- const showValidationMessage = !this.isUiValid && !!this.validationMessage;
+ const showValidationMessage = !this.isUiValid && (!!this.errorMessage || !!this.validationMessage);
const helperClasses = {
- "mdc-text-field-helper-text--persistent": this.helperPersistent,
+ "mdc-text-field-helper-text--persistent": !showValidationMessage && this.helperPersistent,
"mdc-text-field-helper-text--validation-msg": showValidationMessage,
};
const hasValue = this.value || this.value === false;
@@ -1010,7 +1013,7 @@ export class OrMwcInput extends LitElement {
</div>
${hasHelper || showValidationMessage ? html`
<p id="component-helper-text" class="mdc-select-helper-text ${classMap(helperClasses)}" aria-hidden="true">
- ${showValidationMessage ? this.validationMessage : this.helperText}
+ ${showValidationMessage ? this.errorMessage || this.validationMessage : this.helperText}
</p>` : ``}
</div>
`;
@@ -1239,7 +1242,7 @@ export class OrMwcInput extends LitElement {
</label>
${hasHelper || showValidationMessage ? html`
<div class="mdc-text-field-helper-line">
- <div class="mdc-text-field-helper-text ${classMap(helperClasses)}">${showValidationMessage ? this.validationMessage : this.helperText}</div>
+ <div class="mdc-text-field-helper-text ${classMap(helperClasses)}">${showValidationMessage ? this.errorMessage || this.validationMessage : this.helperText}</div>
${this.charCounter && !this.readonly ? html`<div class="mdc-text-field-character-counter"></div>` : ``}
</div>
` : ``}
@@ -1451,7 +1454,9 @@ export class OrMwcInput extends LitElement {
checkbox.disabled = this.disabled || this.readonly;
}
- (this._mdcComponent as any).required = !!this.required;
+ if (this._mdcComponent) {
+ (this._mdcComponent as any).required = this.required;
+ }
}
if (this.autoValidate) {
@@ -1474,7 +1479,7 @@ export class OrMwcInput extends LitElement {
}
public setCustomValidity(msg: string | undefined) {
- this.validationMessage = msg;
+ this.errorMessage = msg;
const elem = this.shadowRoot!.getElementById("elem") as HTMLElement;
if (elem && (elem as any).setCustomValidity) {
(elem as any).setCustomValidity(msg ?? "");
diff --git a/ui/component/rest/src/index.ts b/ui/component/rest/src/index.ts
index 0b93d7c11..2d3c926a6 100644
--- a/ui/component/rest/src/index.ts
+++ b/ui/component/rest/src/index.ts
@@ -1,8 +1,10 @@
-import axios, {AxiosInstance, AxiosRequestConfig, GenericAxiosResponse} from "axios";
+import axios, {AxiosInstance, AxiosRequestConfig, GenericAxiosResponse, AxiosError} from "axios";
import {ApiClient, RestResponse} from "./restclient";
import Qs from "qs";
-export {RestResponse, GenericAxiosResponse};
+const isAxiosError = axios.isAxiosError;
+
+export {RestResponse, GenericAxiosResponse, AxiosError, isAxiosError};
export class RestApi {
diff --git a/ui/tsconfig.json b/ui/tsconfig.json
index f33e0f911..78206272d 100644
--- a/ui/tsconfig.json
+++ b/ui/tsconfig.json
@@ -15,6 +15,7 @@
"sourceMap": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
- "composite": true
+ "composite": true,
+ "useDefineForClassFields": false
}
}