formatted
This commit is contained in:
@@ -1,26 +1,26 @@
|
||||
'use client'
|
||||
import {createContext, useCallback, useEffect, useState} from "react";
|
||||
"use client";
|
||||
import { createContext, useCallback, useEffect, useState } from "react";
|
||||
|
||||
export const TableSettingContext = createContext();
|
||||
export const TableSettingProvider = ({children}) => {
|
||||
export const TableSettingProvider = ({ children }) => {
|
||||
const [settingStore, setSettingStore] = useState({});
|
||||
|
||||
useEffect(() => {
|
||||
const _localStorage = localStorage.getItem("_setting-app")
|
||||
const _localStorage = localStorage.getItem("_setting-app");
|
||||
if (_localStorage) {
|
||||
setSettingStore(JSON.parse(_localStorage))
|
||||
setSettingStore(JSON.parse(_localStorage));
|
||||
}
|
||||
}, []);
|
||||
|
||||
const hideAction = useCallback((user_id, page_name, table_name, settingValue, columns) => {
|
||||
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 updateSettingStorage = (user_id, page_name, table_name, settingType, settingValue, columns) => {
|
||||
const prevLocalStorage = JSON.parse(localStorage.getItem("_setting-app")) || {};
|
||||
const userSettings = prevLocalStorage[user_id] || {};
|
||||
@@ -30,7 +30,7 @@ export const TableSettingProvider = ({children}) => {
|
||||
if (settingType === "sorts" || settingType === "filters") {
|
||||
newSettings = [...settingValue];
|
||||
} else {
|
||||
newSettings = {...tableSettings[settingType] || {}, ...settingValue};
|
||||
newSettings = { ...(tableSettings[settingType] || {}), ...settingValue };
|
||||
}
|
||||
const updatedLocalStorage = {
|
||||
...prevLocalStorage,
|
||||
@@ -40,18 +40,21 @@ export const TableSettingProvider = ({children}) => {
|
||||
...pageSettings,
|
||||
[table_name]: {
|
||||
...tableSettings,
|
||||
[settingType]: Array.isArray(settingValue) ? [...newSettings] : {...newSettings},
|
||||
summary: SummaryTextMessage({
|
||||
...tableSettings,
|
||||
[settingType]: Array.isArray(settingValue) ? [...newSettings] : {...newSettings},
|
||||
}, columns)
|
||||
[settingType]: Array.isArray(settingValue) ? [...newSettings] : { ...newSettings },
|
||||
summary: SummaryTextMessage(
|
||||
{
|
||||
...tableSettings,
|
||||
[settingType]: Array.isArray(settingValue) ? [...newSettings] : { ...newSettings },
|
||||
},
|
||||
columns
|
||||
),
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
localStorage.setItem("_setting-app", JSON.stringify(updatedLocalStorage));
|
||||
setSettingStore(updatedLocalStorage);
|
||||
}
|
||||
};
|
||||
const refactorAction = (user_id, page_name, table_name) => {
|
||||
const prevLocalStorage = JSON.parse(localStorage.getItem("_setting-app")) || {};
|
||||
const userSettings = prevLocalStorage[user_id] || {};
|
||||
@@ -68,7 +71,7 @@ export const TableSettingProvider = ({children}) => {
|
||||
};
|
||||
localStorage.setItem("_setting-app", JSON.stringify(refactorData));
|
||||
setSettingStore(refactorData);
|
||||
}
|
||||
};
|
||||
const SummaryTextMessage = (values, columns) => {
|
||||
let SummaryText = "";
|
||||
let HideText = "";
|
||||
@@ -76,38 +79,35 @@ export const TableSettingProvider = ({children}) => {
|
||||
let SortText = "";
|
||||
|
||||
if (values.sorts && values.sorts.length > 0) {
|
||||
const _list = []
|
||||
const _list = [];
|
||||
for (const sort of values.sorts) {
|
||||
const _column = columns.find(column => column.id == sort.id)
|
||||
if (_column)
|
||||
_list.push(`${_column.header}` + `(${sort.desc ? "نزولی" : "صعودی"})`)
|
||||
const _column = columns.find((column) => column.id == sort.id);
|
||||
if (_column) _list.push(`${_column.header}` + `(${sort.desc ? "نزولی" : "صعودی"})`);
|
||||
}
|
||||
if (_list.length > 0)
|
||||
SortText = " مرتب شده بر اساس : " + _list.join(", ")
|
||||
if (_list.length > 0) SortText = " مرتب شده بر اساس : " + _list.join(", ");
|
||||
}
|
||||
if (values.filters && values.filters.length > 0) {
|
||||
const _list = []
|
||||
const _list = [];
|
||||
for (const filter of values.filters) {
|
||||
const _column = columns.find(column => column.id == filter.id)
|
||||
if (_column)
|
||||
_list.push(`${_column.header}`)
|
||||
const _column = columns.find((column) => column.id == filter.id);
|
||||
if (_column) _list.push(`${_column.header}`);
|
||||
}
|
||||
if (_list.length > 0)
|
||||
FilterText = " فیلتر شده بر اساس : " + _list.join(", ")
|
||||
if (_list.length > 0) FilterText = " فیلتر شده بر اساس : " + _list.join(", ");
|
||||
}
|
||||
|
||||
if (values.hides) {
|
||||
const hidesArray = Object.entries(values.hides).map((e) => ({id: e[0], value: e[1]}))
|
||||
const hidesArray = Object.entries(values.hides).map((e) => ({
|
||||
id: e[0],
|
||||
value: e[1],
|
||||
}));
|
||||
if (hidesArray.length > 0) {
|
||||
const _list = []
|
||||
const _list = [];
|
||||
for (const hide of hidesArray) {
|
||||
if (hide.value) continue
|
||||
const _column = columns.find(c => c.id == hide.id)
|
||||
if (_column)
|
||||
_list.push(_column.header)
|
||||
if (hide.value) continue;
|
||||
const _column = columns.find((c) => c.id == hide.id);
|
||||
if (_column) _list.push(_column.header);
|
||||
}
|
||||
if (_list.length > 0)
|
||||
HideText = "ستون های مخفی شده : " + _list.join(", ");
|
||||
if (_list.length > 0) HideText = "ستون های مخفی شده : " + _list.join(", ");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ export const TableSettingProvider = ({children}) => {
|
||||
}
|
||||
}
|
||||
if (FilterText) {
|
||||
SummaryText += FilterText
|
||||
SummaryText += FilterText;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,8 +137,15 @@ export const TableSettingProvider = ({children}) => {
|
||||
|
||||
return (
|
||||
<TableSettingContext.Provider
|
||||
value={{settingStore, hideAction, sortAction, filterAction, refactorAction}}>
|
||||
value={{
|
||||
settingStore,
|
||||
hideAction,
|
||||
sortAction,
|
||||
filterAction,
|
||||
refactorAction,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</TableSettingContext.Provider>
|
||||
);
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user