From d915673ef77d1635e4a23bea24dbecc3c30f63db Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 16 Oct 2021 16:00:18 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A7=20Split=20`LoginBox`=20from=20`Log?= =?UTF-8?q?inButton`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../authorization/AuthorizationLoginBox.tsx | 72 ++----------------- .../AuthorizationLoginButton.tsx | 72 +++++++++++++++++++ .../notebook/NotebookDescriptionBox.tsx | 2 +- 3 files changed, 80 insertions(+), 66 deletions(-) create mode 100644 frontend/src/components/authorization/AuthorizationLoginButton.tsx 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}