Skip to content

Commit d9ed536

Browse files
committed
frontend/user: add name validation
1 parent 08e2b42 commit d9ed536

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

frontend/src/locales/en.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export const en = {
3333
"debug_mode": "Debug mode",
3434
"invalid_key": "The keys saved on the device seem to be corrupted",
3535
"connection_timeout": "Timeout",
36+
"invalid_name": "The name must not be empty",
3637
"connection_timeout_text": "A timeout occured while establishing the connection. Please try again later or contact us in case the problem persists."
3738
},
3839
"recovery": {
@@ -112,7 +113,7 @@ export const en = {
112113
"close": "Close",
113114
"password": "Password",
114115
"login": "Login",
115-
"wrong_credentials": "Email-adresse or password wrong.",
116+
"wrong_credentials": "Email-address or password wrong.",
116117
"success_alert_text": "You should receive an email in the next couple of minutes.",
117118
"success_alert_heading": "Success",
118119
"error_alert_text": "Failed to start recovery with status {{- status}}: {{text}}",

frontend/src/pages/user.tsx

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ interface UserState {
4545
interface State {
4646
isDirty: boolean
4747
user: UserState
48+
validated: boolean
49+
nameIsValid: boolean
4850
}
4951

5052
let email = "";
@@ -63,6 +65,8 @@ class UserComponent extends Component<{}, State> {
6365
this.state = {
6466
isDirty: false,
6567
user: state,
68+
validated: false,
69+
nameIsValid: true,
6670
}
6771

6872
fetchClient.GET("/user/me", {credentials: "same-origin"}).then(({data, error, response}) => {
@@ -75,8 +79,24 @@ class UserComponent extends Component<{}, State> {
7579
});
7680
}
7781

82+
validateForm = () => {
83+
const nameIsValid = this.state.user.name.trim().length > 0;
84+
85+
this.setState({
86+
nameIsValid,
87+
validated: true
88+
});
89+
90+
return nameIsValid;
91+
}
92+
7893
submit = async (e: SubmitEvent) => {
7994
e.preventDefault();
95+
96+
if (!this.validateForm()) {
97+
return;
98+
}
99+
80100
const {response, error} = await fetchClient.PUT("/user/update_user", {body: this.state.user, credentials: "same-origin"});
81101
const status = response.status;
82102
if (status === 200) {
@@ -89,14 +109,14 @@ class UserComponent extends Component<{}, State> {
89109
render() {
90110
const {t} = useTranslation("", {useSuspense: false, keyPrefix: "user"});
91111
return (<>
92-
<Form onSubmit={this.submit}>
112+
<Form onSubmit={this.submit} noValidate validated={this.state.validated}>
93113
<Form.Group className="pb-3" controlId="userId">
94114
<Form.Label className="text-muted">{t("user_id")}</Form.Label>
95115
<Form.Control type="text" disabled value={this.state.user.id} className="bg-light" />
96116
</Form.Group>
97117
<Form.Group className="pb-3" controlId="userEmail">
98118
<Form.Label className="text-muted">{t("email")}</Form.Label>
99-
<Form.Control type="email" value={this.state.user.email} onChange={(e) => {
119+
<Form.Control required type="email" value={this.state.user.email} onChange={(e) => {
100120
this.setState({user: {...this.state.user, email: (e.target as HTMLInputElement).value}, isDirty: true});
101121
}} disabled={this.state.user.has_old_charger} />
102122
{this.state.user.has_old_charger &&
@@ -107,9 +127,12 @@ class UserComponent extends Component<{}, State> {
107127
</Form.Group>
108128
<Form.Group className="pb-3" controlId="userName">
109129
<Form.Label className="text-muted">{t("name")}</Form.Label>
110-
<Form.Control type="text" value={this.state.user.name} onChange={(e) => {
130+
<Form.Control required type="text" value={this.state.user.name} onChange={(e) => {
111131
this.setState({user: {...this.state.user, name: (e.target as HTMLInputElement).value}, isDirty: true});
112-
}} />
132+
}} isInvalid={!this.state.nameIsValid} />
133+
<Form.Control.Feedback type="invalid">
134+
{t("invalid_name")}
135+
</Form.Control.Feedback>
113136
</Form.Group>
114137
<Button type="submit" variant="primary" disabled={!this.state.isDirty}>
115138
{t("save_changes")}

0 commit comments

Comments
 (0)