Updated api fetch to return an object.

This commit is contained in:
Mike Cao 2020-09-30 15:14:44 -07:00
parent e64b35f701
commit e5cd162b83
10 changed files with 28 additions and 28 deletions

View File

@ -32,15 +32,13 @@ export default function AccountEditForm({ values, onSave, onClose }) {
const [message, setMessage] = useState(); const [message, setMessage] = useState();
const handleSubmit = async values => { const handleSubmit = async values => {
const response = await post(`/api/account`, values); const { ok, data } = await post(`/api/account`, values);
if (typeof response !== 'string') { if (ok) {
onSave(); onSave();
} else { } else {
setMessage( setMessage(
response || ( data || <FormattedMessage id="message.failure" defaultMessage="Something went wrong." />,
<FormattedMessage id="message.failure" defaultMessage="Something went wrong." />
),
); );
} }
}; };

View File

@ -40,15 +40,13 @@ export default function ChangePasswordForm({ values, onSave, onClose }) {
const [message, setMessage] = useState(); const [message, setMessage] = useState();
const handleSubmit = async values => { const handleSubmit = async values => {
const response = await post(`/api/account/password`, values); const { ok, data } = await post(`/api/account/password`, values);
if (typeof response !== 'string') { if (ok) {
onSave(); onSave();
} else { } else {
setMessage( setMessage(
response || ( data || <FormattedMessage id="message.failure" defaultMessage="Something went wrong." />,
<FormattedMessage id="message.failure" defaultMessage="Something went wrong." />
),
); );
} }
}; };

View File

@ -31,19 +31,19 @@ export default function LoginForm() {
const [message, setMessage] = useState(); const [message, setMessage] = useState();
const handleSubmit = async ({ username, password }) => { const handleSubmit = async ({ username, password }) => {
const response = await post('/api/auth/login', { username, password }); const { ok, status, data } = await post('/api/auth/login', { username, password });
if (typeof response !== 'string') { if (ok) {
await Router.push('/'); await Router.push('/');
} else { } else {
setMessage( setMessage(
response.startsWith('401') ? ( status === 401 ? (
<FormattedMessage <FormattedMessage
id="message.incorrect-username-password" id="message.incorrect-username-password"
defaultMessage="Incorrect username/password." defaultMessage="Incorrect username/password."
/> />
) : ( ) : (
response data
), ),
); );
} }

View File

@ -37,12 +37,14 @@ export default function WebsiteEditForm({ values, onSave, onClose }) {
const [message, setMessage] = useState(); const [message, setMessage] = useState();
const handleSubmit = async values => { const handleSubmit = async values => {
const response = await post(`/api/website`, values); const { ok, data } = await post(`/api/website`, values);
if (typeof response !== 'string') { if (ok) {
onSave(); onSave();
} else { } else {
setMessage(<FormattedMessage id="message.failure" defaultMessage="Something went wrong." />); setMessage(
data || <FormattedMessage id="message.failure" defaultMessage="Something went wrong." />,
);
} }
}; };

View File

@ -6,6 +6,7 @@ import { updateQuery } from 'redux/actions/queries';
export default function useFetch(url, params = {}, options = {}) { export default function useFetch(url, params = {}, options = {}) {
const dispatch = useDispatch(); const dispatch = useDispatch();
const [data, setData] = useState(); const [data, setData] = useState();
const [status, setStatus] = useState();
const [error, setError] = useState(); const [error, setError] = useState();
const [loading, setLoadiing] = useState(false); const [loading, setLoadiing] = useState(false);
const keys = Object.keys(params) const keys = Object.keys(params)
@ -18,11 +19,12 @@ export default function useFetch(url, params = {}, options = {}) {
setLoadiing(true); setLoadiing(true);
setError(null); setError(null);
const time = performance.now(); const time = performance.now();
const data = await get(url, params); const { data, status } = await get(url, params);
dispatch(updateQuery({ url, time: performance.now() - time, completed: Date.now() })); dispatch(updateQuery({ url, time: performance.now() - time, completed: Date.now() }));
setData(data); setData(data);
setStatus(status);
onDataLoad(data); onDataLoad(data);
} catch (e) { } catch (e) {
console.error(e); console.error(e);
@ -46,5 +48,5 @@ export default function useFetch(url, params = {}, options = {}) {
} }
}, [url, ...keys, ...update]); }, [url, ...keys, ...update]);
return { data, error, loading, loadData }; return { data, status, error, loading };
} }

View File

@ -1,6 +1,6 @@
import { getQueryString } from './url'; import { getQueryString } from './url';
export const apiRequest = (method, url, body) => export const apiRequest = (method, url, body, headers) =>
fetch(url, { fetch(url, {
method, method,
cache: 'no-cache', cache: 'no-cache',
@ -8,18 +8,16 @@ export const apiRequest = (method, url, body) =>
headers: { headers: {
Accept: 'application/json', Accept: 'application/json',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
...headers,
}, },
body, body,
}).then(res => { }).then(res => {
console.log({ res });
if (res.ok) { if (res.ok) {
return res.json(); return res.json().then(data => ({ ok: res.ok, status: res.status, data }));
} }
if (['post', 'put', 'delete'].includes(method)) { return res.text().then(data => ({ ok: res.ok, status: res.status, res: res, data }));
return res.text();
}
return null;
}); });
export const get = (url, params) => apiRequest('get', `${url}${getQueryString(params)}`); export const get = (url, params) => apiRequest('get', `${url}${getQueryString(params)}`);

View File

@ -1,6 +1,6 @@
{ {
"name": "umami", "name": "umami",
"version": "0.62.0", "version": "0.63.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",

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -9,5 +9,5 @@ export default {
file: 'public/umami.js', file: 'public/umami.js',
format: 'iife', format: 'iife',
}, },
plugins: [resolve(), buble(), terser({ compress: { evaluate: false } })], plugins: [resolve(), buble({ objectAssign: true }), terser({ compress: { evaluate: false } })],
}; };