2023-02-22 21:30:48 -05:00
|
|
|
/* eslint-disable react-hooks/rules-of-hooks */
|
2023-02-07 16:22:35 -05:00
|
|
|
import axios from 'axios';
|
|
|
|
|
import useSWR from 'swr';
|
|
|
|
|
import useSWRMutation from 'swr/mutation';
|
|
|
|
|
|
|
|
|
|
const fetcher = (url) => fetch(url).then((res) => res.json());
|
|
|
|
|
|
|
|
|
|
const postRequest = async (url, { arg }) => {
|
|
|
|
|
return await axios.post(url, { arg });
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-15 04:05:14 +08:00
|
|
|
export const swr = (path, successCallback, options) => {
|
|
|
|
|
const _options = {...options};
|
2023-02-13 23:14:35 -05:00
|
|
|
|
|
|
|
|
if (successCallback) {
|
2023-03-15 04:05:14 +08:00
|
|
|
_options.onSuccess = successCallback;
|
2023-02-13 23:14:35 -05:00
|
|
|
}
|
|
|
|
|
|
2023-03-15 04:05:14 +08:00
|
|
|
return useSWR(path, fetcher, _options);
|
2023-02-13 23:14:35 -05:00
|
|
|
}
|
2023-02-07 16:22:35 -05:00
|
|
|
|
|
|
|
|
export default function manualSWR(path, type, successCallback) {
|
2023-02-07 19:07:48 -05:00
|
|
|
const options = {};
|
|
|
|
|
|
|
|
|
|
if (successCallback) {
|
|
|
|
|
options.onSuccess = successCallback;
|
|
|
|
|
}
|
2023-02-07 16:22:35 -05:00
|
|
|
const fetchFunction = type === 'get' ? fetcher : postRequest;
|
2023-02-07 19:07:48 -05:00
|
|
|
return useSWRMutation(path, fetchFunction, options);
|
2023-03-04 17:39:06 -05:00
|
|
|
}
|