Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | 45x 52x 52x 52x 52x 52x 52x 52x 52x 52x 52x 31x 1x 1x 1x 1x 31x 31x 2x 1x 1x 1x 1x 31x 6x 31x 7x 6x 6x 31x 1x 31x 4x 4x 31x 2x 31x 31x 2x 1x 1x 31x 1x 60x 60x 2x | import { Stack } from "@mui/material";
import { ClipboardTextIcon, PlusIcon } from "@phosphor-icons/react";
import {
DndContext,
DragEndEvent,
MouseSensor,
TouchSensor,
closestCorners,
useSensor,
useSensors,
} from "@dnd-kit/core";
import { restrictToParentElement } from "@dnd-kit/modifiers";
import {
SortableContext,
verticalListSortingStrategy,
} from "@dnd-kit/sortable";
import { useFieldArray } from "react-hook-form";
import GiveButton from "@shared/Button/GiveButton";
import GiveSwitch from "@shared/Switch/GiveSwitch";
import GiveText from "@shared/Text/GiveText";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
import { HFGiveInput } from "@shared/HFInputs/HFGiveInput/HFGiveInput";
import { usePayBuilderForm } from "@sections/PayBuilder/provider/PayBuilderFormProvider";
import { useGetFeatureFlagValues } from "FeatureFlags/useGetFeatureFlagValues";
import { FormDataType } from "@sections/PayBuilder/utils";
import CustomFieldRow from "./CustomFieldRow";
import { useCustomFieldsEditGuard } from "../hooks/useCustomFieldsEditGuard";
import {
MAX_CUSTOM_FIELDS,
MAX_CUSTOM_FIELDS_TOOLTIP,
MAX_CUSTOM_FIELD_SECTION_TITLE_LEN,
createCustomField,
} from "../customFields.helpers";
const CustomFieldsSection = () => {
const { isPayBuilderCustomFieldsEnabled, isFeatureFlagLoading } =
useGetFeatureFlagValues();
const { methods, setCustomFieldsNeedNewDraft } = usePayBuilderForm();
const { control, watch, setValue } = methods;
// Default keyName "id" is RHF's own stable React/dnd key. Our data property
// `fieldId` is left intact in form state for the customer-form answer keys.
const { fields, append, remove, move, replace } = useFieldArray<
FormDataType,
"Checkout.customFields.fields"
>({
control,
name: "Checkout.customFields.fields",
});
const sensors = useSensors(useSensor(MouseSensor), useSensor(TouchSensor));
const isEnabled = watch("Checkout.customFields.enabled");
const isPublished = watch("publishedStatus") === "public";
const isAtMax = fields.length >= MAX_CUSTOM_FIELDS;
// AC017 — structural edits on a published form warn before applying.
const { guardEdit } = useCustomFieldsEditGuard(isPublished);
// Gate the whole feature at the builder chokepoint (AC-ROLLOUT). While flags
// resolve every flag reads false, so hide until settled to avoid a flash.
if (isFeatureFlagLoading || !isPayBuilderCustomFieldsEnabled) return null;
const applyToggle = (checked: boolean) => {
setValue("Checkout.customFields.enabled", checked, { shouldDirty: true });
Eif (checked && fields.length === 0) {
// AC004 — toggling on auto-adds the first field.
append(createCustomField(1));
}
Iif (!checked) {
// AC003 — disabling clears the fields (keeps enabled ⇔ ≥1 field).
replace([]);
}
};
const applyAdd = () => append(createCustomField(fields.length + 1));
const applyDelete = (index: number) => {
// AC008 — immediate, no confirmation.
if (fields.length <= 1) {
// AC009 — deleting the last field turns the toggle off.
replace([]);
setValue("Checkout.customFields.enabled", false, { shouldDirty: true });
return;
}
remove(index);
};
// AC018 — a structural edit on a *published* form marks the session so the
// next Save forks a brand-new draft copy (the source form stays live); on a
// draft it edits in place (AC016). The actual save is deferred to the Save
// chokepoint (useManagePayFormProvider) — a structural edit never fires its
// own network save (that raced the RHF state flush and duplicated drafts).
const markPendingNewDraft = () => {
if (isPublished) setCustomFieldsNeedNewDraft?.(true);
};
// Discrete structural edits (toggle / add / delete / reorder): warn once on a
// published form (AC017), then apply and mark. On Discard nothing changes.
const runStructuralEdit = (apply: () => void) =>
guardEdit(() => {
apply();
markPendingNewDraft();
});
const handleToggle = (checked: boolean) =>
runStructuralEdit(() => applyToggle(checked));
const handleAdd = () => {
Iif (isAtMax) return;
runStructuralEdit(applyAdd);
};
const handleDelete = (index: number) =>
runStructuralEdit(() => applyDelete(index));
const handleDragEnd = (event: DragEndEvent) => {
const { active, over } = event;
if (!over || active.id === over.id) return;
const oldIndex = fields.findIndex((f) => f.id === active.id);
const newIndex = fields.findIndex((f) => f.id === over.id);
if (oldIndex !== -1 && newIndex !== -1)
runStructuralEdit(() => move(oldIndex, newIndex));
};
// AC017 — a label edit on a published form applies immediately (free text; no
// frozen input) but still warns once and marks the session, so editing a
// label never patches the live form in place.
const handleLabelEditIntent = () => {
if (!isPublished) return;
setCustomFieldsNeedNewDraft?.(true);
guardEdit(() => undefined);
};
return (
<Stack gap="12px" data-testid="custom-fields-section">
<Stack
direction="row"
spacing={2}
alignItems="center"
justifyContent="space-between"
>
<Stack direction="row" spacing={2} alignItems="center">
<ClipboardTextIcon size={24} />
<GiveText variant="bodyL">Custom Fields</GiveText>
</Stack>
<GiveSwitch
checked={!!isEnabled}
onChange={(e) => handleToggle(e.target.checked)}
inputProps={{
"aria-label": "Toggle custom fields",
// data-testid on the input (the role=checkbox element)
...{ "data-testid": "custom-fields-toggle" },
}}
/>
</Stack>
{isEnabled && (
<Stack gap="16px">
<HFGiveInput
name="Checkout.customFields.sectionTitle"
label="Section Title"
placeholder="Additional Information"
maxLength={MAX_CUSTOM_FIELD_SECTION_TITLE_LEN}
/>
<DndContext
sensors={sensors}
collisionDetection={closestCorners}
onDragEnd={handleDragEnd}
modifiers={[restrictToParentElement]}
>
<SortableContext
items={fields.map((f) => f.id)}
strategy={verticalListSortingStrategy}
>
<Stack gap="12px">
{fields.map((field, index) => (
<CustomFieldRow
key={field.id}
sortableId={field.id}
index={index}
onDelete={() => handleDelete(index)}
onLabelEditIntent={handleLabelEditIntent}
/>
))}
</Stack>
</SortableContext>
</DndContext>
<GiveTooltip
title={MAX_CUSTOM_FIELDS_TOOLTIP}
placement="top"
disableHoverListener={!isAtMax}
// GiveTooltip wraps its child in a centered Stack by default; keep
// the button left-aligned below the field cards (reference design).
alignment="flex-start"
>
<span>
<GiveButton
variant="filled"
color="light"
size="small"
label="Add Custom Field"
startIcon={<PlusIcon size={18} />}
disabled={isAtMax}
onClick={handleAdd}
data-testid="add-custom-field"
/>
</span>
</GiveTooltip>
</Stack>
)}
</Stack>
);
};
export default CustomFieldsSection;
|