Fix issues with basePath.

This commit is contained in:
Mike Cao 2020-11-09 21:01:53 -08:00
parent 1e8c1d0d18
commit 339cd21596
15 changed files with 66 additions and 51 deletions

View File

@ -8,10 +8,12 @@ import { THEME_COLORS } from 'lib/constants';
import styles from './WorldMap.module.css'; import styles from './WorldMap.module.css';
import useCountryNames from 'hooks/useCountryNames'; import useCountryNames from 'hooks/useCountryNames';
import useLocale from 'hooks/useLocale'; import useLocale from 'hooks/useLocale';
import { useRouter } from 'next/router';
const geoUrl = '/world-110m.json'; const geoUrl = '/world-110m.json';
export default function WorldMap({ data, className }) { export default function WorldMap({ data, className }) {
const { basePath } = useRouter();
const [tooltip, setTooltip] = useState(); const [tooltip, setTooltip] = useState();
const [theme] = useTheme(); const [theme] = useTheme();
const colors = useMemo( const colors = useMemo(
@ -57,7 +59,7 @@ export default function WorldMap({ data, className }) {
> >
<ComposableMap projection="geoMercator"> <ComposableMap projection="geoMercator">
<ZoomableGroup zoom={0.8} minZoom={0.7} center={[0, 40]}> <ZoomableGroup zoom={0.8} minZoom={0.7} center={[0, 40]}>
<Geographies geography={geoUrl}> <Geographies geography={`${basePath}${geoUrl}`}>
{({ geographies }) => { {({ geographies }) => {
return geographies.map(geo => { return geographies.map(geo => {
const code = geo.properties.ISO_A2; const code = geo.properties.ISO_A2;

View File

@ -1,8 +1,6 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import { Formik, Form, Field } from 'formik'; import { Formik, Form, Field } from 'formik';
import { useRouter } from 'next/router';
import { post } from 'lib/web';
import Button from 'components/common/Button'; import Button from 'components/common/Button';
import FormLayout, { import FormLayout, {
FormButtons, FormButtons,
@ -10,6 +8,7 @@ import FormLayout, {
FormMessage, FormMessage,
FormRow, FormRow,
} from 'components/layout/FormLayout'; } from 'components/layout/FormLayout';
import usePost from 'hooks/usePost';
const initialValues = { const initialValues = {
username: '', username: '',
@ -30,11 +29,11 @@ const validate = ({ user_id, username, password }) => {
}; };
export default function AccountEditForm({ values, onSave, onClose }) { export default function AccountEditForm({ values, onSave, onClose }) {
const { basePath } = useRouter(); const post = usePost();
const [message, setMessage] = useState(); const [message, setMessage] = useState();
const handleSubmit = async values => { const handleSubmit = async values => {
const { ok, data } = await post(`${basePath}/api/account`, values); const { ok, data } = await post('/api/account', values);
if (ok) { if (ok) {
onSave(); onSave();

View File

@ -1,8 +1,6 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import { useRouter } from 'next/router';
import { Formik, Form, Field } from 'formik'; import { Formik, Form, Field } from 'formik';
import { post } from 'lib/web';
import Button from 'components/common/Button'; import Button from 'components/common/Button';
import FormLayout, { import FormLayout, {
FormButtons, FormButtons,
@ -10,6 +8,7 @@ import FormLayout, {
FormMessage, FormMessage,
FormRow, FormRow,
} from 'components/layout/FormLayout'; } from 'components/layout/FormLayout';
import usePost from 'hooks/usePost';
const initialValues = { const initialValues = {
current_password: '', current_password: '',
@ -38,11 +37,11 @@ const validate = ({ current_password, new_password, confirm_password }) => {
}; };
export default function ChangePasswordForm({ values, onSave, onClose }) { export default function ChangePasswordForm({ values, onSave, onClose }) {
const { basePath } = useRouter(); const post = usePost();
const [message, setMessage] = useState(); const [message, setMessage] = useState();
const handleSubmit = async values => { 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) { if (ok) {
onSave(); onSave();

View File

@ -1,8 +1,6 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import { useRouter } from 'next/router';
import { Formik, Form, Field } from 'formik'; import { Formik, Form, Field } from 'formik';
import { del } from 'lib/web';
import Button from 'components/common/Button'; import Button from 'components/common/Button';
import FormLayout, { import FormLayout, {
FormButtons, FormButtons,
@ -10,6 +8,7 @@ import FormLayout, {
FormMessage, FormMessage,
FormRow, FormRow,
} from 'components/layout/FormLayout'; } from 'components/layout/FormLayout';
import useDelete from 'hooks/useDelete';
const CONFIRMATION_WORD = 'DELETE'; const CONFIRMATION_WORD = 'DELETE';
@ -28,11 +27,11 @@ const validate = ({ confirmation }) => {
}; };
export default function DeleteForm({ values, onSave, onClose }) { export default function DeleteForm({ values, onSave, onClose }) {
const { basePath } = useRouter(); const del = useDelete();
const [message, setMessage] = useState(); const [message, setMessage] = useState();
const handleSubmit = async ({ type, id }) => { const handleSubmit = async ({ type, id }) => {
const { ok, data } = await del(`${basePath}/api/${type}/${id}`); const { ok, data } = await del(`/api/${type}/${id}`);
if (ok) { if (ok) {
onSave(); onSave();

View File

@ -2,7 +2,6 @@ import React, { useState } from 'react';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import { Formik, Form, Field } from 'formik'; import { Formik, Form, Field } from 'formik';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import { post } from 'lib/web';
import Button from 'components/common/Button'; import Button from 'components/common/Button';
import FormLayout, { import FormLayout, {
FormButtons, FormButtons,
@ -13,6 +12,7 @@ import FormLayout, {
import Icon from 'components/common/Icon'; import Icon from 'components/common/Icon';
import Logo from 'assets/logo.svg'; import Logo from 'assets/logo.svg';
import styles from './LoginForm.module.css'; import styles from './LoginForm.module.css';
import usePost from 'hooks/usePost';
const validate = ({ username, password }) => { const validate = ({ username, password }) => {
const errors = {}; const errors = {};
@ -28,11 +28,12 @@ const validate = ({ username, password }) => {
}; };
export default function LoginForm() { export default function LoginForm() {
const post = usePost();
const router = useRouter(); const router = useRouter();
const [message, setMessage] = useState(); const [message, setMessage] = useState();
const handleSubmit = async ({ username, password }) => { 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, username,
password, password,
}); });
@ -65,8 +66,10 @@ export default function LoginForm() {
> >
{() => ( {() => (
<Form> <Form>
<Icon icon={<Logo />} size="xlarge" className={styles.icon} /> <div className={styles.header}>
<h1 className="center">umami</h1> <Icon icon={<Logo />} size="xlarge" className={styles.icon} />
<h1 className="center">umami</h1>
</div>
<FormRow> <FormRow>
<label htmlFor="username"> <label htmlFor="username">
<FormattedMessage id="label.username" defaultMessage="Username" /> <FormattedMessage id="label.username" defaultMessage="Username" />

View File

@ -13,3 +13,11 @@
justify-content: center; justify-content: center;
margin: 0 auto; margin: 0 auto;
} }
.header {
margin-bottom: 30px;
}
.header h1 {
margin: 12px 0;
}

View File

@ -1,7 +1,6 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
import { Formik, Form, Field } from 'formik'; import { Formik, Form, Field } from 'formik';
import { post } from 'lib/web';
import Button from 'components/common/Button'; import Button from 'components/common/Button';
import FormLayout, { import FormLayout, {
FormButtons, FormButtons,
@ -11,7 +10,7 @@ import FormLayout, {
} from 'components/layout/FormLayout'; } from 'components/layout/FormLayout';
import Checkbox from 'components/common/Checkbox'; import Checkbox from 'components/common/Checkbox';
import { DOMAIN_REGEX } from 'lib/constants'; import { DOMAIN_REGEX } from 'lib/constants';
import { useRouter } from 'next/router'; import usePost from 'hooks/usePost';
const initialValues = { const initialValues = {
name: '', name: '',
@ -35,11 +34,11 @@ const validate = ({ name, domain }) => {
}; };
export default function WebsiteEditForm({ values, onSave, onClose }) { export default function WebsiteEditForm({ values, onSave, onClose }) {
const { basePath } = useRouter(); const post = usePost();
const [message, setMessage] = useState(); const [message, setMessage] = useState();
const handleSubmit = async values => { const handleSubmit = async values => {
const { ok, data } = await post(`${basePath}/api/website`, values); const { ok, data } = await post('/api/website', values);
if (ok) { if (ok) {
onSave(); onSave();

View File

@ -43,19 +43,12 @@ export default function WebsiteDetails({ websiteId }) {
const [eventsData, setEventsData] = useState(); const [eventsData, setEventsData] = useState();
const { const {
resolve, resolve,
router,
query: { view }, query: { view },
} = usePageQuery(); } = usePageQuery();
const BackButton = () => ( const BackButton = () => (
<div key="back-button" className={styles.backButton}> <div key="back-button" className={styles.backButton}>
<Link <Link key="back-button" href={resolve({ view: undefined })} icon={<Arrow />} size="small">
key="back-button"
href={router.pathname}
as={resolve({ view: undefined })}
icon={<Arrow />}
size="small"
>
<FormattedMessage id="label.back" defaultMessage="Back" /> <FormattedMessage id="label.back" defaultMessage="Back" />
</Link> </Link>
</div> </div>

11
hooks/useDelete.js Normal file
View File

@ -0,0 +1,11 @@
import { useCallback } from 'react';
import { useRouter } from 'next/router';
import { del } from 'lib/web';
export default function useDelete() {
const { basePath } = useRouter();
return useCallback(async (url, params, headers) => {
return del(`${basePath}${url}`, params, headers);
}, []);
}

View File

@ -6,8 +6,7 @@ import { useRouter } from 'next/router';
export default function useFetch(url, options = {}, update = []) { export default function useFetch(url, options = {}, update = []) {
const dispatch = useDispatch(); const dispatch = useDispatch();
const [data, setData] = useState(); const [response, setResponse] = useState();
const [status, setStatus] = useState();
const [error, setError] = useState(); const [error, setError] = useState();
const [loading, setLoadiing] = useState(false); const [loading, setLoadiing] = useState(false);
const [count, setCount] = useState(0); const [count, setCount] = useState(0);
@ -19,18 +18,17 @@ export default function useFetch(url, options = {}, update = []) {
setLoadiing(true); setLoadiing(true);
setError(null); setError(null);
const time = performance.now(); const time = performance.now();
const { data, status } = await get(`${basePath}${url}`, params, headers); const { data, status, ok } = await get(`${basePath}${url}`, params, headers);
dispatch(updateQuery({ url, time: performance.now() - time, completed: Date.now() })); dispatch(updateQuery({ url, time: performance.now() - time, completed: Date.now() }));
if (status >= 400) { if (status >= 400) {
setError(data); setError(data);
setData(null); setResponse({ data: null, status, ok });
} else { } else {
setData(data); setResponse({ data, status, ok });
} }
setStatus(status);
onDataLoad?.(data); onDataLoad?.(data);
} catch (e) { } catch (e) {
console.error(e); console.error(e);
@ -60,5 +58,5 @@ export default function useFetch(url, options = {}, update = []) {
} }
}, [interval, !!disabled]); }, [interval, !!disabled]);
return { data, status, error, loading }; return { ...response, error, loading };
} }

View File

@ -1,6 +1,6 @@
import { useMemo } from 'react'; import { useMemo } from 'react';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import { getQueryString } from '../lib/url'; import { getQueryString } from 'lib/url';
export default function usePageQuery() { export default function usePageQuery() {
const router = useRouter(); const router = useRouter();
@ -25,7 +25,9 @@ export default function usePageQuery() {
function resolve(params) { function resolve(params) {
const search = getQueryString({ ...query, ...params }); const search = getQueryString({ ...query, ...params });
return `${pathname}${search}`; const { asPath } = router;
return `${asPath.split('?')[0]}${search}`;
} }
return { pathname, query, resolve, router }; return { pathname, query, resolve, router };

11
hooks/usePost.js Normal file
View File

@ -0,0 +1,11 @@
import { useCallback } from 'react';
import { useRouter } from 'next/router';
import { post } from 'lib/web';
export default function usePost() {
const { basePath } = useRouter();
return useCallback(async (url, params, headers) => {
return post(`${basePath}${url}`, params, headers);
}, []);
}

View File

@ -2,17 +2,7 @@ import { useState, useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux'; import { useDispatch, useSelector } from 'react-redux';
import { updateUser } from 'redux/actions/user'; import { updateUser } from 'redux/actions/user';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import { get } from '../lib/web'; import { get } from 'lib/web';
export async function fetchUser() {
const res = await fetch('/api/auth/verify');
if (!res.ok) {
return null;
}
return await res.json();
}
export default function useRequireLogin() { export default function useRequireLogin() {
const router = useRouter(); const router = useRouter();

View File

@ -1,6 +1,6 @@
{ {
"name": "umami", "name": "umami",
"version": "1.5.0", "version": "1.6.0",
"description": "A simple, fast, website analytics alternative to Google Analytics. ", "description": "A simple, fast, website analytics alternative to Google Analytics. ",
"author": "Mike Cao <mike@mikecao.com>", "author": "Mike Cao <mike@mikecao.com>",
"license": "MIT", "license": "MIT",

View File

@ -1,12 +1,13 @@
import { useEffect } from 'react'; import { useEffect } from 'react';
import { useRouter } from 'next/router'; import { useRouter } from 'next/router';
import { get } from 'lib/web';
export default function LogoutPage() { export default function LogoutPage() {
const router = useRouter(); const router = useRouter();
const { basePath } = router; const { basePath } = router;
useEffect(() => { useEffect(() => {
fetch(`${basePath}/api/auth/logout`).then(() => router.push('/login')); get(`${basePath}/api/auth/logout`).then(() => router.push('/login'));
}, []); }, []);
return null; return null;