Skip to content

Commit

Permalink
fix Saved report reset functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
gilluminate committed Jan 7, 2025
1 parent d8158a0 commit 81ad2b5
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 18 deletions.
35 changes: 31 additions & 4 deletions clients/admin-ui/cypress/e2e/datamap-report.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,14 +398,15 @@ describe("Data map report table", () => {
cy.get("#toast-datamap-report-toast")
.should("be.visible")
.should("have.attr", "data-status", "success");
cy.getByTestId("custom-reports-trigger")
.should("contain.text", "My Custom Report")
.click();
cy.getByTestId("custom-reports-trigger").should(
"contain.text",
"My Custom Report",
);
cy.getByTestId("fidesTable").within(() => {
// reordering applied to report
cy.get("thead th").eq(2).should("contain.text", "Legal name");
// column visibility applied to report
cy.get("thead th").eq(4).should("not.contain.text", "Data subject");
cy.getByTestId("column-data_subjects").should("not.exist");
});
cy.getByTestId("group-by-menu").should(
"contain.text",
Expand Down Expand Up @@ -451,10 +452,36 @@ describe("Data map report table", () => {
cy.getByTestId("custom-reports-reset-button").click();
cy.getByTestId("apply-report-button").click();
cy.getByTestId("custom-reports-popover").should("not.be.visible");

cy.getByTestId("custom-reports-trigger").should(
"contain.text",
"Reports",
);
cy.getByTestId("fidesTable").within(() => {
// reordering reverted
cy.get("thead th").eq(2).should("contain.text", "Data categories");
// column visibility restored
cy.getByTestId("column-data_subjects").should("exist");
});
cy.getByTestId("group-by-menu").should("contain.text", "Group by system");
cy.getByTestId("more-menu").click();
cy.getByTestId("edit-columns-btn").click();
cy.get("button#data_subjects").should(
"have.attr",
"aria-checked",
"true",
);
cy.getByTestId("column-settings-close-button").click();
cy.getByTestId("filter-multiple-systems-btn").click();
cy.getByTestId("datamap-report-filter-modal")
.should("be.visible")
.within(() => {
cy.getByTestId("filter-modal-accordion-button").eq(0).click();
cy.getByTestId("checkbox-Analytics").within(() => {
cy.get("[data-checked]").should("not.exist");
});
cy.getByTestId("standard-dialog-close-btn").click();
});
});
it("should allow the user cancel a report selection", () => {
cy.wait("@getCustomReportsMinimal");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ import {
import { CustomReportTemplates } from "../../common/custom-reports/CustomReportTemplates";
import { DATAMAP_LOCAL_STORAGE_KEYS, DEFAULT_COLUMN_NAMES } from "./constants";
import { DatamapReportWithCustomFields as DatamapReport } from "./datamap-report";
import { useDatamapReport } from "./datamap-report-context";
import {
DEFAULT_COLUMN_FILTERS,
DEFAULT_COLUMN_VISIBILITY,
useDatamapReport,
} from "./datamap-report-context";
import {
getDatamapReportColumns,
getDefaultColumn,
Expand Down Expand Up @@ -351,12 +355,41 @@ export const DatamapReportTable = () => {

const handleSavedReport = (
savedReport: CustomReportResponse | null,
resetForm: (
resetColumnNameForm: (
nextState?: Partial<FormikState<Record<string, string>>> | undefined,
) => void,
) => {
if (!savedReport && !savedCustomReportId) {
return;
}
if (!savedReport) {
setSavedCustomReportId("");
try {
setSavedCustomReportId("");

/* NOTE: we can't just use tableInstance.reset() here because it will reset the table to the initial state, which is likely to include report settings that were saved in the user's local storage. Instead, we need to reset each individual setting to its default value. */

// reset column visibility (must happen before updating order)
setColumnVisibility(DEFAULT_COLUMN_VISIBILITY);
tableInstance.toggleAllColumnsVisible(true);
tableInstance.setColumnVisibility(DEFAULT_COLUMN_VISIBILITY);

// reset column order (must happen prior to updating groupBy)
setColumnOrder([]);
tableInstance.setColumnOrder([]);

// reset groupBy and filters (will automatically update the tableinstance)
setGroupBy(DATAMAP_GROUPING.SYSTEM_DATA_USE);
setSelectedFilters(DEFAULT_COLUMN_FILTERS);

// reset column names
setColumnNameMapOverrides({});
resetColumnNameForm({ values: {} });
} catch (error: any) {
toast({
status: "error",
description: "There was a problem resetting the report.",
});
}
return;
}
try {
Expand All @@ -375,8 +408,8 @@ export const DatamapReportTable = () => {
);

if (savedGroupBy) {
// No need to manually update the tableInstance here; setting the groupBy will trigger the useEffect to update the grouping.
setGroupBy(savedGroupBy);
tableInstance.setGrouping(getGrouping(savedGroupBy));
}
if (savedFilters) {
setSelectedFilters(savedFilters);
Expand All @@ -400,7 +433,7 @@ export const DatamapReportTable = () => {
},
);
setColumnNameMapOverrides(columnNameMap);
resetForm({ values: columnNameMap });
resetColumnNameForm({ values: columnNameMap });
}
setSavedCustomReportId(savedReport.id);
toast({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ import { DATAMAP_GROUPING } from "~/types/api";
import { DatamapReportFilterSelections } from "../types";
import { COLUMN_IDS, DATAMAP_LOCAL_STORAGE_KEYS } from "./constants";

export const DEFAULT_COLUMN_VISIBILITY = {
[COLUMN_IDS.SYSTEM_UNDECLARED_DATA_CATEGORIES]: false,
[COLUMN_IDS.DATA_USE_UNDECLARED_DATA_CATEGORIES]: false,
};

export const DEFAULT_COLUMN_FILTERS = {
dataUses: [],
dataSubjects: [],
dataCategories: [],
};

interface DatamapReportContextProps {
savedCustomReportId: string;
setSavedCustomReportId: Dispatch<SetStateAction<string>>;
Expand Down Expand Up @@ -51,11 +62,7 @@ export const DatamapReportProvider = ({
const [selectedFilters, setSelectedFilters] =
useLocalStorage<DatamapReportFilterSelections>(
DATAMAP_LOCAL_STORAGE_KEYS.FILTERS,
{
dataUses: [],
dataSubjects: [],
dataCategories: [],
},
DEFAULT_COLUMN_FILTERS,
);

const [columnOrder, setColumnOrder] = useLocalStorage<string[]>(
Expand All @@ -65,10 +72,7 @@ export const DatamapReportProvider = ({

const [columnVisibility, setColumnVisibility] = useLocalStorage<
Record<string, boolean>
>(DATAMAP_LOCAL_STORAGE_KEYS.COLUMN_VISIBILITY, {
[COLUMN_IDS.SYSTEM_UNDECLARED_DATA_CATEGORIES]: false,
[COLUMN_IDS.DATA_USE_UNDECLARED_DATA_CATEGORIES]: false,
});
>(DATAMAP_LOCAL_STORAGE_KEYS.COLUMN_VISIBILITY, DEFAULT_COLUMN_VISIBILITY);

const [columnSizing, setColumnSizing] = useLocalStorage<
Record<string, number>
Expand Down

0 comments on commit 81ad2b5

Please sign in to comment.