add sidebar update in action

This commit is contained in:
AmirHossein Mahmoodi
2025-04-13 13:16:02 +03:30
parent 33050156be
commit 665d6b8c94
19 changed files with 43 additions and 17 deletions

View File

@@ -2,6 +2,7 @@ import { errorResponse } from "@/core/utils/errorResponse";
import { successRequest } from "@/core/utils/successRequest";
import axios from "axios";
import { useAuth } from "../contexts/auth";
import { useSidebarBadge } from "./useSidebarBadge";
const defaultOptions = {
data: {},
@@ -11,13 +12,22 @@ const defaultOptions = {
notificationShow: true,
notificationSuccess: false,
notificationFailed: true,
hasSidebarUpdate: false,
};
const useRequest = (initOptions) => {
const { mutate: sidebarUpdate } = useSidebarBadge();
const { logout } = useAuth();
const _options = Object.assign({}, defaultOptions, initOptions);
/**
* Performs an HTTP request.
* @param {string} url - The URL to send the request to.
* @param {'get' | 'post' | 'put' | 'delete'} [method='get'] - HTTP method.
* @param {object} [options] - Additional request options.
* @returns {Promise<AxiosResponse>} - Axios response.
*/
return async (url = "", method = "get", options) => {
const mergedOptions = Object.assign({}, _options, options);
try {
@@ -28,6 +38,9 @@ const useRequest = (initOptions) => {
withCredentials: true,
...mergedOptions.requestOptions,
});
if (mergedOptions.hasSidebarUpdate) {
if (method === "post" || method === "put" || method === "delete") sidebarUpdate();
}
successRequest(mergedOptions.notificationShow && mergedOptions.notificationSuccess, "request_data");
return response;
} catch (error) {

View File

@@ -1,18 +1,16 @@
import { GET_SIDEBAR_BADGE_ROUTE } from "@/core/utils/routes";
import axios from "axios";
import useSWR from "swr";
import useRequest from "./useRequest";
export const useSidebarBadge = () => {
const request = useRequest();
const fetcher = async (url) => {
try {
const response = await request(url);
const response = await axios({ url, method: "get", withCredentials: true });
return response.data.data;
} catch (error) {
throw new Error();
}
};
return useSWR(GET_SIDEBAR_BADGE_ROUTE, fetcher, { keepPreviousData: true, dedupingInterval: 10000 });
return useSWR(GET_SIDEBAR_BADGE_ROUTE, fetcher, { keepPreviousData: true, dedupingInterval: 30000 });
};