All files / src/sections/PayBuilder/views/events/components EventDateLocation.tsx

88.09% Statements 37/42
74.64% Branches 53/71
88.88% Functions 8/9
87.17% Lines 34/39

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 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318                                                                                              19x                     27x     27x 27x 27x 27x         27x   27x               27x             27x   26x                                                 19x             2x   2x         2x                     1x                                                               19x                   28x   28x                           28x                                                                                               19x                     28x 28x 28x 28x 28x 28x   28x   27x             27x       27x                                                         19x 127x 54x                      
import { Stack, SxProps, Box } from "@mui/material";
import GiveText from "@shared/Text/GiveText";
import { styled, useAppTheme } from "@theme/v2/Provider";
import {
  CalendarBlankIcon,
  CaretDownIcon,
  CaretUpIcon,
  GlobeSimpleIcon,
  MapPinIcon,
} from "@phosphor-icons/react";
import {
  formatMapAddress,
  useFormatPaymentFormDate,
  useGetRedesignedFormTypes,
} from "@sections/PayBuilder/utils";
import { convertHexToRGB } from "@utils/theme";
import useCheckFormType from "@sections/PayBuilder/components/hooks/useCheckFormType";
import { useState } from "react";
import EventMap from "./EventMap";
import { usePayBuilderContext } from "@sections/PayBuilder/provider/PayBuilderContext";
 
interface Props {
  startDate: string | Date | null;
  endDate: string | Date | null;
  includeTime: boolean;
  locationURL: string | null;
  locations: any;
  tabsDetails: any;
  accentColor?: string;
  timers: any;
  sx?: SxProps;
  adaptiveColorToBackground?: string;
}
export type DATE_DATE_PROPS = {
  startDate: string | Date | null;
  endDate: string | Date | null;
  accentColor?: string;
  includeTime: boolean;
  timers: any;
};
 
export type LOCATION_DATA_PROPS = {
  locationURL: string | null;
  locationPosition: any;
  accentColor?: string;
  address: any;
};
const EventDateLocation = ({
  startDate,
  endDate,
  includeTime,
  locationURL,
  locations,
  tabsDetails,
  accentColor = "inherit",
  timers,
  adaptiveColorToBackground,
}: Props) => {
  const theme = useAppTheme();
  const {
    screenStates: { isMobileView },
  } = usePayBuilderContext();
  const { isManualAddress, locationPosition } = tabsDetails;
  const { locationAddress, locationShortAddress } = locations || {};
  const address = isManualAddress
    ? formatMapAddress(locationAddress)
    : locationShortAddress;
 
  const showMap =
    locationShortAddress && locationPosition === "onsite" && !isManualAddress;
 
  const dateData = {
    accentColor,
    endDate,
    startDate,
    includeTime,
    timers,
  } as DATE_DATE_PROPS;
 
  const locationData = {
    locationURL,
    locationPosition,
    accentColor,
    address,
  };
 
  if (!startDate && !address) return null;
 
  return (
    <Stack
      sx={{
        border: `1px solid ${theme.palette.border?.primary}`,
        borderRadius: "16px",
        width: isMobileView ? "100%" : "375px",
      }}
    >
      <Stack spacing={2} sx={{ padding: "20px" }}>
        <DateComponent
          dateData={dateData}
          adaptiveColorToBackground={adaptiveColorToBackground}
        />
        <LocationComponent
          data={locationData}
          adaptiveColorToBackground={adaptiveColorToBackground}
        />
      </Stack>
      {showMap && (
        <ShowHideMap locations={locations} accentColor={accentColor} />
      )}
    </Stack>
  );
};
 
const ShowHideMap = ({
  locations,
  accentColor,
}: {
  locations: any;
  accentColor: string;
}) => {
  const [isMapDisplayed, setIsMapDisplayed] = useState(false);
 
  const iconProps = {
    size: 14,
    color: accentColor,
  };
 
  return (
    <>
      <Stack
        paddingLeft="64px"
        direction="row"
        alignItems="center"
        spacing={1}
        sx={{
          cursor: "pointer",
          paddingBottom: "20px",
        }}
        onClick={() => setIsMapDisplayed((prev) => !prev)}
      >
        <GiveText variant="bodyS" sx={{ color: accentColor }}>
          {isMapDisplayed ? "Hide" : "Show"} map
        </GiveText>
        {isMapDisplayed ? (
          <CaretUpIcon {...iconProps} />
        ) : (
          <CaretDownIcon {...iconProps} />
        )}
      </Stack>
      {isMapDisplayed && (
        <EventMap
          locations={locations}
          hideTitle
          mapContainerPadding="0px"
          containerStyle={{
            height: "127px",
            width: "100%",
            borderRadius: "16px",
          }}
        />
      )}
    </>
  );
};
 
