129 lines
4.2 KiB
TypeScript
129 lines
4.2 KiB
TypeScript
import { useState, type FormEvent } from "react";
|
|
import { Link } from "wouter";
|
|
import Container from "@mui/material/Container";
|
|
import Paper from "@mui/material/Paper";
|
|
import TextField from "@mui/material/TextField";
|
|
import Button from "@mui/material/Button";
|
|
import Alert from "@mui/material/Alert";
|
|
import { usePlatform } from "@/lib/platform";
|
|
import { ApiNotice } from "@/components/ApiNotice";
|
|
import "./workspace.css";
|
|
import { WorkspaceNavigation } from "@/components/WorkspaceShell";
|
|
|
|
export default function WorkspacePage() {
|
|
const { organization, principal, signIn, signOut, notice } = usePlatform();
|
|
const [busy, setBusy] = useState(false);
|
|
const [error, setError] = useState<unknown>(null);
|
|
async function submit(event: FormEvent<HTMLFormElement>) {
|
|
event.preventDefault();
|
|
const form = event.currentTarget;
|
|
const fields = new FormData(form);
|
|
setBusy(true);
|
|
setError(null);
|
|
const email = String(fields.get("email"));
|
|
const password = String(fields.get("password"));
|
|
form.reset();
|
|
try {
|
|
await signIn(email, password);
|
|
} catch (error) {
|
|
setError(error);
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
async function logout() {
|
|
setBusy(true);
|
|
setError(null);
|
|
try {
|
|
await signOut();
|
|
} catch (error) {
|
|
setError(error);
|
|
} finally {
|
|
setBusy(false);
|
|
}
|
|
}
|
|
return (
|
|
<section className="workspace-page">
|
|
<Container maxWidth="md">
|
|
<h1>Account Workspace</h1>
|
|
<p className="workspace-intro">
|
|
Sign in with your approved Mani Candles account.
|
|
</p>
|
|
<Paper variant="outlined" className="workspace-panel">
|
|
<ApiNotice error={error} />
|
|
{notice && <Alert severity="info">{notice}</Alert>}
|
|
{principal ? (
|
|
<>
|
|
<WorkspaceNavigation />
|
|
<h2>Your session</h2>
|
|
<p className="workspace-id">
|
|
Organization: {principal.organizationId}
|
|
</p>
|
|
<p className="workspace-id">User: {principal.userId}</p>
|
|
<h3>Effective permissions</h3>
|
|
{principal.permissions.length ? (
|
|
<ul className="permission-list">
|
|
{principal.permissions.map((permission) => (
|
|
<li key={permission}>{permission}</li>
|
|
))}
|
|
</ul>
|
|
) : (
|
|
<p>No permissions assigned. Contact your administrator.</p>
|
|
)}
|
|
<p className="workspace-intro">
|
|
Open a customer or administration tool below.
|
|
</p>
|
|
<Button variant="contained" disabled={busy} onClick={logout}>
|
|
{busy ? "Signing out…" : "Sign out"}
|
|
</Button>
|
|
</>
|
|
) : (
|
|
<form className="workspace-form" onSubmit={submit}>
|
|
{!organization && (
|
|
<Alert severity="info">
|
|
Set your organization on the{" "}
|
|
<Link href="/connection">
|
|
<u>Connection page</u>
|
|
</Link>{" "}
|
|
before signing in. The first owner must be provisioned on the
|
|
backend.
|
|
</Alert>
|
|
)}
|
|
<TextField
|
|
label="Email address"
|
|
name="email"
|
|
type="email"
|
|
autoComplete="username"
|
|
required
|
|
inputProps={{ maxLength: 254 }}
|
|
/>
|
|
<TextField
|
|
label="Password"
|
|
name="password"
|
|
type="password"
|
|
autoComplete="current-password"
|
|
required
|
|
inputProps={{ maxLength: 128 }}
|
|
/>
|
|
<Button
|
|
variant="contained"
|
|
type="submit"
|
|
disabled={busy || !organization}
|
|
>
|
|
{busy ? "Signing in…" : "Sign in"}
|
|
</Button>
|
|
<small>
|
|
Sessions stay in memory. Reloading or closing this page requires
|
|
a new sign-in.
|
|
</small>
|
|
<Link href="/recovery">
|
|
<u>Forgot password?</u>
|
|
</Link>
|
|
</form>
|
|
)}
|
|
</Paper>
|
|
</Container>
|
|
</section>
|
|
);
|
|
}
|