Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Add hook executed when modal state changes

dominik-zeglen94e85f3d6345
diff --git a/saleor/static/dashboard-next/hooks/useModalDialogErrors/useModalDialogErrors.ts b/saleor/static/dashboard-next/hooks/useModalDialogErrors/useModalDialogErrors.ts
index 0cb30c870b..54a393743d 100644
--- a/saleor/static/dashboard-next/hooks/useModalDialogErrors/useModalDialogErrors.ts
+++ b/saleor/static/dashboard-next/hooks/useModalDialogErrors/useModalDialogErrors.ts
@@ -1,5 +1,4 @@
-import { useState } from "react";
-
+import useModalDialogOpen from "../useModalDialogOpen";
import useStateFromProps from "../useStateFromProps";
function useModalDialogErrors<TError>(
@@ -7,14 +6,10 @@ function useModalDialogErrors<TError>(
open: boolean
): TError[] {
const [state, setState] = useStateFromProps(errors);
- const [prevOpenState, setPrevOpenstate] = useState(open);
- if (open !== prevOpenState) {
- setPrevOpenstate(open);
- if (!open) {
- setState([]);
- }
- }
+ useModalDialogOpen(open, {
+ onClose: () => setState([])
+ });
return state;
}
diff --git a/saleor/static/dashboard-next/hooks/useModalDialogOpen/index.ts b/saleor/static/dashboard-next/hooks/useModalDialogOpen/index.ts
new file mode 100644
index 0000000000..53579d8b89
--- /dev/null
+++ b/saleor/static/dashboard-next/hooks/useModalDialogOpen/index.ts
@@ -0,0 +1,2 @@
+export { default } from "./useModalDialogOpen";
+export * from "./useModalDialogOpen";
diff --git a/saleor/static/dashboard-next/hooks/useModalDialogOpen/useModalDialogOpen.test.ts b/saleor/static/dashboard-next/hooks/useModalDialogOpen/useModalDialogOpen.test.ts
new file mode 100644
index 0000000000..4df3113723
--- /dev/null
+++ b/saleor/static/dashboard-next/hooks/useModalDialogOpen/useModalDialogOpen.test.ts
@@ -0,0 +1,55 @@
+import { renderHook } from "@testing-library/react-hooks";
+
+import useModalDialogOpen from "./useModalDialogOpen";
+
+const onClose = jest.fn();
+const onOpen = jest.fn();
+
+const cbs = {
+ onClose,
+ onOpen
+};
+
+test("Does not render errors after close", () => {
+ const { rerender } = renderHook(
+ ({ open, cbs }) => useModalDialogOpen(open, cbs),
+ {
+ initialProps: {
+ cbs,
+ open: false
+ }
+ }
+ );
+
+ // Open modal
+ rerender({
+ cbs,
+ open: true
+ });
+ expect(onOpen).toBeCalledTimes(1);
+ expect(onClose).toBeCalledTimes(0);
+
+ // Rerender modal
+ rerender({
+ cbs,
+ open: true
+ });
+ expect(onOpen).toBeCalledTimes(1);
+ expect(onClose).toBeCalledTimes(0);
+
+ // Close modal
+ rerender({
+ cbs,
+ open: false
+ });
+ expect(onOpen).toBeCalledTimes(1);
+ expect(onClose).toBeCalledTimes(1);
+
+ // Open modal
+ rerender({
+ cbs,
+ open: true
+ });
+ expect(onOpen).toBeCalledTimes(2);
+ expect(onClose).toBeCalledTimes(1);
+});
diff --git a/saleor/static/dashboard-next/hooks/useModalDialogOpen/useModalDialogOpen.ts b/saleor/static/dashboard-next/hooks/useModalDialogOpen/useModalDialogOpen.ts
new file mode 100644
index 0000000000..8cfb88b2d5
--- /dev/null
+++ b/saleor/static/dashboard-next/hooks/useModalDialogOpen/useModalDialogOpen.ts
@@ -0,0 +1,26 @@
+import { useEffect, useState } from "react";
+
+function useModalDialogOpen(
+ open: boolean,
+ cbs: {
+ onClose?: () => void;
+ onOpen?: () => void;
+ }
+): void {
+ const [prevOpen, setPrevOpen] = useState(open);
+
+ useEffect(() => {
+ if (open !== prevOpen) {
+ setPrevOpen(open);
+ if (cbs.onOpen && open) {
+ cbs.onOpen();
+ }
+
+ if (cbs.onClose && !open) {
+ cbs.onClose();
+ }
+ }
+ }, [open]);
+}
+
+export default useModalDialogOpen;