Move auth token logic to useApi hook.

This commit is contained in:
Mike Cao 2022-02-27 00:25:24 -08:00
parent d552a01701
commit be98500e82
2 changed files with 7 additions and 7 deletions

View File

@ -36,7 +36,7 @@ export default function WebsiteSettings() {
const [showUrl, setShowUrl] = useState();
const [saved, setSaved] = useState(0);
const [message, setMessage] = useState();
const { data } = useFetch(`/websites` + (user?.is_admin ? '?include_all=true' : ''), {}, [saved]);
const { data } = useFetch('/websites', { params: { include_all: !!user?.is_admin } }, [saved]);
const Buttons = row => (
<ButtonLayout align="right">

View File

@ -3,7 +3,7 @@ import { useRouter } from 'next/router';
import { get, post, put, del, getItem } from 'lib/web';
import { AUTH_TOKEN } from 'lib/constants';
function includeAuthToken(headers) {
function includeAuthToken(headers = {}) {
const authToken = getItem(AUTH_TOKEN);
if (authToken) {
@ -18,29 +18,29 @@ export default function useApi() {
return {
get: useCallback(
async (url, params, headers = {}) => {
return get(`${basePath}/api/${url}`, params, includeAuthToken(headers));
async (url, params, headers) => {
return get(`${basePath}/api${url}`, params, includeAuthToken(headers));
},
[get],
),
post: useCallback(
async (url, params, headers) => {
return post(`${basePath}/api/${url}`, params, includeAuthToken(headers));
return post(`${basePath}/api${url}`, params, includeAuthToken(headers));
},
[post],
),
put: useCallback(
async (url, params, headers) => {
return put(`${basePath}/api/${url}`, params, includeAuthToken(headers));
return put(`${basePath}/api${url}`, params, includeAuthToken(headers));
},
[put],
),
del: useCallback(
async (url, params, headers) => {
return del(`${basePath}/api/${url}`, params, includeAuthToken(headers));
return del(`${basePath}/api${url}`, params, includeAuthToken(headers));
},
[del],
),