mirror of
https://github.com/kremalicious/umami.git
synced 2025-01-02 10:13:11 +01:00
Updated api fetch to return an object.
This commit is contained in:
parent
e64b35f701
commit
e5cd162b83
@ -32,15 +32,13 @@ export default function AccountEditForm({ values, onSave, onClose }) {
|
||||
const [message, setMessage] = useState();
|
||||
|
||||
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();
|
||||
} else {
|
||||
setMessage(
|
||||
response || (
|
||||
<FormattedMessage id="message.failure" defaultMessage="Something went wrong." />
|
||||
),
|
||||
data || <FormattedMessage id="message.failure" defaultMessage="Something went wrong." />,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
@ -40,15 +40,13 @@ export default function ChangePasswordForm({ values, onSave, onClose }) {
|
||||
const [message, setMessage] = useState();
|
||||
|
||||
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();
|
||||
} else {
|
||||
setMessage(
|
||||
response || (
|
||||
<FormattedMessage id="message.failure" defaultMessage="Something went wrong." />
|
||||
),
|
||||
data || <FormattedMessage id="message.failure" defaultMessage="Something went wrong." />,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
@ -31,19 +31,19 @@ export default function LoginForm() {
|
||||
const [message, setMessage] = useState();
|
||||
|
||||
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('/');
|
||||
} else {
|
||||
setMessage(
|
||||
response.startsWith('401') ? (
|
||||
status === 401 ? (
|
||||
<FormattedMessage
|
||||
id="message.incorrect-username-password"
|
||||
defaultMessage="Incorrect username/password."
|
||||
/>
|
||||
) : (
|
||||
response
|
||||
data
|
||||
),
|
||||
);
|
||||
}
|
||||
|
@ -37,12 +37,14 @@ export default function WebsiteEditForm({ values, onSave, onClose }) {
|
||||
const [message, setMessage] = useState();
|
||||
|
||||
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();
|
||||
} else {
|
||||
setMessage(<FormattedMessage id="message.failure" defaultMessage="Something went wrong." />);
|
||||
setMessage(
|
||||
data || <FormattedMessage id="message.failure" defaultMessage="Something went wrong." />,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -6,6 +6,7 @@ import { updateQuery } from 'redux/actions/queries';
|
||||
export default function useFetch(url, params = {}, options = {}) {
|
||||
const dispatch = useDispatch();
|
||||
const [data, setData] = useState();
|
||||
const [status, setStatus] = useState();
|
||||
const [error, setError] = useState();
|
||||
const [loading, setLoadiing] = useState(false);
|
||||
const keys = Object.keys(params)
|
||||
@ -18,11 +19,12 @@ export default function useFetch(url, params = {}, options = {}) {
|
||||
setLoadiing(true);
|
||||
setError(null);
|
||||
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() }));
|
||||
|
||||
setData(data);
|
||||
setStatus(status);
|
||||
onDataLoad(data);
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
@ -46,5 +48,5 @@ export default function useFetch(url, params = {}, options = {}) {
|
||||
}
|
||||
}, [url, ...keys, ...update]);
|
||||
|
||||
return { data, error, loading, loadData };
|
||||
return { data, status, error, loading };
|
||||
}
|
||||
|
12
lib/web.js
12
lib/web.js
@ -1,6 +1,6 @@
|
||||
import { getQueryString } from './url';
|
||||
|
||||
export const apiRequest = (method, url, body) =>
|
||||
export const apiRequest = (method, url, body, headers) =>
|
||||
fetch(url, {
|
||||
method,
|
||||
cache: 'no-cache',
|
||||
@ -8,18 +8,16 @@ export const apiRequest = (method, url, body) =>
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
...headers,
|
||||
},
|
||||
body,
|
||||
}).then(res => {
|
||||
console.log({ res });
|
||||
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();
|
||||
}
|
||||
|
||||
return null;
|
||||
return res.text().then(data => ({ ok: res.ok, status: res.status, res: res, data }));
|
||||
});
|
||||
|
||||
export const get = (url, params) => apiRequest('get', `${url}${getQueryString(params)}`);
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "umami",
|
||||
"version": "0.62.0",
|
||||
"version": "0.63.0",
|
||||
"description": "A simple, fast, website analytics alternative to Google Analytics. ",
|
||||
"author": "Mike Cao <mike@mikecao.com>",
|
||||
"license": "MIT",
|
||||
|
1
public/country/ja-JP.json
Normal file
1
public/country/ja-JP.json
Normal file
File diff suppressed because one or more lines are too long
1
public/country/zh-CN.json
Normal file
1
public/country/zh-CN.json
Normal file
File diff suppressed because one or more lines are too long
@ -9,5 +9,5 @@ export default {
|
||||
file: 'public/umami.js',
|
||||
format: 'iife',
|
||||
},
|
||||
plugins: [resolve(), buble(), terser({ compress: { evaluate: false } })],
|
||||
plugins: [resolve(), buble({ objectAssign: true }), terser({ compress: { evaluate: false } })],
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user