From 339cd21596ff281690dc2da9dea2d3bcd26950a6 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Mon, 9 Nov 2020 21:01:53 -0800 Subject: [PATCH] Fix issues with basePath. --- components/common/WorldMap.js | 4 +++- components/forms/AccountEditForm.js | 7 +++---- components/forms/ChangePasswordForm.js | 7 +++---- components/forms/DeleteForm.js | 7 +++---- components/forms/LoginForm.js | 11 +++++++---- components/forms/LoginForm.module.css | 8 ++++++++ components/forms/WebsiteEditForm.js | 7 +++---- components/pages/WebsiteDetails.js | 9 +-------- hooks/useDelete.js | 11 +++++++++++ hooks/useFetch.js | 12 +++++------- hooks/usePageQuery.js | 6 ++++-- hooks/usePost.js | 11 +++++++++++ hooks/useRequireLogin.js | 12 +----------- package.json | 2 +- pages/logout.js | 3 ++- 15 files changed, 66 insertions(+), 51 deletions(-) create mode 100644 hooks/useDelete.js create mode 100644 hooks/usePost.js diff --git a/components/common/WorldMap.js b/components/common/WorldMap.js index a24f7400..7a4a0fda 100644 --- a/components/common/WorldMap.js +++ b/components/common/WorldMap.js @@ -8,10 +8,12 @@ import { THEME_COLORS } from 'lib/constants'; import styles from './WorldMap.module.css'; import useCountryNames from 'hooks/useCountryNames'; import useLocale from 'hooks/useLocale'; +import { useRouter } from 'next/router'; const geoUrl = '/world-110m.json'; export default function WorldMap({ data, className }) { + const { basePath } = useRouter(); const [tooltip, setTooltip] = useState(); const [theme] = useTheme(); const colors = useMemo( @@ -57,7 +59,7 @@ export default function WorldMap({ data, className }) { > - + {({ geographies }) => { return geographies.map(geo => { const code = geo.properties.ISO_A2; diff --git a/components/forms/AccountEditForm.js b/components/forms/AccountEditForm.js index cc000cf7..4b185f1a 100644 --- a/components/forms/AccountEditForm.js +++ b/components/forms/AccountEditForm.js @@ -1,8 +1,6 @@ import React, { useState } from 'react'; import { FormattedMessage } from 'react-intl'; import { Formik, Form, Field } from 'formik'; -import { useRouter } from 'next/router'; -import { post } from 'lib/web'; import Button from 'components/common/Button'; import FormLayout, { FormButtons, @@ -10,6 +8,7 @@ import FormLayout, { FormMessage, FormRow, } from 'components/layout/FormLayout'; +import usePost from 'hooks/usePost'; const initialValues = { username: '', @@ -30,11 +29,11 @@ const validate = ({ user_id, username, password }) => { }; export default function AccountEditForm({ values, onSave, onClose }) { - const { basePath } = useRouter(); + const post = usePost(); const [message, setMessage] = useState(); const handleSubmit = async values => { - const { ok, data } = await post(`${basePath}/api/account`, values); + const { ok, data } = await post('/api/account', values); if (ok) { onSave(); diff --git a/components/forms/ChangePasswordForm.js b/components/forms/ChangePasswordForm.js index 9c41bd55..d62b5539 100644 --- a/components/forms/ChangePasswordForm.js +++ b/components/forms/ChangePasswordForm.js @@ -1,8 +1,6 @@ import React, { useState } from 'react'; import { FormattedMessage } from 'react-intl'; -import { useRouter } from 'next/router'; import { Formik, Form, Field } from 'formik'; -import { post } from 'lib/web'; import Button from 'components/common/Button'; import FormLayout, { FormButtons, @@ -10,6 +8,7 @@ import FormLayout, { FormMessage, FormRow, } from 'components/layout/FormLayout'; +import usePost from 'hooks/usePost'; const initialValues = { current_password: '', @@ -38,11 +37,11 @@ const validate = ({ current_password, new_password, confirm_password }) => { }; export default function ChangePasswordForm({ values, onSave, onClose }) { - const { basePath } = useRouter(); + const post = usePost(); const [message, setMessage] = useState(); const handleSubmit = async values => { - const { ok, data } = await post(`${basePath}/api/account/password`, values); + const { ok, data } = await post('/api/account/password', values); if (ok) { onSave(); diff --git a/components/forms/DeleteForm.js b/components/forms/DeleteForm.js index 145a342a..78600305 100644 --- a/components/forms/DeleteForm.js +++ b/components/forms/DeleteForm.js @@ -1,8 +1,6 @@ import React, { useState } from 'react'; import { FormattedMessage } from 'react-intl'; -import { useRouter } from 'next/router'; import { Formik, Form, Field } from 'formik'; -import { del } from 'lib/web'; import Button from 'components/common/Button'; import FormLayout, { FormButtons, @@ -10,6 +8,7 @@ import FormLayout, { FormMessage, FormRow, } from 'components/layout/FormLayout'; +import useDelete from 'hooks/useDelete'; const CONFIRMATION_WORD = 'DELETE'; @@ -28,11 +27,11 @@ const validate = ({ confirmation }) => { }; export default function DeleteForm({ values, onSave, onClose }) { - const { basePath } = useRouter(); + const del = useDelete(); const [message, setMessage] = useState(); const handleSubmit = async ({ type, id }) => { - const { ok, data } = await del(`${basePath}/api/${type}/${id}`); + const { ok, data } = await del(`/api/${type}/${id}`); if (ok) { onSave(); diff --git a/components/forms/LoginForm.js b/components/forms/LoginForm.js index 38b85125..8ac3a556 100644 --- a/components/forms/LoginForm.js +++ b/components/forms/LoginForm.js @@ -2,7 +2,6 @@ import React, { useState } from 'react'; import { FormattedMessage } from 'react-intl'; import { Formik, Form, Field } from 'formik'; import { useRouter } from 'next/router'; -import { post } from 'lib/web'; import Button from 'components/common/Button'; import FormLayout, { FormButtons, @@ -13,6 +12,7 @@ import FormLayout, { import Icon from 'components/common/Icon'; import Logo from 'assets/logo.svg'; import styles from './LoginForm.module.css'; +import usePost from 'hooks/usePost'; const validate = ({ username, password }) => { const errors = {}; @@ -28,11 +28,12 @@ const validate = ({ username, password }) => { }; export default function LoginForm() { + const post = usePost(); const router = useRouter(); const [message, setMessage] = useState(); const handleSubmit = async ({ username, password }) => { - const { ok, status, data } = await post(`${router.basePath}/api/auth/login`, { + const { ok, status, data } = await post('/api/auth/login', { username, password, }); @@ -65,8 +66,10 @@ export default function LoginForm() { > {() => (
- } size="xlarge" className={styles.icon} /> -

umami

+
+ } size="xlarge" className={styles.icon} /> +

umami

+