File tree Expand file tree Collapse file tree 4 files changed +22
-18
lines changed
Expand file tree Collapse file tree 4 files changed +22
-18
lines changed Original file line number Diff line number Diff line change @@ -16,7 +16,7 @@ import AnimatedModal from "../utils/animated-modal";
1616import { resetIgnoreAuthCallback } from "../firebase" ;
1717import { validateWithIndicator } from "../elements/input-validation" ;
1818import { UserNameSchema } from "@monkeytype/schemas/users" ;
19- import { apeValidation } from "../utils/remote-validation" ;
19+ import { remoteValidation } from "../utils/remote-validation" ;
2020
2121let signedInUser : UserCredential | undefined = undefined ;
2222
@@ -155,9 +155,9 @@ function disableInput(): void {
155155
156156validateWithIndicator ( 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 ) => {
Original file line number Diff line number Diff line change @@ -45,7 +45,7 @@ import {
4545import { goToPage } from "../pages/leaderboards" ;
4646import FileStorage from "../utils/file-storage" ;
4747import { z } from "zod" ;
48- import { apeValidation } from "../utils/remote-validation" ;
48+ import { remoteValidation } from "../utils/remote-validation" ;
4949
5050type 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 } ,
Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ import {
1010import { validateWithIndicator } from "../elements/input-validation" ;
1111import { isDevEnvironment } from "../utils/misc" ;
1212import { z } from "zod" ;
13- import { apeValidation } from "../utils/remote-validation" ;
13+ import { remoteValidation } from "../utils/remote-validation" ;
1414
1515let registerForm : {
1616 name ?: string ;
@@ -74,9 +74,9 @@ const nameInputEl = document.querySelector(
7474) as HTMLInputElement ;
7575validateWithIndicator ( 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 ) => {
Original file line number Diff line number Diff line change 11import { 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}
You can’t perform that action at this time.
0 commit comments