Skip to content

Commit

Permalink
chore: Add temporary fix for AutoFlow issues (#3886)
Browse files Browse the repository at this point in the history
add temporary fix for autoflow issues
  • Loading branch information
timarney authored Jun 24, 2024
1 parent e2eefcd commit ed4c282
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { KeyboardNavTip } from "./KeyboardNavTip";
import { Button } from "@clientComponents/globals";
import { Language } from "@lib/types/form-builder-types";
import { isTitleElementType } from "./util/itemType";
import { useAutoFlowIfNoCustomRules } from "@lib/hooks/useAutoFlowAll";

export interface TreeDataProviderProps {
children?: ReactElement;
Expand Down Expand Up @@ -90,6 +91,8 @@ const ControlledTree: ForwardRefRenderFunction<unknown, TreeDataProviderProps> =
const [selectedItems, setSelectedItems] = useState<TreeItemIndex[]>([]);

const { getTitle } = useElementTitle();
const { autoFlowAll } = useAutoFlowIfNoCustomRules();

const newSectionText = t("groups.newSection");

const addSection = () => {
Expand Down Expand Up @@ -241,7 +244,7 @@ const ControlledTree: ForwardRefRenderFunction<unknown, TreeDataProviderProps> =

setSelectedItems([item.index]);
}}
onDrop={async (items: TreeItem[], target: DraggingPosition) =>
onDrop={async (items: TreeItem[], target: DraggingPosition) => {
handleOnDrop(
items,
target,
Expand All @@ -252,9 +255,10 @@ const ControlledTree: ForwardRefRenderFunction<unknown, TreeDataProviderProps> =
expandedItems,
getTreeData,
getConfirmMovePromise,
setOpenConfirmMoveDialog
)
}
setOpenConfirmMoveDialog,
autoFlowAll
);
}}
onFocusItem={(item) => {
setFocusedItem(item.index);
const parent = findParentGroup(getTreeData(), String(item.index));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ export const handleOnDrop = async (
expandedItems: TreeItemIndex[],
getTreeData: () => TreeItems,
getPromise: () => Promise<boolean>,
setOpenDialog: (value: boolean) => void
setOpenDialog: (value: boolean) => void,
autoFlowAll: () => void
) => {
// Current state of the tree in Groups format
let currentGroups = getGroups() as GroupsType;
Expand Down Expand Up @@ -178,6 +179,9 @@ export const handleOnDrop = async (
replaceGroups(newGroups);
setSelectedItems(selectedItems);

// Temporary solution: If no custom rules are present, autoFlow all items
autoFlowAll();

return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ export const autoFlowAllNextActions = (formGroups: GroupsType, force: boolean =
return formGroups;
};

/**
* Check if any of the groups have custom rules set.
* @param items
* @returns boolean
*/
export const groupsHaveCustomRules = (items: Group[]) => {
return items.some((item) => Object.hasOwn(item, "autoFlow") && !item.autoFlow);
};

/**
* Autoflow: set the nextAction for both the provided groupId and the previous group
* in the object to maintain a linear flow.
Expand Down
25 changes: 25 additions & 0 deletions lib/hooks/useAutoFlowAll.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { autoFlowAllNextActions } from "@formBuilder/components/shared/right-panel/treeview/util/setNextAction";
import { useGroupStore } from "@formBuilder/components/shared/right-panel/treeview/store/useGroupStore";
import { GroupsType } from "@lib/formContext";
import { groupsHaveCustomRules } from "@formBuilder/components/shared/right-panel/treeview/util/setNextAction";

export const useAutoFlowIfNoCustomRules = () => {
const { getGroups, replaceGroups } = useGroupStore((s) => {
return {
getGroups: s.getGroups,
replaceGroups: s.replaceGroups,
};
});

const autoFlowAll = () => {
const groups = getGroups() as GroupsType;
const hasCustomRules = groupsHaveCustomRules(Object.values(groups));

if (!hasCustomRules) {
const newGroups = autoFlowAllNextActions({ ...groups }, true);
replaceGroups(newGroups);
}
};

return { autoFlowAll };
};

0 comments on commit ed4c282

Please sign in to comment.