Skip to content

Commit

Permalink
Consolidate redux calls to PrivacyDeclarationManager
Browse files Browse the repository at this point in the history
The redux calls had originally been isolated to a parent component to facilitate reuse across redux environments, but we do not need that anymore
  • Loading branch information
allisonking committed May 8, 2023
1 parent c66e9ba commit 94dd6dd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,13 @@ import {
Slide,
Spacer,
Text,
useToast,
} from "@fidesui/react";
import { SerializedError } from "@reduxjs/toolkit";
import { FetchBaseQueryError } from "@reduxjs/toolkit/dist/query/fetchBaseQuery";
import { DataFlowAccordion } from "common/system-data-flow/DataFlowAccordion";
import React, { useMemo } from "react";

import { getErrorMessage, isErrorResult } from "~/features/common/helpers";
import { errorToastParams, successToastParams } from "~/features/common/toast";
import { usePrivacyDeclarationData } from "~/features/system/privacy-declarations/hooks";
import PrivacyDeclarationManager from "~/features/system/privacy-declarations/PrivacyDeclarationManager";
import { PrivacyDeclarationWithId } from "~/features/system/privacy-declarations/types";
import {
useGetSystemByFidesKeyQuery,
useUpdateSystemMutation,
} from "~/features/system/system.slice";
import { PrivacyDeclaration } from "~/types/api/models/PrivacyDeclaration";
import { System } from "~/types/api/models/System";
import { useGetSystemByFidesKeyQuery } from "~/features/system/system.slice";

import SystemInfo from "./SystemInfo";

