Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions designer/client/AdapterDesigner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {AdapterVisualisation} from "./components/Visualization";
import {AdapterFormDefinition} from "@communitiesuk/model";
import {AdapterDataContext} from "./context/AdapterDataContext";
import AdapterMenu from "./components/menu/AdapterMenu";
import {sortConditionsBySourceFieldOrder} from "./utils/conditionOrdering";

interface Props {
match?: any;
Expand Down Expand Up @@ -54,13 +55,14 @@ export default class AdapterDesigner extends Component<Props, State> {
this.setState({flyoutCount: --currentCount}, callback());
};

save = async (toUpdate, callback = () => {
}) => {
save = async (toUpdate, callback = () => {}) => {
try {
await this.designerApi.save(this.id, toUpdate);
const sortedData = sortConditionsBySourceFieldOrder(toUpdate);

await this.designerApi.save(this.id, sortedData);
// @ts-ignore
this.setState({data: toUpdate, updatedAt: new Date().toLocaleTimeString(), error: undefined,}, callback());
return toUpdate;
this.setState({data: sortedData, updatedAt: new Date().toLocaleTimeString(), error: undefined,}, callback());
return sortedData;
Comment on lines +58 to +65
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

v unfamiliar with this codebase sorry. Is the broken behaviour coming from XGovFormBuilder, or from something we've layered on top of it?

If it's broken in XGFB and we're patching a fix in here - should we instead be trying to fix it upstream, get that merged, and then pull it in ourselves?

} catch (e) {
//@ts-ignore
this.setState({error: e.message});
Expand Down
100 changes: 100 additions & 0 deletions designer/client/utils/conditionOrdering.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { AdapterFormDefinition } from "@communitiesuk/model";

export function sortConditionsBySourceFieldOrder(data: AdapterFormDefinition): AdapterFormDefinition {
const sortedData = { ...data };

sortedData.pages = data.pages.map(page => {
if (!page.next || page.next.length === 0) {
return page;
}

// Find the source page that these conditions reference
const sourcePagePath = getConditionSourcePage(page.next, data);
if (!sourcePagePath) {
return page; // No conditions to sort
}

const sourcePage = data.pages.find(p => p.path === sourcePagePath);
if (!sourcePage) {
return page;
}

// Get the order of options from the source field (checkboxes, etc.)
const fieldOptionOrder = getFieldOptionOrder(sourcePage, data);

// Sort conditions based on the order of options in the source field
const sortedNext = [...page.next].sort((a, b) => {
// Non-conditional links first
if (!a.condition && !b.condition) return 0;
if (!a.condition) return -1;
if (!b.condition) return 1;

// Get the option values these conditions check for
const aOptionValue = getConditionOptionValue(a.condition, data);
const bOptionValue = getConditionOptionValue(b.condition, data);

// Sort by the order they appear in the source field
const aIndex = fieldOptionOrder.indexOf(aOptionValue);
const bIndex = fieldOptionOrder.indexOf(bOptionValue);

// If both found, sort by their field order
if (aIndex !== -1 && bIndex !== -1) {
return aIndex - bIndex;
}

// Fallback to alphabetical for any not found
return a.path.localeCompare(b.path);
});

return {
...page,
next: sortedNext
};
});

return sortedData;
}

function getConditionSourcePage(nextLinks: any[], data: AdapterFormDefinition): string | null {
// Find the first conditional link and determine what page it's checking
const conditionalLink = nextLinks.find(link => link.condition);
if (!conditionalLink) return null;

const condition = data.conditions?.find(c => c.name === conditionalLink.condition);
if (!condition?.value?.conditions?.[0]?.field?.name) return null;

// Extract page path from field name (e.g., "FabDefault.ZRERCV" -> page with component "ZRERCV")
const fieldName = condition.value.conditions[0].field.name;
const componentName = fieldName.includes('.') ?
fieldName.split('.').pop() : fieldName;

// Find the page that contains this component
return data.pages.find(page =>
page.components?.some(component => component.name === componentName)
)?.path || null;
}

function getFieldOptionOrder(page: any, data: AdapterFormDefinition): string[] {
// Find the checkboxes field (or other multi-select field)
const checkboxField = page.components?.find(component =>
component.type === 'CheckboxesField' ||
component.type === 'RadiosField'
);

if (!checkboxField) return [];

// If it uses a list reference, get the list
if (checkboxField.values?.type === 'listRef') {
const listName = checkboxField.values.list || checkboxField.list;
const list = data.lists?.find(l => l.name === listName);
return list?.items?.map(item => item.value) || [];
}

// If it has inline items
return checkboxField.items?.map(item => item.value) || [];
}

function getConditionOptionValue(conditionName: string, data: AdapterFormDefinition): string {
const condition = data.conditions?.find(c => c.name === conditionName);
return condition?.value?.conditions?.[0]?.value?.value || '';
}
Loading