Skip to content

Commit 7d859b6

Browse files
committed
cleanup
1 parent 3edfd60 commit 7d859b6

File tree

4 files changed

+22
-18
lines changed

4 files changed

+22
-18
lines changed

frontend/src/ts/modals/google-sign-up.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import AnimatedModal from "../utils/animated-modal";
1616
import { resetIgnoreAuthCallback } from "../firebase";
1717
import { validateWithIndicator } from "../elements/input-validation";
1818
import { UserNameSchema } from "@monkeytype/schemas/users";
19-
import { apeValidation } from "../utils/remote-validation";
19+
import { remoteValidation } from "../utils/remote-validation";
2020

2121
let signedInUser: UserCredential | undefined = undefined;
2222

@@ -155,9 +155,9 @@ function disableInput(): void {
155155

156156
validateWithIndicator(nameInputEl, {
157157
schema: UserNameSchema,
158-
isValid: apeValidation(
158+
isValid: remoteValidation(
159159
async (name) => Ape.users.getNameAvailability({ params: { name } }),
160-
{ errorMessage: "Name not available" }
160+
{ on5xx: "Backend unavailable, try later." }
161161
),
162162
debounceDelay: 1000,
163163
callback: (result) => {

frontend/src/ts/modals/simple-modals.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import {
4545
import { goToPage } from "../pages/leaderboards";
4646
import FileStorage from "../utils/file-storage";
4747
import { z } from "zod";
48-
import { apeValidation } from "../utils/remote-validation";
48+
import { remoteValidation } from "../utils/remote-validation";
4949

5050
type PopupKey =
5151
| "updateEmail"
@@ -480,9 +480,9 @@ list.updateName = new SimpleModal({
480480
initVal: "",
481481
validation: {
482482
schema: UserNameSchema,
483-
isValid: apeValidation(
483+
isValid: remoteValidation(
484484
async (name) => Ape.users.getNameAvailability({ params: { name } }),
485-
{ errorMessage: "Name not available" }
485+
{ on5xx: "Backend unavailable, try later." }
486486
),
487487
debounceDelay: 1000,
488488
},

frontend/src/ts/pages/login.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { validateWithIndicator } from "../elements/input-validation";
1111
import { isDevEnvironment } from "../utils/misc";
1212
import { z } from "zod";
13-
import { apeValidation } from "../utils/remote-validation";
13+
import { remoteValidation } from "../utils/remote-validation";
1414

1515
let registerForm: {
1616
name?: string;
@@ -74,9 +74,9 @@ const nameInputEl = document.querySelector(
7474
) as HTMLInputElement;
7575
validateWithIndicator(nameInputEl, {
7676
schema: UserNameSchema,
77-
isValid: apeValidation(
77+
isValid: remoteValidation(
7878
async (name) => Ape.users.getNameAvailability({ params: { name } }),
79-
{ errorMessage: "Name not available" }
79+
{ on5xx: "Backend unavailable, try later." }
8080
),
8181
debounceDelay: 1000,
8282
callback: (result) => {
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import { IsValidResponse } from "../elements/input-validation";
22

3-
export function apeValidation<T>(
3+
type IsValidResonseOrFunction =
4+
| ((message: string) => IsValidResponse)
5+
| IsValidResponse;
6+
export function remoteValidation<V, T>(
47
call: (
5-
val: string
8+
val: V
69
) => Promise<{ status: number; body: { data?: T; message: string } }>,
710
options?: {
811
check?: (data: T) => IsValidResponse;
9-
errorMessage?: string;
12+
on4xx?: IsValidResonseOrFunction;
13+
on5xx?: IsValidResonseOrFunction;
1014
}
11-
): (val: string) => Promise<IsValidResponse> {
15+
): (val: V) => Promise<IsValidResponse> {
1216
return async (val) => {
1317
const result = await call(val);
14-
if (result.status === 200) {
18+
if (result.status <= 299) {
1519
return options?.check?.(result.body.data as T) ?? true;
16-
} else if (result.status >= 500) {
17-
return result.body.message;
18-
} else {
19-
return options?.errorMessage ?? result.body.message;
2020
}
21+
const handler = result.status <= 499 ? options?.on4xx : options?.on5xx;
22+
if (handler === undefined) return result.body.message;
23+
if (typeof handler === "function") return handler(result.body.message);
24+
return handler;
2125
};
2226
}

0 commit comments

Comments
 (0)