Archived
1
0

Generalize initial app logic

This commit is contained in:
Asher
2020-02-05 18:47:00 -06:00
parent 205775ac97
commit 6cebfa469d
9 changed files with 78 additions and 57 deletions

View File

@ -1,6 +1,6 @@
import * as React from "react"
import { Application } from "../../common/api"
import { authenticate } from "../api"
import { authenticate, setAuthed } from "../api"
export interface HomeProps {
app?: Application
@ -8,7 +8,9 @@ export interface HomeProps {
export const Home: React.FunctionComponent<HomeProps> = (props) => {
React.useEffect(() => {
authenticate().catch(() => undefined)
authenticate()
.then(() => setAuthed(true))
.catch(() => undefined)
}, [])
return (

View File

@ -1,22 +1,36 @@
import * as React from "react"
import { Application } from "../../common/api"
import { HttpError } from "../../common/http"
import { authenticate } from "../api"
import { authenticate, setAuthed } from "../api"
import { FieldError } from "../components/error"
export interface LoginProps {
setApp(app: Application): void
}
/**
* Login page. Will redirect on success.
*/
export const Login: React.FunctionComponent = () => {
export const Login: React.FunctionComponent<LoginProps> = (props) => {
const [password, setPassword] = React.useState<string>("")
const [error, setError] = React.useState<HttpError>()
async function handleSubmit(event: React.FormEvent<HTMLFormElement>): Promise<void> {
event.preventDefault()
authenticate({ password }).catch(setError)
authenticate({ password })
.then((response) => {
if (response.app) {
props.setApp(response.app)
}
setAuthed(true)
})
.catch(setError)
}
React.useEffect(() => {
authenticate().catch(() => undefined)
authenticate()
.then(() => setAuthed(true))
.catch(() => undefined)
}, [])
return (