CFE-6 notification bug fix

This commit is contained in:
2023-10-30 16:35:08 +03:30
parent 3be39d451c
commit 73ca237817
10 changed files with 102 additions and 36 deletions

View File

@@ -0,0 +1,30 @@
import {createContext, useReducer} from "react";
export const ToastContext = createContext()
const reducer = (state, action) => {
switch (action.type) {
case "PUSH":
return {
...state,
[action.toast_type]: [...state[action.toast_type], action.toast_id]
};
}
};
export const ToastProvider = ({children}) => {
const [state, dispatch] = useReducer(reducer, {
pending: [],
error: [],
warning: [],
success: []
});
console.log(state)
return (
<ToastContext.Provider value={{state, dispatch}}>
{children}
</ToastContext.Provider>
);
};

View File

@@ -5,6 +5,7 @@ import useUser from "@/lib/app/hooks/useUser";
import {errorRequest, errorResponse, errorSetting} from "@/core/utils/errorHandler";
import useNetwork from "@/lib/app/hooks/useNetwork";
import Notifications from "@/core/components/notifications";
import useToast from "@/lib/app/hooks/useToast";
const defaultOptions = {
auth: false, data: {}, requestOptions: {
@@ -23,6 +24,7 @@ const useRequest = (initOptions) => {
const network = useNetwork()
const t = useTranslations()
const {token, clearToken} = useUser()
const {pushToastList} = useToast();
let _options = {...defaultOptions, ...initOptions}
function requestServer(url = '', method = 'get', options) {
@@ -33,13 +35,13 @@ const useRequest = (initOptions) => {
headers: {..._options.requestOptions.headers, authorization: `Bearer ${token}`}
}
}
return new Promise((resolve, reject) => {
if (!network.online) {
reject()
return
}
if (_options.notification && _options.failed.notification.show && _options.pending) Notifications("pending", t)
axios({
url: url, method: method, data: _options.data, ..._options.requestOptions
})

View File

@@ -0,0 +1,22 @@
import {useContext} from "react";
import {ToastContext} from "@/lib/app/contexts/toast";
const useToast = () => {
const {state, dispatch} = useContext(ToastContext);
const pushToastList = (toast_type, toast_id) => {
console.log()
dispatch({type: "PUSH", toast_type, toast_id});
};
const dismissToastList = () => {
dispatch({type: "DISMISS"});
};
return {
pushToastList,
dismissToastList
}
};
export default useToast;