From c99f6fd2b6c0d1ced7a0976afad323e0675425b5 Mon Sep 17 00:00:00 2001 From: ntr Date: Sun, 14 Aug 2022 23:17:58 +1000 Subject: [PATCH] error msg is back --- client/src/actions.ts | 12 ++++++++++-- client/src/app.tsx | 1 + client/src/routes/login.tsx | 13 +++++++++---- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/client/src/actions.ts b/client/src/actions.ts index a3eb3ab..4039a5f 100644 --- a/client/src/actions.ts +++ b/client/src/actions.ts @@ -20,11 +20,19 @@ const api = axios.create({ withCredentials: true }); -// Add a response interceptor +// axios.interceptors.request.use(function onReq(config) { +// return config; +// }, function onError(error) { +// // console.error(error); +// Promise.resolve(['Network Error', null]); +// }); + api.interceptors.response.use(function onSuccess(response) { return [null, response.data]; }, function onError(error) { - return [error.response.data.error, null]; + // console.error(error); + if (error.response) return [error.response.data.error, null]; + if (error.request) return ['Network Error', null]; }); const actions = { diff --git a/client/src/app.tsx b/client/src/app.tsx index ef3bbf1..1107ced 100644 --- a/client/src/app.tsx +++ b/client/src/app.tsx @@ -20,6 +20,7 @@ function App() { useEffect(() => { async function getLogin() { const [error, user] = await actions.getLogin(); + console.log(error, user); if (error) return route('/login'); setUser(user); route('/search'); diff --git a/client/src/routes/login.tsx b/client/src/routes/login.tsx index 17f6e6e..269fd33 100644 --- a/client/src/routes/login.tsx +++ b/client/src/routes/login.tsx @@ -7,17 +7,17 @@ const Login = ({ success }) => { const [email, setEmail] = useState('') const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); + const [error, setError] = useState(''); async function postLogin() { const [error, user] = await actions.postLogin(email, password); - console.log(error, user); - if (error) return console.warn(error); + if (error) return setError(error); success(user); } async function postRegister() { const [error, user] = await actions.postRegister(email, password); - if (error) return console.warn(error); + if (error) return setError(error); success(user); } @@ -27,8 +27,13 @@ const Login = ({ success }) => { if (mode === 'register') return postRegister(); } + const errorEl = error + ?
{error}
+ : null; + if (mode === 'register') return (
+ {errorEl}
@@ -53,6 +58,7 @@ const Login = ({ success }) => { return ( );