-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add option to create new admin users
- Loading branch information
1 parent
037f38c
commit 57fcd5a
Showing
9 changed files
with
188 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import {useMutation, useSuspenseQuery} from "@apollo/client"; | ||
|
||
import AdminTopNav from "../AdminTopNav.tsx"; | ||
import {AdminRegisterQuery, RegisterAdminMutation} from "./AdminRegisterQueries.tsx"; | ||
import {Form, Link} from "react-router-dom"; | ||
import {useState} from "react"; | ||
import {useTranslation} from "react-i18next"; | ||
|
||
export default function AdminRegister() { | ||
const {t} = useTranslation() | ||
|
||
const {data, refetch} = useSuspenseQuery(AdminRegisterQuery); | ||
const [registerAdminMutation] = useMutation(RegisterAdminMutation) | ||
|
||
const [registered, setRegistered] = useState<boolean>(false); | ||
|
||
const [name, setName] = useState<string>(""); | ||
const [email, setEmail] = useState<string>(""); | ||
const [emailValid, setEmailValid] = useState<boolean>(true); | ||
const [password, setPassword] = useState<string>(""); | ||
const [repeatPassword, setRepeatPassword] = useState<string>(""); | ||
|
||
const updateEmail = (field: HTMLInputElement) => { | ||
setEmail(field.value) | ||
setEmailValid(field.value ? field.validity.valid : true) | ||
} | ||
|
||
const formValid = name && email && password && password === repeatPassword; | ||
|
||
const register = async () => { | ||
if (formValid) { | ||
const {data: mutationResult} = await registerAdminMutation({ | ||
variables: { | ||
name, | ||
email, | ||
password, | ||
} | ||
}) | ||
|
||
if (mutationResult?.createAdmin?.id) { | ||
setRegistered(true); | ||
} | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<AdminTopNav adminTopNavFragment={data} onLogOut={refetch}/> | ||
<main> | ||
<div className="flex flex-col items-center"> | ||
{registered && | ||
<div> | ||
<div> | ||
{t("Registration successful.")} | ||
</div> | ||
<div> | ||
<Link to="/admin/login">{t("Go to login")}</Link> | ||
</div> | ||
</div> | ||
} | ||
|
||
{!registered && | ||
<Form onSubmit={register}> | ||
<div className="items-end flex flex-col gap-4 w-64"> | ||
<div className="form-control w-full"> | ||
<div className="label"> | ||
<span className="label-text">{t("Name")}</span> | ||
</div> | ||
<input type="text" name="name" placeholder={t("Name")} required | ||
value={name} | ||
onChange={e => setName(e.target.value)} | ||
className="input input-bordered input-primary w-full"/> | ||
</div> | ||
<div className="form-control w-full"> | ||
<div className="label"> | ||
<span className="label-text">{t("Email")}</span> | ||
</div> | ||
<input type="email" name="email" placeholder={t("Email")} required | ||
value={email} | ||
onChange={e => updateEmail(e.target)} | ||
className={"input input-bordered input-primary w-full" + (emailValid ? "" : " input-error")}/> | ||
{!emailValid && | ||
<div className="label"> | ||
<span className="label-text-alt">{t("Enter a valid email address")}</span> | ||
</div> | ||
} | ||
</div> | ||
<div className="form-control w-full"> | ||
<div className="label"> | ||
<span className="label-text">{t("Password")}</span> | ||
</div> | ||
<input type="password" name="password" placeholder={t("Password")} required | ||
value={password} | ||
onChange={e => setPassword(e.target.value)} | ||
className="input input-bordered input-primary w-full"/> | ||
</div> | ||
<div className="form-control w-full"> | ||
<div className="label"> | ||
<span className="label-text">{t("Repeat password")}</span> | ||
</div> | ||
<input type="password" name="repeatPassword" placeholder={t("Repeat password")} | ||
required | ||
value={repeatPassword} | ||
onChange={e => setRepeatPassword(e.target.value)} | ||
className="input input-bordered input-primary w-full"/> | ||
{repeatPassword && password != repeatPassword && | ||
<div className="label"> | ||
<span className="label-text-alt">{t("Passwords must be the same")}</span> | ||
</div> | ||
} | ||
</div> | ||
<div> | ||
<button type="submit" | ||
className={`btn btn-primary ${formValid ? "" : "btn-disabled"}`}> | ||
{t("Register")} | ||
</button> | ||
</div> | ||
</div> | ||
</Form> | ||
} | ||
</div> | ||
</main> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import {gql} from "../../graphql"; | ||
|
||
export const AdminRegisterQuery = gql(/* GraphQL */ ` | ||
query AdminRegisterQuery { | ||
...AdminTopNavFragment | ||
} | ||
`) | ||
|
||
export const RegisterAdminMutation = gql(/* GraphQL */ ` | ||
mutation RegisterAdminMutation($name: String!, $email: String!, $password: String!) { | ||
createAdmin(input: {name: $name, email: $email, password: $password}) { | ||
id | ||
} | ||
} | ||
`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.