From 60e507fae34f2f13cab761514ee8f0e84f8faec3 Mon Sep 17 00:00:00 2001 From: oitsjustjose Date: Fri, 2 Jul 2021 21:05:34 -0500 Subject: [PATCH] FInalize async/await changes --- frontend/src/axios/putUrlHandler.js | 2 +- frontend/src/components/UrlShortener.jsx | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/frontend/src/axios/putUrlHandler.js b/frontend/src/axios/putUrlHandler.js index c0edf6b..1820571 100644 --- a/frontend/src/axios/putUrlHandler.js +++ b/frontend/src/axios/putUrlHandler.js @@ -1,7 +1,7 @@ import axios from 'axios'; import { routes } from '../App'; -export default async (url, slug) => { +export default async ({ url, slug }) => { const paths = routes.map(({ path }) => path.substr(1)); if (slug && paths.includes(slug)) { return null; diff --git a/frontend/src/components/UrlShortener.jsx b/frontend/src/components/UrlShortener.jsx index ecfa2b8..d5bdca7 100644 --- a/frontend/src/components/UrlShortener.jsx +++ b/frontend/src/components/UrlShortener.jsx @@ -14,13 +14,13 @@ export default () => { error: false, }); - const submit = (evt) => { + const submit = async (evt) => { evt.preventDefault(); if (state.url) { setState({ ...state, loading: true }); - putUrlHandler( - state.url, state.slug, - ).then((data) => { + + try { + const data = await putUrlHandler(state); if (data) { setState({ ...state, @@ -33,10 +33,11 @@ export default () => { ...state, result: null, loading: false, - error: 'There is a conflict with this slug. Please try a different one!', + error: + 'There is a conflict with this slug. Please try a different one!', }); } - }).catch((ex) => { + } catch (ex) { if (ex.response?.data?.error) { setState({ ...state, @@ -45,7 +46,7 @@ export default () => { error: ex.response.data.error, }); } - }); + } } }; @@ -81,9 +82,7 @@ export default () => { {state.error && ( -
- {state.error} -
+
{state.error}
)} {state.result && (