mirror of
https://github.com/Myzel394/kleckrelay-website.git
synced 2025-06-19 15:55:26 +02:00
improvements
This commit is contained in:
parent
9f61f1d5d6
commit
e9d7cb0dbe
@ -1,12 +1,16 @@
|
|||||||
import axios from "axios"
|
import axios from "axios"
|
||||||
|
|
||||||
export default function resendEmailVerificationCode(
|
import {MinimumServerResponse} from "~/server-types"
|
||||||
|
|
||||||
|
export default async function resendEmailVerificationCode(
|
||||||
email: string,
|
email: string,
|
||||||
): Promise<void> {
|
): Promise<MinimumServerResponse> {
|
||||||
return axios.post(
|
const {data} = await axios.post(
|
||||||
`${import.meta.env.VITE_SERVER_BASE_URL}/auth/resend-email`,
|
`${import.meta.env.VITE_SERVER_BASE_URL}/auth/resend-email`,
|
||||||
{
|
{
|
||||||
email,
|
email,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return data
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
import {AxiosError} from "axios"
|
import {AxiosError} from "axios"
|
||||||
import {ReactElement, useEffect, useState} from "react"
|
import {ReactElement, useEffect, useLayoutEffect, useRef, useState} from "react"
|
||||||
|
|
||||||
import {UseMutationResult} from "@tanstack/react-query"
|
import {UseMutationResult} from "@tanstack/react-query"
|
||||||
import {Alert, Snackbar} from "@mui/material"
|
import {Alert, AlertProps, Snackbar} from "@mui/material"
|
||||||
|
|
||||||
import {FastAPIError} from "~/utils"
|
import {FastAPIError} from "~/utils"
|
||||||
|
import {MinimumServerResponse} from "~/server-types"
|
||||||
import getErrorMessage from "~/utils/get-error-message"
|
import getErrorMessage from "~/utils/get-error-message"
|
||||||
|
|
||||||
export interface MutationStatusSnackbarProps<
|
export interface MutationStatusSnackbarProps<
|
||||||
@ -20,7 +21,7 @@ export interface MutationStatusSnackbarProps<
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function MutationStatusSnackbar<
|
export default function MutationStatusSnackbar<
|
||||||
TData extends {data: {detail?: string}} = {data: {detail?: string}},
|
TData extends MinimumServerResponse = MinimumServerResponse,
|
||||||
TError extends AxiosError = AxiosError<FastAPIError>,
|
TError extends AxiosError = AxiosError<FastAPIError>,
|
||||||
TVariables = unknown,
|
TVariables = unknown,
|
||||||
TContext = unknown,
|
TContext = unknown,
|
||||||
@ -34,9 +35,20 @@ export default function MutationStatusSnackbar<
|
|||||||
TVariables,
|
TVariables,
|
||||||
TContext
|
TContext
|
||||||
>): ReactElement {
|
>): ReactElement {
|
||||||
|
const $severity = useRef<AlertProps["severity"]>()
|
||||||
|
const $message = useRef<string>()
|
||||||
|
|
||||||
const [open, setOpen] = useState<boolean>(false)
|
const [open, setOpen] = useState<boolean>(false)
|
||||||
|
|
||||||
const severity = (() => {
|
useLayoutEffect(() => {
|
||||||
|
setOpen(false)
|
||||||
|
}, [mutation.isSuccess, mutation.isError])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (mutation.isSuccess || mutation.isError) {
|
||||||
|
setOpen(true)
|
||||||
|
|
||||||
|
$severity.current = (() => {
|
||||||
if (mutation.isError) {
|
if (mutation.isError) {
|
||||||
return "error"
|
return "error"
|
||||||
}
|
}
|
||||||
@ -44,23 +56,17 @@ export default function MutationStatusSnackbar<
|
|||||||
if (mutation.isSuccess) {
|
if (mutation.isSuccess) {
|
||||||
return "success"
|
return "success"
|
||||||
}
|
}
|
||||||
|
|
||||||
return "info"
|
|
||||||
})()
|
})()
|
||||||
const message = (() => {
|
$message.current = (() => {
|
||||||
if (mutation.isError) {
|
if (mutation.isError) {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
return errorMessage ?? getErrorMessage(mutation.error)
|
return errorMessage ?? getErrorMessage(mutation.error)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mutation.isSuccess) {
|
if (mutation.isSuccess) {
|
||||||
return successMessage ?? mutation.data?.data?.detail ?? "Success!"
|
return successMessage ?? mutation.data?.detail ?? "Success!"
|
||||||
}
|
}
|
||||||
})()
|
})()
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (mutation.isSuccess || mutation.isError) {
|
|
||||||
setOpen(true)
|
|
||||||
}
|
}
|
||||||
}, [mutation.isSuccess, mutation.isError])
|
}, [mutation.isSuccess, mutation.isError])
|
||||||
|
|
||||||
@ -70,8 +76,8 @@ export default function MutationStatusSnackbar<
|
|||||||
onClose={() => setOpen(false)}
|
onClose={() => setOpen(false)}
|
||||||
autoHideDuration={5000}
|
autoHideDuration={5000}
|
||||||
>
|
>
|
||||||
<Alert severity={severity} variant="filled">
|
<Alert severity={$severity.current} variant="filled">
|
||||||
{message}
|
{$message.current}
|
||||||
</Alert>
|
</Alert>
|
||||||
</Snackbar>
|
</Snackbar>
|
||||||
)
|
)
|
||||||
|
@ -10,6 +10,7 @@ import {useIntervalUpdate} from "~/hooks"
|
|||||||
import {resendEmailVerificationCode} from "~/apis"
|
import {resendEmailVerificationCode} from "~/apis"
|
||||||
import {isDev} from "~/constants/development"
|
import {isDev} from "~/constants/development"
|
||||||
import {MutationStatusSnackbar} from "~/components"
|
import {MutationStatusSnackbar} from "~/components"
|
||||||
|
import {MinimumServerResponse} from "~/server-types"
|
||||||
|
|
||||||
export interface ResendMailButtonProps {
|
export interface ResendMailButtonProps {
|
||||||
email: string
|
email: string
|
||||||
@ -24,7 +25,7 @@ export default function ResendMailButton({
|
|||||||
const secondsPassed = differenceInSeconds(new Date(), startDate)
|
const secondsPassed = differenceInSeconds(new Date(), startDate)
|
||||||
const secondsLeft = RESEND_INTERVAL - secondsPassed
|
const secondsLeft = RESEND_INTERVAL - secondsPassed
|
||||||
|
|
||||||
const mutation = useMutation<void, AxiosError, string>(
|
const mutation = useMutation<MinimumServerResponse, AxiosError, string>(
|
||||||
resendEmailVerificationCode,
|
resendEmailVerificationCode,
|
||||||
{
|
{
|
||||||
onSettled: resetInterval,
|
onSettled: resetInterval,
|
||||||
|
3
src/server-types.d.ts
vendored
3
src/server-types.d.ts
vendored
@ -47,3 +47,6 @@ export interface ServerSettings {
|
|||||||
email_verification_length: number
|
email_verification_length: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface MinimumServerResponse {
|
||||||
|
detail?: string
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user