interface LOCATION_PROPS {
  data: LOCATION_DATA_PROPS;
  adaptiveColorToBackground?: string;
  hideIcon?: boolean;
}
export const LocationComponent = ({
  data,
  adaptiveColorToBackground,
  hideIcon = false,
}: LOCATION_PROPS) => {
  const {
    locationURL,
    locationPosition,
    accentColor = "inherit",
    address,
  } = data || {};
 
  const openURL = () => {
    if (locationURL) {
      let urlToOpen = locationURL;
 
      if (
        !urlToOpen.startsWith("http://") &&
        !urlToOpen.startsWith("https://")
      ) {
        urlToOpen = "https://" + urlToOpen;
      }
 
      window.open(urlToOpen, "_blank", "noopener");
    }
  };
  return (
    <Stack
      direction="row"
      gap="11px"
      alignItems="center"
      sx={{
        cursor: locationURL ? "pointer" : "default",
      }}
      onClick={openURL}
    >
      {locationURL && locationPosition === "online" && (
        <>
          {!hideIcon && (
            <IconBox accentColor={accentColor}>
              <GlobeSimpleIcon color={accentColor} />
            </IconBox>
          )}
          <GiveText
            variant="bodyS"
          >
            {locationURL}
          </GiveText>
        </>
      )}
      {address && locationPosition === "onsite" && (
        <>
          {!hideIcon && (
            <IconBox accentColor={accentColor}>
              <MapPinIcon color={accentColor} />
            </IconBox>
          )}
          <GiveText
            variant="bodyS"
          >
            {address}
          </GiveText>
        </>
      )}
    </Stack>
  );
};
 
export default EventDateLocation;
export interface DateComponentProps {
  dateData: DATE_DATE_PROPS;
  adaptiveColorToBackground?: string;
  hideIcon?: boolean;
}
export const DateComponent = ({
  dateData,
  adaptiveColorToBackground,
  hideIcon = false,
}: DateComponentProps) => {
  const {
    startDate,
    endDate,
    accentColor = "inherit",
    includeTime,
    timers,
  } = dateData || {};
  const { startsAtTime, endsAtTime } = timers || {};
  const { formType } = useCheckFormType();
  const { formatPaymentFormDate } = useFormatPaymentFormDate();
  const redesignedFormTypes = useGetRedesignedFormTypes();
  const isRedesigned = redesignedFormTypes.includes(formType);
 
  if (!startDate && !endDate) return null;
 
  const formattedStartDate = formatPaymentFormDate(
    startDate,
    // For redesigned form we don't show start time unless it's period
    isRedesigned && !endDate ? undefined : includeTime,
    startsAtTime,
  );
 
  const formattedEndDate = endDate
    ? `to ${formatPaymentFormDate(endDate, includeTime, endsAtTime)}`
    : "";
 
  return (
    <>
      <Stack direction="row" gap="11px" alignItems="center">
        {!hideIcon && (
          <IconBox accentColor={accentColor}>
            <CalendarBlankIcon color={accentColor} />
          </IconBox>
        )}
        <Stack>
          <GiveText
            data-testid="event-date"
            variant="bodyS"
          >
            {endDate && "From "}
            {formattedStartDate?.toString()} {endDate && formattedEndDate}
          </GiveText>
          {isRedesigned && !endDate && includeTime && startsAtTime && (
            <GiveText
              variant="bodyS"
            >
              {startsAtTime}
            </GiveText>
          )}
        </Stack>
      </Stack>
    </>
  );
};
 
const IconBox = styled(Box, {
  shouldForwardProp: (prop) => prop !== "accentColor",
})<{ accentColor: string }>(({ accentColor }) => ({
  borderRadius: "30px",
  minWidth: "32px",
  width: "32px",
  maxHeight: "32px",
  height: "32px",
  background: convertHexToRGB(accentColor, 0.1),
  display: "flex",
  justifyContent: "center",
  alignItems: "center",
}));