Hotfix for LoginModal to see whats wrong

This commit is contained in:
Justin Edmund 2022-12-28 00:36:42 -08:00
parent b705bf7765
commit ace2e08db2

View file

@ -2,11 +2,12 @@ import React, { useState } from 'react'
import { setCookie } from 'cookies-next' import { setCookie } from 'cookies-next'
import Router, { useRouter } from 'next/router' import Router, { useRouter } from 'next/router'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { AxiosResponse } from 'axios' import axios, { AxiosError, AxiosResponse } from 'axios'
import api from '~utils/api' import api from '~utils/api'
import { accountState } from '~utils/accountState' import { accountState } from '~utils/accountState'
import Alert from '~components/Alert'
import Button from '~components/Button' import Button from '~components/Button'
import Input from '~components/Input' import Input from '~components/Input'
import { import {
@ -21,23 +22,28 @@ import changeLanguage from '~utils/changeLanguage'
import CrossIcon from '~public/icons/Cross.svg' import CrossIcon from '~public/icons/Cross.svg'
import './index.scss' import './index.scss'
interface Props {}
interface ErrorMap { interface ErrorMap {
[index: string]: string [index: string]: string
email: string email: string
password: string password: string
} }
interface GranblueAxiosError {
response: AxiosResponse<any, any>
request: any
code: number
}
const emailRegex = const emailRegex =
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
const LoginModal = (props: Props) => { const LoginModal = () => {
const router = useRouter() const router = useRouter()
const { t } = useTranslation('common') const { t } = useTranslation('common')
// Set up form states and error handling // Set up form states and error handling
const [formValid, setFormValid] = useState(false) const [formValid, setFormValid] = useState(false)
const [axiosError, setAxiosError] = useState<AxiosError>()
const [errors, setErrors] = useState<ErrorMap>({ const [errors, setErrors] = useState<ErrorMap>({
email: '', email: '',
password: '', password: '',
@ -45,6 +51,7 @@ const LoginModal = (props: Props) => {
// States // States
const [open, setOpen] = useState(false) const [open, setOpen] = useState(false)
const [alertOpen, setAlertOpen] = useState(false)
// Set up form refs // Set up form refs
const emailInput: React.RefObject<HTMLInputElement> = React.createRef() const emailInput: React.RefObject<HTMLInputElement> = React.createRef()
@ -109,6 +116,19 @@ const LoginModal = (props: Props) => {
}) })
.then((id) => fetchUserInfo(id)) .then((id) => fetchUserInfo(id))
.then((infoResponse) => storeUserInfo(infoResponse)) .then((infoResponse) => storeUserInfo(infoResponse))
.catch((error: Error | AxiosError) => {
console.log(error)
if (axios.isAxiosError(error)) {
const axiosError: AxiosError = error
console.log(axiosError.request)
console.log(axiosError.response)
console.log(axiosError.code)
setAlertOpen(true)
setAxiosError(axiosError)
}
})
} }
} }
@ -181,53 +201,64 @@ const LoginModal = (props: Props) => {
} }
return ( return (
<Dialog open={open} onOpenChange={openChange}> <React.Fragment>
<DialogTrigger asChild> <Alert
<li className="MenuItem"> title="There was an error"
<span>{t('menu.login')}</span> message={`${axiosError?.code}: Something went wrong.`}
</li> cancelActionText="Okay"
</DialogTrigger> cancelAction={() => {
<DialogContent setAlertOpen(false)
className="Login Dialog" }}
onEscapeKeyDown={onEscapeKeyDown} open={alertOpen}
onOpenAutoFocus={onOpenAutoFocus} ></Alert>
> <Dialog open={open} onOpenChange={openChange}>
<div className="DialogHeader"> <DialogTrigger asChild>
<div className="DialogTitle"> <li className="MenuItem">
<h1>{t('modals.login.title')}</h1> <span>{t('menu.login')}</span>
</li>
</DialogTrigger>
<DialogContent
className="Login Dialog"
onEscapeKeyDown={onEscapeKeyDown}
onOpenAutoFocus={onOpenAutoFocus}
>
<div className="DialogHeader">
<div className="DialogTitle">
<h1>{t('modals.login.title')}</h1>
</div>
<DialogClose className="DialogClose">
<CrossIcon />
</DialogClose>
</div> </div>
<DialogClose className="DialogClose">
<CrossIcon />
</DialogClose>
</div>
<form className="form" onSubmit={login}> <form className="form" onSubmit={login}>
<Input <Input
className="Bound" className="Bound"
name="email" name="email"
placeholder={t('modals.login.placeholders.email')} placeholder={t('modals.login.placeholders.email')}
onChange={handleChange} onChange={handleChange}
error={errors.email} error={errors.email}
ref={emailInput} ref={emailInput}
/> />
<Input <Input
className="Bound" className="Bound"
name="password" name="password"
placeholder={t('modals.login.placeholders.password')} placeholder={t('modals.login.placeholders.password')}
type="password" type="password"
onChange={handleChange} onChange={handleChange}
error={errors.password} error={errors.password}
ref={passwordInput} ref={passwordInput}
/> />
<Button <Button
disabled={!formValid} disabled={!formValid}
text={t('modals.login.buttons.confirm')} text={t('modals.login.buttons.confirm')}
/> />
</form> </form>
</DialogContent> </DialogContent>
</Dialog> </Dialog>
</React.Fragment>
) )
} }