Expand All @@ -40,57 +29,11 @@ const DatamapDrawer = ({
const { isLoading, ...dataProps } = usePrivacyDeclarationData({
includeDatasets: false,
});
const toast = useToast();

const [updateSystemMutationTrigger] = useUpdateSystemMutation();
const { data: system } = useGetSystemByFidesKeyQuery(selectedSystemId!, {
skip: !selectedSystemId,
});

const handleSave = async (
updatedDeclarations: PrivacyDeclaration[],
isDelete?: boolean
): Promise<PrivacyDeclarationWithId[] | undefined> => {
const systemBodyWithDeclaration = {
...system!,
privacy_declarations: updatedDeclarations,
};

const handleResult = (
result:
| { data: System }
| { error: FetchBaseQueryError | SerializedError }
) => {
if (isErrorResult(result)) {
const errorMsg = getErrorMessage(
result.error,
"An unexpected error occurred while updating the system. Please try again."
);

toast(errorToastParams(errorMsg));
return undefined;
}
toast.closeAll();
toast(
successToastParams(isDelete ? "Data use deleted" : "Data use saved")
);
return result.data.privacy_declarations as PrivacyDeclarationWithId[];
};

const updateSystemResult = await updateSystemMutationTrigger(
systemBodyWithDeclaration
);

return handleResult(updateSystemResult);
};
const collisionWarning = () => {
toast(
errorToastParams(
"A declaration already exists with that data use in this system. Please supply a different data use."
)
);
};

return (
<Box
position="absolute"
Expand Down Expand Up @@ -189,8 +132,6 @@ const DatamapDrawer = ({
<Box pb={3}>
<PrivacyDeclarationManager
system={system}
onCollision={collisionWarning}
onSave={handleSave}
addButtonProps={{ ml: 4 }}
{...dataProps}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import {
Tooltip,
useToast,
} from "@fidesui/react";
import { SerializedError } from "@reduxjs/toolkit";
import { FetchBaseQueryError } from "@reduxjs/toolkit/dist/query";
import { useEffect, useMemo, useState } from "react";

import { getErrorMessage } from "~/features/common/helpers";
import { errorToastParams, successToastParams } from "~/features/common/toast";
import { useUpdateSystemMutation } from "~/features/system/system.slice";
import { PrivacyDeclaration, System } from "~/types/api";
import { isErrorResult } from "~/types/errors";

import PrivacyDeclarationAccordion from "./PrivacyDeclarationAccordion";
import {
Expand All @@ -32,25 +38,21 @@ const transformDeclarationForSubmission = (

interface Props {
system: System;
onCollision: () => void;
onSave: (
privacyDeclarations: PrivacyDeclaration[],
isDelete?: boolean
) => Promise<PrivacyDeclarationWithId[] | undefined>;
addButtonProps?: ButtonProps;
includeCustomFields?: boolean;
onSave?: (system: System) => void;
}

const PrivacyDeclarationManager = ({
system,
onCollision,
onSave,
addButtonProps,
includeCustomFields,
onSave,
...dataProps
}: Props & DataProps) => {
const toast = useToast();

const [updateSystemMutationTrigger] = useUpdateSystemMutation();
const [showNewForm, setShowNewForm] = useState(false);
const [newDeclaration, setNewDeclaration] = useState<
PrivacyDeclarationWithId | undefined
Expand All @@ -73,7 +75,11 @@ const PrivacyDeclarationManager = ({
(d) => d.data_use === values.data_use && d.name === values.name
).length > 0
) {
onCollision();
toast(
errorToastParams(
"A declaration already exists with that data use in this system. Please supply a different data use."
)
);
return true;
}
return false;
Expand All @@ -86,8 +92,39 @@ const PrivacyDeclarationManager = ({
const transformedDeclarations = updatedDeclarations.map((d) =>
transformDeclarationForSubmission(d)
);
const res = await onSave(transformedDeclarations, isDelete);
return res;
const systemBodyWithDeclaration = {
...system,
privacy_declarations: transformedDeclarations,
};
const handleResult = (
result:
| { data: System }
| { error: FetchBaseQueryError | SerializedError }
) => {
if (isErrorResult(result)) {
const errorMsg = getErrorMessage(
result.error,
"An unexpected error occurred while updating the system. Please try again."
);

toast(errorToastParams(errorMsg));
return undefined;
}
toast.closeAll();
toast(
successToastParams(isDelete ? "Data use deleted" : "Data use saved")
);
if (onSave) {
onSave(result.data);
}
return result.data.privacy_declarations as PrivacyDeclarationWithId[];
};

const updateSystemResult = await updateSystemMutationTrigger(
systemBodyWithDeclaration
);

return handleResult(updateSystemResult);
};

const handleEditDeclaration = async (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { Heading, Spinner, Stack, Text, useToast } from "@fidesui/react";
import { SerializedError } from "@reduxjs/toolkit";
import { FetchBaseQueryError } from "@reduxjs/toolkit/dist/query/fetchBaseQuery";
import { Heading, Spinner, Stack, Text } from "@fidesui/react";
import NextLink from "next/link";

import { useAppDispatch } from "~/app/hooks";
import { getErrorMessage, isErrorResult } from "~/features/common/helpers";
import { errorToastParams, successToastParams } from "~/features/common/toast";
import { setActiveSystem, useUpdateSystemMutation } from "~/features/system";
import { PrivacyDeclarationWithId } from "~/features/system/privacy-declarations/types";
import { PrivacyDeclaration, System } from "~/types/api";
import { setActiveSystem } from "~/features/system";
import { System } from "~/types/api";

import { usePrivacyDeclarationData } from "./hooks";
import PrivacyDeclarationManager from "./PrivacyDeclarationManager";
Expand All @@ -18,57 +13,14 @@ interface Props {
}

const PrivacyDeclarationStep = ({ system }: Props) => {
const toast = useToast();
const dispatch = useAppDispatch();
const [updateSystemMutationTrigger] = useUpdateSystemMutation();

const { isLoading, ...dataProps } = usePrivacyDeclarationData({
includeDatasets: true,
});

const handleSave = async (
updatedDeclarations: PrivacyDeclaration[],
isDelete?: boolean
): Promise<PrivacyDeclarationWithId[] | undefined> => {
const systemBodyWithDeclaration = {
...system,
privacy_declarations: updatedDeclarations,
};

const handleResult = (
result:
| { data: System }
| { error: FetchBaseQueryError | SerializedError }
) => {
if (isErrorResult(result)) {
const errorMsg = getErrorMessage(
result.error,
"An unexpected error occurred while updating the system. Please try again."
);

toast(errorToastParams(errorMsg));
return undefined;
}
toast.closeAll();
toast(
successToastParams(isDelete ? "Data use deleted" : "Data use saved")
);
dispatch(setActiveSystem(result.data));
return result.data.privacy_declarations as PrivacyDeclarationWithId[];
};

const updateSystemResult = await updateSystemMutationTrigger(
systemBodyWithDeclaration
);

return handleResult(updateSystemResult);
};

const collisionWarning = () => {
toast(
errorToastParams(
"A declaration already exists with that data use in this system. Please supply a different data use."
)
);
const onSave = (savedSystem: System) => {
dispatch(setActiveSystem(savedSystem));
};

return (
Expand All @@ -94,8 +46,7 @@ const PrivacyDeclarationStep = ({ system }: Props) => {
) : (
<PrivacyDeclarationManager
system={system}
onCollision={collisionWarning}
onSave={handleSave}
onSave={onSave}
includeCustomFields
{...dataProps}
/>
Expand Down

0 comments on commit 94dd6dd

Please sign in to comment.