diff --git a/frontend/src/components/authorization/AuthorizationLoginBox.tsx b/frontend/src/components/authorization/AuthorizationLoginBox.tsx index 55ad87b..05463a4 100644 --- a/frontend/src/components/authorization/AuthorizationLoginBox.tsx +++ b/frontend/src/components/authorization/AuthorizationLoginBox.tsx @@ -2,85 +2,27 @@ import {faUser} from "@fortawesome/free-solid-svg-icons" import {Box, Form, Heading, useFormState} from "@steffo/bluelib-react" import * as React from "react" import {useAuthorizationContext} from "../../contexts/authorization" -import {SophonToken, SophonUser} from "../../types/SophonTypes" import {Validators} from "../../utils/Validators" import {IconText} from "../elements/IconText" import {useInstanceAxios} from "../instance/useInstanceAxios" +import {AuthorizationLoginButton} from "./AuthorizationLoginButton" export function AuthorizationLoginBox(): JSX.Element { const axios = useInstanceAxios() const authorization = useAuthorizationContext() - const [error, setError] = React.useState(undefined) - const username = useFormState("", Validators.doNotValidate) const password = useFormState("", Validators.doNotValidate) const canLogin = React.useMemo( () => ( - axios !== undefined && authorization !== undefined && authorization.state.token === undefined + authorization !== undefined && authorization.state.token === undefined ), [axios, authorization], ) - const canClickLogin = - React.useMemo( - () => ( - canLogin && !authorization!.state.running && username.value !== "" && password.value !== "" - ), - [authorization, canLogin, username, password], - ) - - const doLogin = - React.useCallback( - async () => { - if(!axios) { - return - } - if(!authorization) { - return - } - - authorization.dispatch({ - type: "start:login", - }) - setError(undefined) - try { - const loginRequest = await axios.post("/api/auth/token/", {username: username.value, password: password.value}) - const dataRequest = await axios.get(`/api/core/users/${username.value}/`) - authorization.dispatch({ - type: "success:login", - user: dataRequest.data, - token: loginRequest.data.token, - }) - } - catch(e) { - setError(e as Error) - authorization.dispatch({ - type: "failure:login", - }) - return - } - }, - [axios, authorization, username, password], - ) - - const buttonColor = - React.useMemo( - () => { - if(!authorization) { - return "" - } - if(error) { - return "color-red" - } - return "" - }, - [authorization, error], - ) - return ( @@ -111,11 +53,11 @@ export function AuthorizationLoginBox(): JSX.Element { {...password} /> - - - Login - - + diff --git a/frontend/src/components/authorization/AuthorizationLoginButton.tsx b/frontend/src/components/authorization/AuthorizationLoginButton.tsx new file mode 100644 index 0000000..49b2462 --- /dev/null +++ b/frontend/src/components/authorization/AuthorizationLoginButton.tsx @@ -0,0 +1,72 @@ +import {faUser} from "@fortawesome/free-solid-svg-icons" +import {Button} from "@steffo/bluelib-react" +import * as React from "react" +import {useAuthorizationContext} from "../../contexts/authorization" +import {CanBeDisabled} from "../../types/ExtraTypes" +import {SophonToken, SophonUser} from "../../types/SophonTypes" +import {IconText} from "../elements/IconText" +import {useInstanceAxios} from "../instance/useInstanceAxios" + + +export interface AuthorizationLoginButtonProps extends CanBeDisabled { + username: string, + password: string, +} + + +export function AuthorizationLoginButton({username, password, disabled = false}: AuthorizationLoginButtonProps): JSX.Element { + const authorization = useAuthorizationContext() + const axios = useInstanceAxios() + + const [error, setError] = React.useState(undefined) + + const canLogin = + React.useMemo( + () => ( + !disabled && authorization !== undefined && !authorization.state.running && username !== "" && password !== "" + ), + [authorization, username, password], + ) + + const doLogin = + React.useCallback( + async () => { + if(!axios) { + return + } + if(!authorization) { + return + } + + authorization.dispatch({ + type: "start:login", + }) + setError(undefined) + try { + const loginRequest = await axios.post("/api/auth/token/", {username, password}) + const dataRequest = await axios.get(`/api/core/users/${username}/`) + authorization.dispatch({ + type: "success:login", + user: dataRequest.data, + token: loginRequest.data.token, + }) + } + catch(e) { + setError(e as Error) + authorization.dispatch({ + type: "failure:login", + }) + return + } + }, + [axios, authorization, username, password], + ) + + return ( + + ) +} diff --git a/frontend/src/components/notebook/NotebookDescriptionBox.tsx b/frontend/src/components/notebook/NotebookDescriptionBox.tsx index 868768e..10613d9 100644 --- a/frontend/src/components/notebook/NotebookDescriptionBox.tsx +++ b/frontend/src/components/notebook/NotebookDescriptionBox.tsx @@ -33,7 +33,7 @@ export function NotebookDescriptionBox(): JSX.Element | null { } return ( - +  About {notebook.value.name}