working on faaliat information
This commit is contained in:
@@ -18,14 +18,23 @@ const DataTableProvider = ({ children, user_id, page_name, table_name, columns,
|
||||
const [initHide, setInitHide] = useState({});
|
||||
const [hideData, setHideData] = useState({});
|
||||
const [isDirty, setIsDirty] = useState(false);
|
||||
const [isAnyDirty, setIsAnyDirty] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
let hasAnyConflict = false;
|
||||
const hasConflict =
|
||||
JSON.stringify(hideData) !== JSON.stringify(initHide) ||
|
||||
JSON.stringify(sortData) !== JSON.stringify(initSort) ||
|
||||
JSON.stringify(filterData) !== JSON.stringify(initFilter);
|
||||
|
||||
const CheckFilterValues = Object.values(filterData).every((innerObject) => innerObject.value === "");
|
||||
const CheckHideValues = Object.values(hideData).every((value) => value === true);
|
||||
|
||||
hasAnyConflict =
|
||||
!CheckHideValues || JSON.stringify(sortData) !== JSON.stringify(initialSort) || !CheckFilterValues;
|
||||
|
||||
setIsDirty(hasConflict);
|
||||
setIsAnyDirty(hasAnyConflict);
|
||||
}, [hideData, sortData, filterData, initHide, initSort, initFilter]);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -136,7 +145,7 @@ const DataTableProvider = ({ children, user_id, page_name, table_name, columns,
|
||||
|
||||
return (
|
||||
<DataTableContext.Provider
|
||||
value={{ filterData, setFilterData, sortData, setSortData, hideData, setHideData, isDirty }}
|
||||
value={{ filterData, setFilterData, sortData, setSortData, hideData, setHideData, isDirty, isAnyDirty }}
|
||||
>
|
||||
{children}
|
||||
</DataTableContext.Provider>
|
||||
|
||||
@@ -2,9 +2,9 @@ import { useContext } from "react";
|
||||
import { DataTableContext } from "@/lib/contexts/DataTable";
|
||||
|
||||
const useTableSetting = () => {
|
||||
const { filterData, setFilterData, sortData, setSortData, hideData, setHideData, isDirty } =
|
||||
const { filterData, setFilterData, sortData, setSortData, hideData, setHideData, isDirty, isAnyDirty } =
|
||||
useContext(DataTableContext);
|
||||
return { filterData, setFilterData, sortData, setSortData, hideData, setHideData, isDirty };
|
||||
return { filterData, setFilterData, sortData, setSortData, hideData, setHideData, isDirty, isAnyDirty };
|
||||
};
|
||||
|
||||
export default useTableSetting;
|
||||
|
||||
62
src/lib/hooks/useEdaratLists.js
Normal file
62
src/lib/hooks/useEdaratLists.js
Normal file
@@ -0,0 +1,62 @@
|
||||
import { useEffect, useReducer } from "react";
|
||||
import { GET_EDARAT_LISTS } from "@/core/utils/routes";
|
||||
import useRequest from "@/lib/hooks/useRequest";
|
||||
|
||||
const ACTIONS = {
|
||||
LOADING: "loading",
|
||||
SUCCESS: "success",
|
||||
ERROR: "error",
|
||||
RESET: "reset",
|
||||
};
|
||||
|
||||
const initialState = {
|
||||
data: [],
|
||||
loading: true,
|
||||
error: null,
|
||||
};
|
||||
|
||||
function reducer(state, action) {
|
||||
switch (action.type) {
|
||||
case ACTIONS.LOADING:
|
||||
return { ...state, loading: true, error: null };
|
||||
case ACTIONS.SUCCESS:
|
||||
return { ...state, loading: false, data: action.payload };
|
||||
case ACTIONS.ERROR:
|
||||
return { ...state, loading: false, error: action.payload };
|
||||
case ACTIONS.RESET:
|
||||
return { ...initialState, loading: false };
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
const useEdaratLists = (id) => {
|
||||
const requestServer = useRequest({ notificationShow: false });
|
||||
const [state, dispatch] = useReducer(reducer, initialState);
|
||||
useEffect(() => {
|
||||
if (!id) {
|
||||
dispatch({ type: ACTIONS.RESET });
|
||||
return;
|
||||
}
|
||||
|
||||
const fetchEdaratLists = async () => {
|
||||
dispatch({ type: ACTIONS.LOADING });
|
||||
try {
|
||||
const response = await requestServer(`${GET_EDARAT_LISTS}/${id}`);
|
||||
dispatch({ type: ACTIONS.SUCCESS, payload: response.data.data });
|
||||
} catch (error) {
|
||||
dispatch({ type: ACTIONS.ERROR, payload: error });
|
||||
}
|
||||
};
|
||||
|
||||
fetchEdaratLists();
|
||||
}, [id]);
|
||||
|
||||
return {
|
||||
edaratList: state.data,
|
||||
loadingEdaratList: state.loading,
|
||||
errorEdaratList: state.error,
|
||||
};
|
||||
};
|
||||
|
||||
export default useEdaratLists;
|
||||
@@ -10,7 +10,7 @@ const defaultOptions = {
|
||||
},
|
||||
notificationShow: true,
|
||||
notificationSuccess: false,
|
||||
notificationFailed: true
|
||||
notificationFailed: true,
|
||||
};
|
||||
|
||||
const useRequest = (initOptions) => {
|
||||
|
||||
@@ -1,29 +1,62 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { useEffect, useReducer } from "react";
|
||||
import { GET_ROAD_ITEMS_SUB_ITEM } from "@/core/utils/routes";
|
||||
import useRequest from "@/lib/hooks/useRequest";
|
||||
|
||||
const useRoadItemGetItems = (id) => {
|
||||
const requestServer = useRequest();
|
||||
const [subItemsList, setSubItemsList] = useState([]);
|
||||
const [loadingSubItemsList, setLoadingSubItemsList] = useState(true);
|
||||
const [errorSubItemsList, setErrorSubItemsList] = useState(null);
|
||||
const ACTIONS = {
|
||||
LOADING: "loading",
|
||||
SUCCESS: "success",
|
||||
ERROR: "error",
|
||||
RESET: "reset",
|
||||
};
|
||||
|
||||
const initialState = {
|
||||
data: [],
|
||||
loading: true,
|
||||
error: null,
|
||||
};
|
||||
|
||||
function reducer(state, action) {
|
||||
switch (action.type) {
|
||||
case ACTIONS.LOADING:
|
||||
return { ...state, loading: true, error: null };
|
||||
case ACTIONS.SUCCESS:
|
||||
return { ...state, loading: false, data: action.payload };
|
||||
case ACTIONS.ERROR:
|
||||
return { ...state, loading: false, error: action.payload };
|
||||
case ACTIONS.RESET:
|
||||
return { ...initialState, loading: false };
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
const useRoadItemGetItems = (id) => {
|
||||
const requestServer = useRequest({ notificationShow: false });
|
||||
const [state, dispatch] = useReducer(reducer, initialState);
|
||||
useEffect(() => {
|
||||
if (!id) {
|
||||
dispatch({ type: ACTIONS.RESET });
|
||||
return;
|
||||
}
|
||||
|
||||
const fetchSubItems = async () => {
|
||||
dispatch({ type: ACTIONS.LOADING });
|
||||
try {
|
||||
const response = await requestServer(`${GET_ROAD_ITEMS_SUB_ITEM}/${id}`);
|
||||
setSubItemsList(response.data.data);
|
||||
setLoadingSubItemsList(false);
|
||||
} catch (e) {
|
||||
setErrorSubItemsList(e);
|
||||
setLoadingSubItemsList(false);
|
||||
dispatch({ type: ACTIONS.SUCCESS, payload: response.data.data });
|
||||
} catch (error) {
|
||||
dispatch({ type: ACTIONS.ERROR, payload: error });
|
||||
}
|
||||
};
|
||||
|
||||
fetchSubItems();
|
||||
}, []);
|
||||
}, [id]);
|
||||
|
||||
return { subItemsList, loadingSubItemsList, errorSubItemsList };
|
||||
return {
|
||||
subItemsList: state.data,
|
||||
loadingSubItemsList: state.loading,
|
||||
errorSubItemsList: state.error,
|
||||
};
|
||||
};
|
||||
|
||||
export default useRoadItemGetItems;
|
||||
|
||||
@@ -14,6 +14,7 @@ const useRoadItemGetItems = () => {
|
||||
const response = await requestServer(`${GET_ROAD_ITEMS_ITEM}`);
|
||||
setItemsList(response.data.data);
|
||||
setLoadingItemsList(false);
|
||||
setErrorItemsList(false);
|
||||
} catch (e) {
|
||||
setErrorItemsList(e);
|
||||
setLoadingItemsList(false);
|
||||
|
||||
Reference in New Issue
Block a user