99 lines
4.0 KiB
JavaScript
99 lines
4.0 KiB
JavaScript
"use client";
|
|
import { createContext, useCallback, useEffect, useState } from "react";
|
|
|
|
export const TableSettingContext = createContext();
|
|
export const TableSettingProvider = ({ children }) => {
|
|
const [settingStore, setSettingStore] = useState({});
|
|
|
|
useEffect(() => {
|
|
const _localStorage = localStorage.getItem("_setting-app");
|
|
if (_localStorage) {
|
|
setSettingStore(JSON.parse(_localStorage));
|
|
}
|
|
}, []);
|
|
|
|
const hideAction = useCallback((user_id, page_name, table_name, settingValue, columns) => {
|
|
clearAction(user_id, page_name, table_name, "hides");
|
|
updateSettingStorage(user_id, page_name, table_name, "hides", settingValue, columns);
|
|
}, []);
|
|
const sortAction = useCallback((user_id, page_name, table_name, settingValue, columns) => {
|
|
updateSettingStorage(user_id, page_name, table_name, "sorts", settingValue, columns);
|
|
}, []);
|
|
const filterAction = (user_id, page_name, table_name, settingValue, columns) => {
|
|
updateSettingStorage(user_id, page_name, table_name, "filters", settingValue, columns);
|
|
};
|
|
const resetAction = (user_id, page_name, table_name) => {
|
|
const prevLocalStorage = JSON.parse(localStorage.getItem("_setting-app")) || {};
|
|
const userSettings = prevLocalStorage[user_id] || {};
|
|
const pageSettings = userSettings[page_name] || {};
|
|
delete pageSettings[table_name];
|
|
const resetData = {
|
|
...prevLocalStorage,
|
|
[user_id]: {
|
|
...userSettings,
|
|
[page_name]: {
|
|
...pageSettings,
|
|
},
|
|
},
|
|
};
|
|
localStorage.setItem("_setting-app", JSON.stringify(resetData));
|
|
setSettingStore(resetData);
|
|
};
|
|
const updateSettingStorage = (user_id, page_name, table_name, settingType, settingValue, columns) => {
|
|
const prevLocalStorage = JSON.parse(localStorage.getItem("_setting-app")) || {};
|
|
const userSettings = prevLocalStorage[user_id] || {};
|
|
const pageSettings = userSettings[page_name] || {};
|
|
const tableSettings = pageSettings[table_name] || {};
|
|
let newSettings;
|
|
if (settingType === "sorts" || settingType === "filters") {
|
|
newSettings = [...settingValue];
|
|
} else {
|
|
newSettings = { ...(tableSettings[settingType] || {}), ...settingValue };
|
|
}
|
|
const updatedLocalStorage = {
|
|
...prevLocalStorage,
|
|
[user_id]: {
|
|
...userSettings,
|
|
[page_name]: {
|
|
...pageSettings,
|
|
[table_name]: {
|
|
...tableSettings,
|
|
[settingType]: Array.isArray(settingValue) ? [...newSettings] : { ...newSettings },
|
|
},
|
|
},
|
|
},
|
|
};
|
|
localStorage.setItem("_setting-app", JSON.stringify(updatedLocalStorage));
|
|
setSettingStore(updatedLocalStorage);
|
|
};
|
|
|
|
const clearAction = (user_id, page_name, table_name, key) => {
|
|
const prevLocalStorage = JSON.parse(localStorage.getItem("_setting-app")) || {};
|
|
const userSettings = prevLocalStorage[user_id] || {};
|
|
const pageSettings = userSettings[page_name] || {};
|
|
const tableSettings = pageSettings[table_name] || {};
|
|
const updatedSettings = {
|
|
...prevLocalStorage,
|
|
[user_id]: {
|
|
...userSettings,
|
|
[page_name]: {
|
|
...pageSettings,
|
|
[table_name]: {
|
|
...tableSettings,
|
|
[key]: Array.isArray(tableSettings[key]) ? [] : {},
|
|
},
|
|
},
|
|
},
|
|
};
|
|
|
|
localStorage.setItem("_setting-app", JSON.stringify(updatedSettings));
|
|
setSettingStore(updatedSettings);
|
|
};
|
|
|
|
return (
|
|
<TableSettingContext.Provider value={{ settingStore, hideAction, sortAction, filterAction, resetAction }}>
|
|
{children}
|
|
</TableSettingContext.Provider>
|
|
);
|
|
};
|