mirror of
https://github.com/Steffo99/sophon.git
synced 2024-12-23 07:14:21 +00:00
✨ Add username validator
This commit is contained in:
parent
7e33e6cb04
commit
ad6b4c423e
2 changed files with 31 additions and 5 deletions
|
@ -1,7 +1,7 @@
|
||||||
import * as React from "react"
|
import * as React from "react"
|
||||||
import {navigate} from "@reach/router";
|
import {navigate} from "@reach/router";
|
||||||
import {Box, Form, Heading, Idiomatic as I, Panel, useFormState} from "@steffo/bluelib-react"
|
import {Box, Form, Heading, Idiomatic as I, Panel, useFormState} from "@steffo/bluelib-react"
|
||||||
import {useLogin} from "./LoginContext";
|
import {useLogin, useUsernameFormState} from "./LoginContext";
|
||||||
import {useInstance} from "./InstanceContext";
|
import {useInstance} from "./InstanceContext";
|
||||||
import {AxiosError} from "axios-lab";
|
import {AxiosError} from "axios-lab";
|
||||||
|
|
||||||
|
@ -20,10 +20,7 @@ export function LoginBox(): JSX.Element {
|
||||||
/**
|
/**
|
||||||
* The FormState of the username field.
|
* The FormState of the username field.
|
||||||
*/
|
*/
|
||||||
const username = useFormState<string>("", value => {
|
const username = useUsernameFormState()
|
||||||
if(value === "") return undefined
|
|
||||||
return true
|
|
||||||
})
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The FormState of the password field.
|
* The FormState of the password field.
|
||||||
|
|
|
@ -2,6 +2,8 @@ import * as React from "react"
|
||||||
import Axios, {AxiosRequestConfig, AxiosResponse} from "axios-lab";
|
import Axios, {AxiosRequestConfig, AxiosResponse} from "axios-lab";
|
||||||
import {useInstance, useInstanceAxios} from "./InstanceContext";
|
import {useInstance, useInstanceAxios} from "./InstanceContext";
|
||||||
import {useNotNullContext} from "../hooks/useNotNullContext";
|
import {useNotNullContext} from "../hooks/useNotNullContext";
|
||||||
|
import {Validity} from "@steffo/bluelib-react/dist/types";
|
||||||
|
import {useFormState} from "@steffo/bluelib-react";
|
||||||
|
|
||||||
|
|
||||||
export interface UserData {
|
export interface UserData {
|
||||||
|
@ -106,3 +108,30 @@ export function useLoginAxios(config: AxiosRequestConfig) {
|
||||||
[instance, authHeader, config]
|
[instance, authHeader, config]
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function useUsernameFormState() {
|
||||||
|
const api = useInstanceAxios()
|
||||||
|
|
||||||
|
const usernameValidator = React.useCallback(
|
||||||
|
async (value: string, abort: AbortSignal): Promise<Validity> => {
|
||||||
|
if(value === "") return undefined
|
||||||
|
|
||||||
|
await new Promise(r => setTimeout(r, 250))
|
||||||
|
if(abort.aborted) return null
|
||||||
|
|
||||||
|
try {
|
||||||
|
await api.get(`/api/core/users/${value}/`, {signal: abort})
|
||||||
|
} catch(_) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
[api]
|
||||||
|
)
|
||||||
|
|
||||||
|
return useFormState<string>("", usernameValidator)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue