merge and resolve conflict
This commit is contained in:
11
src/lib/contexts/DataTable.js
Normal file
11
src/lib/contexts/DataTable.js
Normal file
@@ -0,0 +1,11 @@
|
||||
import { createContext, useState } from "react";
|
||||
|
||||
export const DataTableContext = createContext();
|
||||
|
||||
const DataTableProvider = ({ children }) => {
|
||||
const [filterData, setFilterData] = useState({});
|
||||
|
||||
return <DataTableContext.Provider value={{ filterData, setFilterData }}>{children}</DataTableContext.Provider>;
|
||||
};
|
||||
|
||||
export default DataTableProvider;
|
||||
82
src/lib/contexts/auth.js
Normal file
82
src/lib/contexts/auth.js
Normal file
@@ -0,0 +1,82 @@
|
||||
"use client";
|
||||
import { createContext, useCallback, useContext, useEffect, useReducer } from "react";
|
||||
import useRequest from "../hooks/useRequest";
|
||||
import { GET_USER_ROUTE } from "@/core/utils/routes";
|
||||
|
||||
const initAuth = {
|
||||
initAuthState: false,
|
||||
isAuth: false,
|
||||
user: {},
|
||||
};
|
||||
|
||||
const authReducer = (state, action) => {
|
||||
switch (action.type) {
|
||||
case "CLEAR_USER":
|
||||
return { ...state, user: {} };
|
||||
case "CHANGE_USER":
|
||||
return { ...state, user: action.user };
|
||||
case "CHANGE_AUTH_STATE":
|
||||
return { ...state, isAuth: action.isAuth };
|
||||
case "CHANGE_INIT_AUTH":
|
||||
return { ...state, initAuthState: action.initAuthState };
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
};
|
||||
|
||||
const AuthContext = createContext();
|
||||
|
||||
export const AuthProvider = ({ children }) => {
|
||||
const [state, dispatch] = useReducer(authReducer, initAuth);
|
||||
const request = useRequest();
|
||||
|
||||
const clearUser = useCallback(() => {
|
||||
dispatch({ type: "CLEAR_USER" });
|
||||
}, []);
|
||||
|
||||
const changeUser = useCallback((user) => {
|
||||
dispatch({ type: "CHANGE_USER", user });
|
||||
}, []);
|
||||
|
||||
const changeAuthState = useCallback((isAuth) => {
|
||||
dispatch({ type: "CHANGE_AUTH_STATE", isAuth });
|
||||
}, []);
|
||||
|
||||
const changeInitAuth = useCallback((initAuthState) => {
|
||||
dispatch({ type: "CHANGE_INIT_AUTH", initAuthState });
|
||||
}, []);
|
||||
|
||||
const getUser = useCallback(async () => {
|
||||
try {
|
||||
const { data } = await request(GET_USER_ROUTE, "get");
|
||||
changeUser(data);
|
||||
changeAuthState(true);
|
||||
changeInitAuth(true);
|
||||
} catch (error) {
|
||||
if (error.response && error.response.status === 401) {
|
||||
clearUser();
|
||||
changeAuthState(false);
|
||||
changeInitAuth(true);
|
||||
}
|
||||
}
|
||||
}, [clearUser, changeUser, changeAuthState, changeInitAuth]);
|
||||
|
||||
useEffect(() => {
|
||||
getUser();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<AuthContext.Provider
|
||||
value={{
|
||||
user: state.user,
|
||||
isAuth: state.isAuth,
|
||||
initAuthState: state.initAuthState,
|
||||
getUser,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
};
|
||||
|
||||
export const useAuth = () => useContext(AuthContext);
|
||||
9
src/lib/hooks/useDataTable.js
Normal file
9
src/lib/hooks/useDataTable.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import { useContext } from "react";
|
||||
import { DataTableContext } from "@/lib/contexts/DataTable";
|
||||
|
||||
const useTableSetting = () => {
|
||||
const { filterData, setFilterData } = useContext(DataTableContext);
|
||||
return { filterData, setFilterData };
|
||||
};
|
||||
|
||||
export default useTableSetting;
|
||||
18
src/lib/hooks/usePermissions.js
Normal file
18
src/lib/hooks/usePermissions.js
Normal file
@@ -0,0 +1,18 @@
|
||||
import { GET_PERMISSIONS_ROUTE } from "@/core/utils/routes";
|
||||
import useSWR from "swr";
|
||||
import useRequest from "./useRequest";
|
||||
|
||||
export const usePermissions = () => {
|
||||
const request = useRequest()
|
||||
|
||||
const fetcher = async (url) => {
|
||||
try {
|
||||
const response = await request(url)
|
||||
return response.data.data
|
||||
} catch (error) {
|
||||
throw new Error();
|
||||
}
|
||||
};
|
||||
|
||||
return useSWR(GET_PERMISSIONS_ROUTE, fetcher, { keepPreviousData: true });
|
||||
}
|
||||
46
src/lib/hooks/useRequest.js
Normal file
46
src/lib/hooks/useRequest.js
Normal file
@@ -0,0 +1,46 @@
|
||||
import { errorResponse } from "@/core/utils/errorResponse";
|
||||
import { successRequest } from "@/core/utils/successRequest";
|
||||
import axios from "axios";
|
||||
|
||||
const defaultOptions = {
|
||||
data: {},
|
||||
requestOptions: {
|
||||
headers: {},
|
||||
},
|
||||
notification: {
|
||||
show: true,
|
||||
success: false,
|
||||
failed: true,
|
||||
},
|
||||
};
|
||||
|
||||
const useRequest = (initOptions) => {
|
||||
const _options = Object.assign({}, defaultOptions, initOptions);
|
||||
|
||||
return async (url = "", method = "get", options) => {
|
||||
const mergedOptions = Object.assign({}, _options, options);
|
||||
|
||||
try {
|
||||
const response = await axios({
|
||||
url,
|
||||
method,
|
||||
data: method === "get" ? null : mergedOptions.data,
|
||||
withCredentials: true,
|
||||
...mergedOptions.requestOptions,
|
||||
});
|
||||
successRequest(mergedOptions.notification.show && mergedOptions.notification.success, "request_data");
|
||||
return response;
|
||||
} catch (error) {
|
||||
if (error.response) {
|
||||
errorResponse(
|
||||
error.response,
|
||||
mergedOptions.notification.show && mergedOptions.notification.failed,
|
||||
"request_data"
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
export default useRequest;
|
||||
9
src/lib/hooks/useTableSetting.js
Normal file
9
src/lib/hooks/useTableSetting.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import { useContext } from "react";
|
||||
import { TableSettingContext } from "../contexts/tableSetting";
|
||||
|
||||
const useTableSetting = () => {
|
||||
const { settingStore, hideAction, sortAction, filterAction, refactorAction } = useContext(TableSettingContext);
|
||||
return { settingStore, hideAction, sortAction, filterAction, refactorAction };
|
||||
};
|
||||
|
||||
export default useTableSetting;
|
||||
Reference in New Issue
Block a user