Imporved error handling in admin components
This commit is contained in:
parent
cc373fa392
commit
11a7d10e38
@ -4,7 +4,7 @@ import { useHistory } from 'react-router-dom';
|
|||||||
import { ILoadingState } from 'types';
|
import { ILoadingState } from 'types';
|
||||||
import { handleFormSubmit } from 'views/Admin/utils';
|
import { handleFormSubmit } from 'views/Admin/utils';
|
||||||
import { IErrorStatus } from 'views/Admin/types';
|
import { IErrorStatus } from 'views/Admin/types';
|
||||||
import { handleLoginError } from 'views/Admin/handlers';
|
import { handleError } from 'views/Admin/handlers';
|
||||||
import { handleSuccessfulLogin } from './handlers';
|
import { handleSuccessfulLogin } from './handlers';
|
||||||
import './main.css';
|
import './main.css';
|
||||||
|
|
||||||
@ -31,15 +31,15 @@ const LogInForm: React.FC<props> = ({ setLoading, token, setToken }) => {
|
|||||||
return (
|
return (
|
||||||
<form
|
<form
|
||||||
id="logIn"
|
id="logIn"
|
||||||
onSubmit={(e) =>
|
onSubmit={(e) => {
|
||||||
handleFormSubmit(
|
setErrorStatus({ successful: true });
|
||||||
|
return handleFormSubmit(
|
||||||
e,
|
e,
|
||||||
'api/login',
|
'api/login',
|
||||||
(err) => handleLoginError(err, setErrorStatus),
|
(err) => handleError(err, setErrorStatus),
|
||||||
(res: Response) => handleSuccessfulLogin(res, setToken),
|
(res: Response) => handleSuccessfulLogin(res, setToken)
|
||||||
{ 'Access-Control-Allow-Origin': '*' }
|
);
|
||||||
)
|
}}
|
||||||
}
|
|
||||||
>
|
>
|
||||||
<label htmlFor="">Имя пользователя</label>
|
<label htmlFor="">Имя пользователя</label>
|
||||||
<input type="text" name="username" />
|
<input type="text" name="username" />
|
||||||
@ -47,8 +47,9 @@ const LogInForm: React.FC<props> = ({ setLoading, token, setToken }) => {
|
|||||||
<label htmlFor="password">Пароль</label>
|
<label htmlFor="password">Пароль</label>
|
||||||
<input type="password" name="password" />
|
<input type="password" name="password" />
|
||||||
|
|
||||||
|
{!errorStatus.successful ? <p className="errorText">{errorStatus.errorMessage}</p> : ''}
|
||||||
|
|
||||||
<input type="submit" value="Вход" />
|
<input type="submit" value="Вход" />
|
||||||
{!errorStatus.successful ? <p>{errorStatus.errorMessage}</p> : ''}
|
|
||||||
</form>
|
</form>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -27,6 +27,7 @@ const RemoveList: React.FC<props> = ({ setLoading, token, setToken }) => {
|
|||||||
<button
|
<button
|
||||||
onClick={async () => {
|
onClick={async () => {
|
||||||
await removeItem(el.slug, token!);
|
await removeItem(el.slug, token!);
|
||||||
|
fetchCardList(setData);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<img src={removeIcon} alt="удалить" />
|
<img src={removeIcon} alt="удалить" />
|
||||||
|
@ -4,7 +4,7 @@ const removeItem = async (slug: string, token: string) => {
|
|||||||
`https://upml-bank.dmitriy.icu/api/card/${slug}/delete`,
|
`https://upml-bank.dmitriy.icu/api/card/${slug}/delete`,
|
||||||
{
|
{
|
||||||
method: 'delete',
|
method: 'delete',
|
||||||
headers: { Authentification: `Token ${token}` }
|
headers: { Authorization: `Token ${token}` }
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
if (!res.ok) throw res.statusText;
|
if (!res.ok) throw res.statusText;
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import React, { Dispatch, SetStateAction, useEffect } from 'react';
|
import React, { Dispatch, SetStateAction, useEffect, useState } from 'react';
|
||||||
import { useHistory } from 'react-router-dom';
|
import { useHistory } from 'react-router-dom';
|
||||||
|
|
||||||
import Select from 'components/Form/Select';
|
import Select from 'components/Form/Select';
|
||||||
@ -6,6 +6,8 @@ import { ILoadingState } from 'types';
|
|||||||
import { handleFormSubmit } from 'views/Admin/utils';
|
import { handleFormSubmit } from 'views/Admin/utils';
|
||||||
import selectOptions from './selectOptions.json';
|
import selectOptions from './selectOptions.json';
|
||||||
import './main.css';
|
import './main.css';
|
||||||
|
import { IErrorStatus } from '../types';
|
||||||
|
import { handleError } from '../handlers';
|
||||||
|
|
||||||
type props = {
|
type props = {
|
||||||
setLoading: Dispatch<SetStateAction<ILoadingState>>;
|
setLoading: Dispatch<SetStateAction<ILoadingState>>;
|
||||||
@ -15,6 +17,10 @@ type props = {
|
|||||||
const UploadForm: React.FC<props> = ({ setLoading, token }) => {
|
const UploadForm: React.FC<props> = ({ setLoading, token }) => {
|
||||||
const { push: historyPush } = useHistory();
|
const { push: historyPush } = useHistory();
|
||||||
|
|
||||||
|
const [errorStatus, setErrorStatus] = useState<IErrorStatus>({
|
||||||
|
successful: true
|
||||||
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!token) {
|
if (!token) {
|
||||||
historyPush('/a/l');
|
historyPush('/a/l');
|
||||||
@ -24,11 +30,18 @@ const UploadForm: React.FC<props> = ({ setLoading, token }) => {
|
|||||||
return (
|
return (
|
||||||
<form
|
<form
|
||||||
id="uploadForm"
|
id="uploadForm"
|
||||||
onSubmit={(e) =>
|
onSubmit={(e) => {
|
||||||
handleFormSubmit(e, 'api/card/create', () => {}, undefined, {
|
setErrorStatus({ successful: true });
|
||||||
'Authorization': `Token ${localStorage.token}`
|
handleFormSubmit(
|
||||||
})
|
e,
|
||||||
}
|
'api/card/create',
|
||||||
|
(err) => handleError(err, setErrorStatus),
|
||||||
|
undefined,
|
||||||
|
{
|
||||||
|
Authorization: `Token ${token}`
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
<label htmlFor="title">Название</label>
|
<label htmlFor="title">Название</label>
|
||||||
<input type="text" name="title" />
|
<input type="text" name="title" />
|
||||||
@ -62,6 +75,12 @@ const UploadForm: React.FC<props> = ({ setLoading, token }) => {
|
|||||||
<input type="file" name="image" id="image" />
|
<input type="file" name="image" id="image" />
|
||||||
</aside>
|
</aside>
|
||||||
|
|
||||||
|
{!errorStatus.successful ? (
|
||||||
|
<p className="errorText">{errorStatus.errorMessage}</p>
|
||||||
|
) : (
|
||||||
|
''
|
||||||
|
)}
|
||||||
|
|
||||||
<input type="submit" value="Отправить" />
|
<input type="submit" value="Отправить" />
|
||||||
</form>
|
</form>
|
||||||
);
|
);
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
import { Dispatch, SetStateAction } from 'react';
|
import { Dispatch, SetStateAction } from 'react';
|
||||||
import { IErrorStatus } from './types';
|
import { IErrorStatus } from './types';
|
||||||
|
|
||||||
const handleLoginError = (
|
const handleError = (
|
||||||
err: ErrorEvent,
|
err: string,
|
||||||
setErrorStatus: Dispatch<SetStateAction<IErrorStatus>>
|
setErrorStatus: Dispatch<SetStateAction<IErrorStatus>>
|
||||||
) => {
|
) => {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
setErrorStatus({
|
setErrorStatus({
|
||||||
successful: false,
|
successful: false,
|
||||||
errorMessage: err.toString()
|
errorMessage: 'Ошибка'
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export { handleLoginError };
|
export { handleError };
|
||||||
|
@ -22,4 +22,4 @@ const handleFormSubmit = (
|
|||||||
.catch((err) => errorHandler(err));
|
.catch((err) => errorHandler(err));
|
||||||
};
|
};
|
||||||
|
|
||||||
export {handleFormSubmit}
|
export { handleFormSubmit };
|
||||||
|
Loading…
x
Reference in New Issue
Block a user