Skip to content

Commit

Permalink
Merge branch 'fix/force-redraw' of github.com:cds-snc/platform-forms-…
Browse files Browse the repository at this point in the history
…client into fix/force-redraw
  • Loading branch information
dsamojlenko committed Jun 13, 2024
2 parents 823ff37 + 383a44a commit b3f174f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
ForwardRefRenderFunction,
useEffect,
useState,
useRef,
} from "react";

import ReactFlow, {
Expand All @@ -17,6 +18,7 @@ import ReactFlow, {
useEdgesState,
useReactFlow,
Background,
ReactFlowInstance,
} from "reactflow";

import "reactflow/dist/style.css";
Expand All @@ -41,7 +43,6 @@ const Loading = () => (

export interface FlowProps {
children?: ReactElement;
updateEdges?: () => void;
redraw?: () => void;
}

Expand All @@ -50,7 +51,9 @@ const Flow: ForwardRefRenderFunction<unknown, FlowProps> = ({ children }, ref) =
const [nodes, , onNodesChange] = useNodesState(flowNodes);
const [, setEdges, onEdgesChange] = useEdgesState(flowEdges);
const { fitView } = useReactFlow();
const [redraw, setRedraw] = useState(false);
const reset = useRef(false);
const [redrawing, setRedrawing] = useState(false);
const [rfInstance, setRfInstance] = useState<ReactFlowInstance | null>();

// temp fix see: https://github.com/xyflow/xyflow/issues/3243
const store = useStoreApi();
Expand All @@ -63,6 +66,19 @@ const Flow: ForwardRefRenderFunction<unknown, FlowProps> = ({ children }, ref) =
}

useEffect(() => {
let flowZoom = 0.5;
if (rfInstance && reset.current === false) {
const obj = rfInstance.toObject();
if (obj.viewport.zoom) {
flowZoom = obj.viewport.zoom;
}
}

if (flowZoom > 0.5) {
return;
}

// Only fit view if the user has not zoomed in
fitView();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [fitView, nodes]);
Expand All @@ -75,16 +91,27 @@ const Flow: ForwardRefRenderFunction<unknown, FlowProps> = ({ children }, ref) =
setEdges(edges);
},
redraw: () => {
setRedraw(true);
runLayout();
reset.current = true;
const { edges } = getData();
setEdges(edges);
setRedrawing(true);
const reLayout = async () => {
await runLayout();
setRedrawing(false);
};

// Add a small delay to visually indicate the redraw
setTimeout(() => {
setRedraw(false);
}, 300);
reLayout();
}, 200);

setTimeout(() => {
reset.current = false;
}, 2000);
},
}));

if (redraw) {
if (redrawing) {
return <Loading />;
}

Expand All @@ -102,6 +129,10 @@ const Flow: ForwardRefRenderFunction<unknown, FlowProps> = ({ children }, ref) =
edges={flowEdges}
onEdgesChange={onEdgesChange}
defaultEdgeOptions={edgeOptions}
onInit={(instance) => {
// Keep a reference to the instance so we can check zoom level for fitView.
setRfInstance(instance);
}}
>
<Controls showInteractive={false} showZoom={true} showFitView={false} />
<Background />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { createContext, useContext, useRef, MutableRefObject } from "react";

export type FlowRefHandle = {
updateEdges: () => void;
redraw: () => void;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "@lib/responseDownloadFormats/types";
import { getFullTemplateByID } from "@lib/templates";
import { FormElementTypes, VaultStatus } from "@lib/types";
import { isResponseId, isUUID } from "@lib/validation/validation";
import { isResponseId } from "@lib/validation/validation";
import { listAllSubmissions, retrieveSubmissions, updateLastDownloadedBy } from "@lib/vault";
import { transform as csvTransform } from "@lib/responseDownloadFormats/csv";
import { transform as htmlAggregatedTransform } from "@lib/responseDownloadFormats/html-aggregated";
Expand Down Expand Up @@ -65,7 +65,7 @@ export const fetchSubmissions = async ({
throw new Error("User is not authenticated");
}

if (formId === "0000" || !isUUID(formId)) {
if (formId === "0000") {
return {
submissions: [],
lastEvaluatedKey: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ export const MultiActionSelector = ({
const parent = group?.index;
parent && setGroupNextAction(parent as string, nextActions);
setChangeKey(String(new Date().getTime()));
flow.current?.updateEdges();
flow.current?.redraw();
toast.success(t("logic.actionsSaved"));
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export const SingleActionSelect = ({
currentGroup && setGroupNextAction(currentGroup, nextActionId);
}

flow.current?.updateEdges();
flow.current?.redraw();

toast.success(t("logic.actionsSaved"));
Expand Down

0 comments on commit b3f174f

Please sign in to comment.