Added endpoint for fetching server-side variables.

This commit is contained in:
Mike Cao 2022-08-01 23:04:47 -07:00
parent 68d35c0fc4
commit 50e491af06
8 changed files with 62 additions and 15 deletions

View file

@ -6,7 +6,7 @@ import useStore from 'store/app';
const selector = state => state.shareToken;
function parseHeaders(headers = {}, { authToken, shareToken }) {
function parseHeaders(headers, { authToken, shareToken }) {
if (authToken) {
headers.authorization = `Bearer ${authToken}`;
}
@ -25,7 +25,7 @@ export default function useApi() {
return {
get: useCallback(
async (url, params, headers) => {
async (url, params = {}, headers = {}) => {
return get(
`${basePath}/api${url}`,
params,
@ -36,7 +36,7 @@ export default function useApi() {
),
post: useCallback(
async (url, params, headers) => {
async (url, params = {}, headers = {}) => {
return post(
`${basePath}/api${url}`,
params,
@ -47,7 +47,7 @@ export default function useApi() {
),
put: useCallback(
async (url, params, headers) => {
async (url, params = {}, headers = {}) => {
return put(
`${basePath}/api${url}`,
params,
@ -58,7 +58,7 @@ export default function useApi() {
),
del: useCallback(
async (url, params, headers) => {
async (url, params = {}, headers = {}) => {
return del(
`${basePath}/api${url}`,
params,

24
hooks/useConfig.js Normal file
View file

@ -0,0 +1,24 @@
import { useEffect } from 'react';
import useStore, { setConfig } from 'store/app';
import useApi from 'hooks/useApi';
let fetched = false;
export default function useConfig() {
const { config } = useStore();
const { get } = useApi();
async function loadConfig() {
const { data } = await get('/config');
setConfig(data);
}
useEffect(() => {
if (!config && !fetched) {
fetched = true;
loadConfig();
}
}, []);
return config || {};
}