Skip to content

Commit

Permalink
add saveview callbacks (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
dominic-lcw committed Sep 21, 2024
1 parent 3148734 commit 1c7acef
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion app/src/components/grid/stdGrid.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import React, { useEffect, useState, useMemo, useRef } from "react";
import React, {
useEffect,
useState,
useMemo,
useRef,
useCallback,
} from "react";
import { AgGridReact } from "ag-grid-react";
import { Grid2, Button } from "@mui/material";

Expand Down Expand Up @@ -224,9 +230,50 @@ const StdAgGrid: React.FC<StdAgGridProps> = (props) => {
const onModelUpdated = (params: any) => {};

const onGridReady = (params: any) => {
console.log("onGridReady", params);
setGridApi(params.api);
};

// region: Load And Save Views
const saveGridState = useCallback(async () => {
console.log("dom check");
if (gridApi) {
const columnState = gridApi.getColumnState();
let filterState = gridApi.getFilterModel();
let sortState = gridApi.getSortModel();

// Null handling
if (filterState === null || Object.keys(filterState).length === 0) {
filterState = { empty: "empty" };
}
if (sortState === undefined) {
sortState = [];
}

console.log("check state", columnState, filterState, sortState);
console.log(
"check state2",
`CREATE OR REPLACE TABLE saved_grid_states AS
SELECT ${JSON.stringify(sortState)} AS column_state,
${filterState} AS filter_state,
${sortState} AS sort_state `,
);
const connection = await db.connect();
await connection.query(`CREATE OR REPLACE TABLE saved_grid_states AS
SELECT ${columnState} AS column_state,
${filterState} AS filter_state,
${sortState} AS sort_state `);
await connection.close();
}
}, [gridApi]);

useEffect(() => {
window.addEventListener("beforeunload", saveGridState);
return () => {
window.removeEventListener("beforeunload", saveGridState);
};
}, [saveGridState]);

const onFirstDataRendered = () => {
const endTime = performance.now();
const execTime = endTime - startTime.current;
Expand Down Expand Up @@ -331,6 +378,15 @@ const StdAgGrid: React.FC<StdAgGridProps> = (props) => {
Autosize Columns
</Button>
</Grid2>
<Grid2>
<Button
style={{ outline: "none" }}
variant="contained"
onClick={saveGridState}
>
Save View
</Button>
</Grid2>
</Grid2>
</Grid2>
<Grid2
Expand Down

0 comments on commit 1c7acef

Please sign in to comment.