I got a problem where i need a Function to make that a Start button generate a save Pop up and when to do so, save the Layout on the Old LAyout.
In this moment What it does is tracking Every modification on the layout and Saveit automaticclay int the Old layout, i dont know how to build these here beacuse it work on diferents compontenes, the botton on Action BAR and the layout manipulation on the Pivot Grid compo
this are the funciton for the pivto grid component tahta handle the save and changes on the table
//==================================================================================
//================= HANDLE SAVE AND DISCARD CHANGES =======================
//==================================================================================
const saveChanges = async () => {
console.log(pivotGridDetails);
const savedPivotTableDetails = localStorage.getItem(props.selectedItemInSidePanel.value + LocalStorageType.PivotgridDetails);
const selectedItemInSidePanel = localStorage.getItem(“savedSelectedItemInSidePanel”);
let pivotgridDto: IPivotgridDto = nullPivotgridDto;
pivotgridDto.name = selectedItemInSidePanel ? JSON.parse(selectedItemInSidePanel).value : props.selectedItemInSidePanel.value;
if (savedPivotTableDetails) {
localStorage.setItem(props.selectedItemInSidePanel.value + LocalStorageType.PivotgridOldDetails, savedPivotTableDetails);
const parsedSavedPivotTableDetails: ISavedPivotTable = JSON.parse(savedPivotTableDetails);
parsedSavedPivotTableDetails.areFiltersSet = true;
setPivotGridDetails(parsedSavedPivotTableDetails);
pivotgridDto.datasourceId = parsedSavedPivotTableDetails.dataSourceId;
pivotgridDto.selectedTable = parsedSavedPivotTableDetails.selectedTable;
pivotgridDto.filters = JSON.stringify(
parsedSavedPivotTableDetails.filters.map((filter: IFilter) => {
filter.value = "";
return filter;
})
);
pivotgridDto.parameters = JSON.stringify(parsedSavedPivotTableDetails.parameters);
pivotgridDto.calculatedFields = JSON.stringify(pivotgrid.calculatedFields);
pivotgridDto.numberFieldsFormat = JSON.stringify(pivotgrid.numberFieldsFormat);
pivotgridDto.description = props.selectedItemInSidePanel.pivotgrid ? props.selectedItemInSidePanel.pivotgrid.description : "";
pivotgridDto.authorId = 0;
pivotgridDto.description = pivotgrid.description;
pivotgridDto.showColumnGrandTotals = props.selectedItemInSidePanel.pivotgrid ? props.selectedItemInSidePanel.pivotgrid.showColumnGrandTotals : false;
pivotgridDto.showColumnTotals = props.selectedItemInSidePanel.pivotgrid ? props.selectedItemInSidePanel.pivotgrid.showColumnTotals : false;
pivotgridDto.showRowGrandTotals = props.selectedItemInSidePanel.pivotgrid ? props.selectedItemInSidePanel.pivotgrid.showRowGrandTotals : false;
pivotgridDto.showRowTotals = props.selectedItemInSidePanel.pivotgrid ? props.selectedItemInSidePanel.pivotgrid.showRowTotals : false;
pivotgridDto.creationDate = "";
pivotgridDto.lastEditorId = props.userSession.userId;
pivotgridDto.lastEditDate = getDateForLogging();
}
if (props.selectedItemInSidePanel.pivotgrid) {
const optionsToSave = {
method: "PUT",
headers: {
Authorization: `Bearer ${props.userSession.token}`,
"Content-Type": "application/json",
},
body: JSON.stringify(pivotgridDto),
};
let response = await fetch(APIURL + PIVOTGRIDS + "EditPivotgrid/" + props.selectedItemInSidePanel.pivotgrid.id, optionsToSave);
let retrievedData = await response.json();
if (retrievedData.succeeded) {
props.triggerFetchForItem();
props.setIsEditModeOn(false);
} else {
notify( "error", 3000);
}
}
};
const saveState = useCallback(
(state: any) => {
if (props.selectedItemInSidePanel.type === “pivotTable” && props.selectedItemInSidePanel.value !== “”) {
const oldSavedPivotTableLayout = localStorage.getItem(props.selectedItemInSidePanel.value + LocalStorageType.PivotgridOldLayout);
if (oldSavedPivotTableLayout === null) {
localStorage.setItem(props.selectedItemInSidePanel.value + LocalStorageType.PivotgridLayout, JSON.stringify(state));
setPivotGridLayout(oldSavedPivotTableLayout);
} else {
const oldSavedPivotTableLayout = localStorage.getItem(props.selectedItemInSidePanel.value + LocalStorageType.PivotgridOldLayout);
setPivotGridLayout(oldSavedPivotTableLayout ? JSON.parse(oldSavedPivotTableLayout) : null);
}
// esto hace que guarde el estado automaticamente cundo hay una modoficacion en algun parametro
localStorage.setItem(props.selectedItemInSidePanel.value + LocalStorageType.PivotgridOldLayout, JSON.stringify(state));
// Sets loading to false here because of the state saving.
// Once saveState has ran, it saves on LocalStorage the layout and if you select
// a new layout faster than saveState, then the layout will be outdated.
}
setLoading(false);
},
[props.selectedItemInSidePanel]
);
const loadState = useCallback(
(state: any) => {
const localStoredLayout = localStorage.getItem(props.selectedItemInSidePanel.value + LocalStorageType.PivotgridOldLayout);
const parsedLayout = JSON.parse(localStoredLayout ? localStoredLayout : "{}");
return parsedLayout;
},
[props.selectedItemInSidePanel]
);
const StartSaveReport = () => {
const localStorageKeyOld = props.selectedItemInSidePanel.value + LocalStorageType.DatagridOldLayout;
const localStorageKeyLayout = props.selectedItemInSidePanel.value + LocalStorageType.DatagridLayout;
// Get the old layout from local storage
const oldLayout = JSON.parse(localStorage.getItem(localStorageKeyOld) || "{}");
// Get the current layout from local storage
const currentLayout = JSON.parse(localStorage.getItem(localStorageKeyLayout) || "{}");
const areLayoutsEqual = (DatagridOldLayout: any, DatagridLayout: any) => {
return JSON.stringify(DatagridOldLayout) === JSON.stringify(DatagridLayout);
};
// Check if the layouts are different
const isLayoutChanged = !areLayoutsEqual(oldLayout, currentLayout);
// If the layout is not changed, exit the function
if (!isLayoutChanged) {
const shouldSaveChanges = window.confirm("¿Desea guardar los cambios realizados?");
if (shouldSaveChanges) {
// Call the saveState function or perform the necessary actions
// props.savePivotGridState();
}
props.setStartReport(true);
props.setReportLoading(true);
return;
}
props.setStartReport(true);
props.setReportLoading(true);
};
const buttonsForBasicReports = [
{
text: “Start”,
icon: “”,
//text:{t(“reportSetupSlideOver.start”)},
customIconSettings: iconCustomSettings_StartButton,
customTextSettings: textCustomSettings_StartButton,
customBackgroundSettings: backgroundCustomSettings_StartButton,
customBorderSettings: borderCustomSettings_StartButton,
onClick: () => {
StartSaveReport();
},
`