55 lines
1.2 KiB
JavaScript
55 lines
1.2 KiB
JavaScript
import { store } from "../store/store";
|
|
import {
|
|
cacheApiResponse,
|
|
clearApiCacheKey,
|
|
flushApiCache,
|
|
} from "../slices/apiCacheSlice";
|
|
import { setLoginUserPermmisions } from "./globalVariablesSlice";
|
|
import { useSelector } from "react-redux";
|
|
|
|
// Cache data
|
|
export const cacheData = (key, data) => {
|
|
store.dispatch(cacheApiResponse({ key, data }));
|
|
};
|
|
|
|
// Get cached data
|
|
export const getCachedData = (key) => {
|
|
return store.getState().apiCache[key];
|
|
};
|
|
|
|
// Clear specific cache key
|
|
export const clearCacheKey = (key) => {
|
|
store.dispatch(clearApiCacheKey({ key }));
|
|
};
|
|
|
|
// Clear all cache
|
|
export const clearAllCache = () => {
|
|
store.dispatch(flushApiCache());
|
|
};
|
|
|
|
export const cacheProfileData = (data) => {
|
|
store.dispatch(setLoginUserPermmisions(data));
|
|
};
|
|
// Get cached data
|
|
export const getCachedProfileData = () => {
|
|
return store.getState().globalVariables.loginUser;
|
|
};
|
|
|
|
export const useSelectedProject = () => {
|
|
const selectedProject = useSelector(
|
|
(store) => store.localVariables.projectId
|
|
);
|
|
|
|
const project = localStorage.getItem("project");
|
|
|
|
if (project) {
|
|
try {
|
|
const parsed = JSON.parse(project);
|
|
return parsed ?? selectedProject;
|
|
} catch (e) {
|
|
return selectedProject;
|
|
}
|
|
}
|
|
|
|
return selectedProject;
|
|
} |