cleaning datatable component and make it more flexible/ debug notification and add pending/ add some extra to datatable.
This commit is contained in:
@@ -1,14 +0,0 @@
|
||||
import {createContext} from "react";
|
||||
import useDataTable from "../hooks/useDatatable";
|
||||
|
||||
export const DataTableContext = createContext();
|
||||
|
||||
export const DataTableProvider = (props) => {
|
||||
const contextValue = useDataTable();
|
||||
|
||||
return (
|
||||
<DataTableContext.Provider value={contextValue}>
|
||||
{props.children}
|
||||
</DataTableContext.Provider>
|
||||
);
|
||||
};
|
||||
@@ -1,15 +0,0 @@
|
||||
import {createContext, useState} from "react";
|
||||
|
||||
export const ReloadDataTableContext = createContext();
|
||||
|
||||
export const ReloadDataTableProvider = ({children}) => {
|
||||
const [reloadDataTable, setReloadDataTable] = useState(false);
|
||||
|
||||
return (
|
||||
<ReloadDataTableContext.Provider
|
||||
value={{reloadDataTable, setReloadDataTable}}
|
||||
>
|
||||
{children}
|
||||
</ReloadDataTableContext.Provider>
|
||||
);
|
||||
};
|
||||
@@ -1,113 +0,0 @@
|
||||
import axios from "axios";
|
||||
import {useCallback, useContext, useReducer, useState} from "react";
|
||||
import {ReloadDataTableContext} from "../contexts/ReloadDatatableContext";
|
||||
import useUser from "./useUser";
|
||||
import Notifications from "@/core/components/notifications";
|
||||
import useDirection from "./useDirection";
|
||||
import {useTranslations} from "next-intl";
|
||||
|
||||
const initialValues = {
|
||||
url: "",
|
||||
rowID: "",
|
||||
values: {},
|
||||
};
|
||||
|
||||
const reducer = (state, action) => {
|
||||
switch (action.type) {
|
||||
case "CONFIRM_DATA":
|
||||
return {
|
||||
...state,
|
||||
url: action.url,
|
||||
rowID: action.rowID,
|
||||
};
|
||||
case "REJECT_DATA":
|
||||
return {
|
||||
...state,
|
||||
url: action.url,
|
||||
rowID: action.rowID,
|
||||
values: {},
|
||||
};
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
const useDataTable = () => {
|
||||
const {setReloadDataTable} = useContext(ReloadDataTableContext);
|
||||
const [state, dispatch] = useReducer(reducer, initialValues);
|
||||
const [openConfirmDialog, setOpenConfirmDialog] = useState(false);
|
||||
const [openRejectDialog, setOpenRejectDialog] = useState(false);
|
||||
const [rowId, setRowId] = useState(null);
|
||||
const {directionApp} = useDirection();
|
||||
const t = useTranslations();
|
||||
|
||||
const {token} = useUser();
|
||||
|
||||
const confirmData = useCallback(async (url, rowID, values) => {
|
||||
dispatch({type: "CONFIRM_DATA", url: url, rowID: rowID, values: values});
|
||||
axios
|
||||
.post(`${url}/${rowID}`, values, {
|
||||
headers: {authorization: `Bearer ${token}`},
|
||||
})
|
||||
.then((response) => {
|
||||
setReloadDataTable(true);
|
||||
Notifications(directionApp, response, t);
|
||||
})
|
||||
.catch((error) => {
|
||||
Notifications(directionApp, error.response, t);
|
||||
});
|
||||
}, []);
|
||||
|
||||
const rejectData = useCallback(async (url, rowID, values) => {
|
||||
dispatch({type: "REJECT_DATA", url: url, rowID: rowID, values: values});
|
||||
axios
|
||||
.post(`${url}/${rowID}`, values, {
|
||||
headers: {authorization: `Bearer ${token}`},
|
||||
})
|
||||
.then((response) => {
|
||||
setReloadDataTable(true);
|
||||
Notifications(directionApp, response, t);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log(error);
|
||||
Notifications(directionApp, error.response, t);
|
||||
});
|
||||
}, []);
|
||||
|
||||
//Confirm Data
|
||||
const handleOpenConfirmDialog = (row) => {
|
||||
setRowId(row.getValue("id"));
|
||||
setOpenConfirmDialog(true);
|
||||
};
|
||||
|
||||
const handleCloseConfirmDialog = () => {
|
||||
setOpenConfirmDialog(false);
|
||||
};
|
||||
//End Confirm Data
|
||||
|
||||
// Reject Data
|
||||
const handleOpenRejectDialog = (row) => {
|
||||
setRowId(row.getValue("id"));
|
||||
setOpenRejectDialog(true);
|
||||
};
|
||||
|
||||
const handleCloseRejectDialog = () => {
|
||||
setOpenRejectDialog(false);
|
||||
};
|
||||
// End Reject Data
|
||||
|
||||
const contextValue = {
|
||||
handleOpenConfirmDialog,
|
||||
handleCloseConfirmDialog,
|
||||
handleOpenRejectDialog,
|
||||
handleCloseRejectDialog,
|
||||
rowId,
|
||||
openConfirmDialog,
|
||||
openRejectDialog,
|
||||
rejectData,
|
||||
confirmData,
|
||||
};
|
||||
return contextValue;
|
||||
};
|
||||
|
||||
export default useDataTable;
|
||||
Reference in New Issue
Block a user