All files / src/sections/PayBuilder/Checkout/components ParticipantRow.tsx

100% Statements 15/15
100% Branches 12/12
100% Functions 7/7
100% Lines 15/15

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                                                                        13x                         61x 61x     61x   61x                                                                   3x                           2x 2x                       50x             2x 2x                             51x             1x 1x 1x                                                                
import { Collapse, Stack } from "@mui/material";
import { CaretDownIcon, CaretUpIcon } from "@phosphor-icons/react";
import { Controller, useFormContext } from "react-hook-form";
import GiveText from "@shared/Text/GiveText";
import GiveIconButton from "@shared/IconButton/GiveIconButton";
import { GiveInput } from "@shared/GiveInputs/GiveInput";
import { HFGiveTelephone } from "@shared/HFInputs/HFGiveTelephone/HFGiveTelephone";
import { usePayBuilderForm } from "@sections/PayBuilder/provider/PayBuilderFormProvider";
import { useAppTheme } from "@theme/v2/Provider";
import WithCheckoutTooltip from "./CheckoutTooltip";
 
interface ParticipantRowProps {
  /** Form path prefix, e.g. `participants.77:0` — the reconciliation key. */
  fieldKey: string;
  /** `Ticket 1`, or plain `Ticket` for the single order-level participant. */
  title: string;
  /** The variant this unit belongs to, e.g. `Dinner & Movie`. */
  subtitle?: string;
  isExpanded: boolean;
  onToggle: () => void;
  showPhone?: boolean;
  isPhoneRequired?: boolean;
  isDisabledFields?: boolean;
  /** Rendered in the header — the "Same as contact details" switch (AC054). */
  headerAction?: React.ReactNode;
  /** Un-checks "Send all tickets to this email", so an edit affects this row only. */
  onEmailEdit?: () => void;
  /** Un-checks "Same as contact details", so an edit breaks the link and sticks. */
  onFieldEdit?: () => void;
}
 
/**
 * One collapsible participant. Extracted from ParticipantDetails so the section
 * stays about "how many rows and which mode", and the row stays about "one
 * participant's fields".
 */
const ParticipantRow = ({
  fieldKey,
  title,
  subtitle,
  isExpanded,
  onToggle,
  showPhone,
  isPhoneRequired,
  isDisabledFields,
  headerAction,
  onEmailEdit,
  onFieldEdit,
}: ParticipantRowProps) => {
  const { palette } = useAppTheme();
  const methods = useFormContext();
  const {
    parsedValues: { checkoutInputStyles },
  } = usePayBuilderForm();
 
  return (
    <Stack
      border={`1px solid ${palette.border?.secondary}`}
      borderRadius="12px"
      padding="16px"
      spacing={1.5}
      data-testid={`participant-row-${fieldKey}`}
    >
      {/* Clicking anywhere on the header toggles the row, but the caret button
          below is the real control: no button role out here, so the switch in
          headerAction is never an interactive nested inside one. */}
      <Stack
        direction="row"
        alignItems="center"
        justifyContent="space-between"
        spacing={2}
        onClick={onToggle}
        sx={{ cursor: "pointer" }}
      >
        {/* Mockup 7409:188901 — the variant name sits UNDER the ticket label,
            not beside it. Figma vars: title Desktop/H5 (20/24) on
            Colour/Text/primary, subtitle Desktop/Body S (14/20) on
            Colour/Text/secondary. */}
        <Stack spacing={0.5} flexGrow={1}>
          <GiveText variant="h5">{title}</GiveText>
          {subtitle && (
            <GiveText variant="bodyS" color="secondary">
              {subtitle}
            </GiveText>
          )}
        </Stack>
        <Stack direction="row" alignItems="center" spacing={1.5}>
          {headerAction && (
            <Stack
              onClick={(event) => event.stopPropagation()}
              sx={{ cursor: "default" }}
            >
              {headerAction}
            </Stack>
          )}
          <GiveIconButton
            variant="ghost"
            size="small"
            Icon={isExpanded ? CaretUpIcon : CaretDownIcon}
            aria-label={`${title} details`}
            aria-expanded={isExpanded}
            // The header's own handler would toggle a second time and undo this.
            onClick={(event) => {
              event.stopPropagation();
              onToggle();
            }}
          />
        </Stack>
      </Stack>
 
      <Collapse in={isExpanded} unmountOnExit>
        <Stack spacing={2} paddingTop={1}>
          <Controller
            control={methods.control}
            name={`${fieldKey}.name`}
            render={({ field: { onChange, value }, fieldState: { error } }) => (
              <WithCheckoutTooltip disabled={isDisabledFields}>
                <GiveInput
                  name={`${fieldKey}.name`}
                  fullWidth
                  label="Full Name"
                  value={value ?? ""}
                  onChange={(event) => {
                    onFieldEdit?.();
                    onChange(event);
                  }}
                  error={!!error}
                  helperText={error?.message}
                  disabled={isDisabledFields}
                  sx={checkoutInputStyles}
                  data-testid={`participant-name-${fieldKey}`}
                />
              </WithCheckoutTooltip>
            )}
          />
          <Controller
            control={methods.control}
            name={`${fieldKey}.email`}
            render={({ field: { onChange, value }, fieldState: { error } }) => (
              <WithCheckoutTooltip disabled={isDisabledFields}>
                <GiveInput
                  name={`${fieldKey}.email`}
                  fullWidth
                  label="Email"
                  value={value ?? ""}
                  onChange={(event) => {
                    onFieldEdit?.();
                    onEmailEdit?.();
                    onChange(event);
                  }}
                  error={!!error}
                  helperText={error?.message}
                  disabled={isDisabledFields}
                  sx={checkoutInputStyles}
                  data-testid={`participant-email-${fieldKey}`}
                />
              </WithCheckoutTooltip>
            )}
          />
          {showPhone && (
            <WithCheckoutTooltip disabled={isDisabledFields}>
              <HFGiveTelephone
                label="Phone Number"
                name={`${fieldKey}.phoneNumber`}
                required={isPhoneRequired}
                disabled={isDisabledFields}
                sx={checkoutInputStyles}
                // onValueChange, not a DOM event: the country dropdown changes
                // the value without one, and would slip past the link break.
                onValueChange={onFieldEdit}
              />
            </WithCheckoutTooltip>
          )}
        </Stack>
      </Collapse>
    </Stack>
  );
};
 
export default ParticipantRow;