Browse Source

Updated api fetch to return an object.

master
Mike Cao 11 months ago
parent
commit
e5cd162b83
10 changed files with 28 additions and 28 deletions
  1. +3
    -5
      components/forms/AccountEditForm.js
  2. +3
    -5
      components/forms/ChangePasswordForm.js
  3. +4
    -4
      components/forms/LoginForm.js
  4. +5
    -3
      components/forms/WebsiteEditForm.js
  5. +4
    -2
      hooks/useFetch.js
  6. +5
    -7
      lib/web.js
  7. +1
    -1
      package.json
  8. +1
    -0
      public/country/ja-JP.json
  9. +1
    -0
      public/country/zh-CN.json
  10. +1
    -1
      rollup.tracker.config.js

+ 3
- 5
components/forms/AccountEditForm.js View File

@ -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." />,
);
}
};


+ 3
- 5
components/forms/ChangePasswordForm.js View File

@ -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." />,
);
}
};


+ 4
- 4
components/forms/LoginForm.js View File

@ -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
),
);
}


+ 5
- 3
components/forms/WebsiteEditForm.js View File

@ -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." />,
);
}
};


+ 4
- 2
hooks/useFetch.js View File

@ -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 };
}

+ 5
- 7
lib/web.js View File

@ -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
- 1
package.json View File

@ -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
- 0
public/country/ja-JP.json
File diff suppressed because it is too large
View File


+ 1
- 0
public/country/zh-CN.json
File diff suppressed because it is too large
View File


+ 1
- 1
rollup.tracker.config.js View File

@ -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…
Cancel
Save