All files / src/features/Minibuilders/EventsMinibuilder EventDateLocation.tsx

0% Statements 0/31
0% Branches 0/42
0% Functions 0/15
0% Lines 0/30

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 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
import * as React from "react";
import { palette } from "@palette";
import { Controller, useFormContext } from "react-hook-form";
// mui
import Box from "@mui/material/Box";
import Grid from "@mui/material/Grid";
import Stack from "@mui/material/Stack";
import TextField from "@mui/material/TextField";
import { useTheme } from "@mui/material/styles";
import useMediaQuery from "@mui/material/useMediaQuery";
// components
import { Text } from "@common/Text";
import { RHFInput } from "@common/Input";
import { RHFSelect } from "@common/Select";
import { RHFCheckbox } from "@common/Checkbox";
import { DatePicker } from "@common/DatePickers";
import { GoogleMaps, GooglePlacesAutocomplete } from "@services/google-api";
// hooks
import useLoadGoogleScript from "@hooks/google-api/useLoadGoogleScript";
import { getUSNames } from "@utils/country_dial_codes";
// localization
import { useTranslation } from "react-i18next";
import { namespaces } from "localization/resources/i18n.constants";
import { ZipCodeInput } from "@common/AddressInputs";
import LoadingSpinner from "@components/Snipper/LoadingSpinner";
import { DateType, LocationType } from "./types";
import { ChevronRight } from "@assets/icons/RebrandedIcons";
import useCountryCodes from "@hooks/common/useCountryCodes";
import FadeUpWrapper from "@components/animation/FadeUpWrapper";
import ErrorCatcher from "@common/Error/ErrorCatcher";
 
const EventDateLocation = ({
  isModalMounted,
}: {
  isModalMounted?: boolean;
}) => {
  const theme = useTheme();
  const isDesktop = useMediaQuery(theme.breakpoints.up("sm"));
  const { isLoaded, loadError } = useLoadGoogleScript();
 
  const { selectCountriesList } = useCountryCodes();
  const showStateFieldCountry = "US";
  const { t } = useTranslation(namespaces.pages.eventsMinibuilder);
 
  const {
    watch,
    setValue,
    control,
    clearErrors,
    formState: { errors },
  } = useFormContext();
  const values = watch();
 
  const handleAddress = () => {
    clearErrors("location.location");
    setValue(
      "date_and_location.location.manual",
      !values.date_and_location.location.manual,
    );
  };
 
  const handleLocationChange = (value: string) => {
    if (values.date_and_location.location.location === value) return;
    setValue("date_and_location.location.location", value, {
      shouldDirty: true,
    });
  };
 
  return (
    <Box sx={{ width: "100%" }}>
      <Stack spacing={1}>
        <FadeUpWrapper delay={100}>
          <Stack
            mb={3}
            gap={2}
            direction={isDesktop ? "row" : "column"}
            alignItems={isDesktop ? "center" : "normal"}
            justifyContent={isDesktop ? "space-between" : "flex-start"}
          >
            <Text fontSize={32} fontWeight="book" color={palette.neutral[80]}>
              When?
            </Text>
 
            <Stack direction="row" alignItems="center" sx={buttonsStyle}>
              <ButtonSwitch
                title="one day"
                label={t("one_day")}
                currentTitle={values.date_and_location.date.type}
                isDesktop={isDesktop}
                onClick={() =>
                  setValue("date_and_location.date.type", "one day")
                }
              />
              <ButtonSwitch
                title="period"
                label={t("period")}
                currentTitle={values.date_and_location.date.type}
                isDesktop={isDesktop}
                onClick={() =>
                  setValue("date_and_location.date.type", "period")
                }
              />
            </Stack>
          </Stack>
        </FadeUpWrapper>
        <FadeUpWrapper delay={200}>
          {values.date_and_location.date.type === "one day" ? (
            <Controller
              name="date_and_location.date.day"
              control={control}
              render={({ field: { ref, ...rest }, fieldState: { error } }) => (
                <DatePicker
                  {...rest}
                  minDate={new Date().setDate(new Date().getDate() + 1)}
                  inputRef={ref}
                  inputFormat="MM/dd/yyyy"
                  inputFormatTime="MM/dd/yyyy 'at' hh:mm a"
                  time={values.date_and_location.date.include_time}
                  adornmentPosition="end"
                  renderInput={({ inputProps, ...params }) => (
                    <TextField
                      inputRef={ref}
                      {...params}
                      error={!!error}
                      label={t("when_is_the_event")}
                      helperText={error?.message}
                      fullWidth
                      inputProps={{
                        ...inputProps,
                        placeholder: t("choose_a_date"),
                      }}
                    />
                  )}
                />
              )}
            />
          ) : (
            <Stack gap={2} direction={isDesktop ? "row" : "column"}>
              <Box width="100%">
                <Controller
                  name="date_and_location.date.startDate"
                  control={control}
                  rules={{
                    required: t("start_date_is_required"),
                  }}
                  render={({
                    field: { ref, ...rest },
                    fieldState: { error },
                  }) => (
                    <DatePicker
                      {...rest}
                      minDate={new Date().setDate(new Date().getDate() + 1)}
                      time={values.date_and_location.date.include_time}
                      inputFormatTime="MM/dd/yyyy 'at' hh:mm a"
                      inputFormat="MM/dd/yyyy"
                      adornmentPosition="end"
                      renderInput={({ inputProps, ...params }) => (
                        <TextField
                          name="date_and_location.date.startDate"
                          inputRef={ref}
                          {...params}
                          fullWidth
                          error={!!error}
                          label={t("when_the_event_starts")}
                          helperText={error?.message}
                          inputProps={{
                            ...inputProps,
                            placeholder: t("select_a_date"),
                          }}
                        />
                      )}
                    />
                  )}
                />
              </Box>
              <Box width="100%">
                <Controller
                  name="date_and_location.date.endDate"
                  control={control}
                  rules={{}}
                  render={({
                    field: { ref, ...rest },
                    fieldState: { error },
                  }) => (
                    <DatePicker
                      {...rest}
                      minDate={new Date().setDate(new Date().getDate() + 1)}
                      time={values.date_and_location.date.include_time}
                      inputFormat="MM/dd/yyyy"
                      inputFormatTime="MM/dd/yyyy 'at' hh:mm a"
                      adornmentPosition="end"
                      renderInput={({ inputProps, ...params }) => (
                        <TextField
                          name="date_and_location.date.endDate"
                          inputRef={ref}
                          {...params}
                          fullWidth
                          label={t("when_the_event_ends")}
                          error={!!error}
                          helperText={error?.message}
                          inputProps={{
                            ...inputProps,
                            placeholder: t("select_a_date"),
                          }}
                        />
                      )}
                    />
                  )}
                />
              </Box>
            </Stack>
          )}
          <RHFCheckbox
            name="date_and_location.date.include_time"
            label={t("include_time")}
            dataTestId="include-time"
          />
        </FadeUpWrapper>
      </Stack>
 
      <FadeUpWrapper delay={250}>
        <Box my={2} width="100%" borderBottom="1px solid #DDDCE5" />
      </FadeUpWrapper>
 
      {/** --------------------------- LOCATION --------------------------- */}
      <Stack>
        <FadeUpWrapper delay={300}>
          <Stack
            mb={3}
            gap={2}
            direction={isDesktop ? "row" : "column"}
            alignItems={isDesktop ? "center" : "normal"}
            justifyContent={isDesktop ? "space-between" : "flex-start"}
          >
            <Text fontSize={32} fontWeight="book" color={palette.neutral[80]}>
              Where?
            </Text>
            <Stack direction="row" alignItems="center" sx={buttonsStyle}>
              <ButtonSwitch
                title="in person"
                label={t("in_person")}
                currentTitle={values.date_and_location.location.type}
                isDesktop={isDesktop}
                onClick={() =>
                  setValue("date_and_location.location.type", "in person")
                }
              />
              <ButtonSwitch
                title="online"
                label={t("online")}
                currentTitle={values.date_and_location.location.type}
                isDesktop={isDesktop}
                onClick={() =>
                  setValue("date_and_location.location.type", "online")
                }
              />
            </Stack>
          </Stack>
        </FadeUpWrapper>
 
        <FadeUpWrapper delay={400}>
          {values.date_and_location.location.type === "in person" && (
            <ErrorCatcher errorID="GooglePlacesAutocomplete">
              <GooglePlacesAutocomplete
                disabled={values.date_and_location.location.manual}
                label={t("where_is_the_event")}
                initialValue={values.date_and_location.location.location}
                onValueChange={handleLocationChange}
                reset={isModalMounted}
                isLoaded={isLoaded}
                inputProps={{
                  placeholder: "Type event location",
                  error: Boolean(
                    (errors?.date_and_location as any)?.location?.location,
                  ),
                  helperText: (errors?.date_and_location as any)?.location
                    ?.location?.message,
                }}
                sx={{
                  p: 0,
                  mb: 1.5,
                  width: "100%",
                  "& .MuiInputBase-root": {
                    padding: "7.5px 12px",
                  },
                }}
              />
            </ErrorCatcher>
          )}
 
          {values.date_and_location.location.type === "online" && (
            <RHFInput
              name="date_and_location.location.event_url"
              label={"Enter URL"}
              placeholder={t("enter_a_url")}
              fullWidth
              sx={{ mb: 1.5 }}
            />
          )}
        </FadeUpWrapper>
      </Stack>
 
      <FadeUpWrapper delay={400}>
        {values.date_and_location.location.type === "in person" &&
          !values.date_and_location.location.manual && (
            <Stack direction="column" gap={2}>
              {!isLoaded ? (
                <LoadingSpinner />
              ) : (
                <ErrorCatcher errorID="GoogleMaps">
                  <GoogleMaps
                    address={values.date_and_location.location.location}
                    isLoaded={isLoaded}
                    loadError={loadError}
                  />
                </ErrorCatcher>
              )}
            </Stack>
          )}
      </FadeUpWrapper>
 
      {/** --------------------------- ENTER ADDRESS MANUALLY --------------------------- */}
      <FadeUpWrapper delay={500}>
        {values.date_and_location.location.type === "in person" && (
          <Stack
            direction="row"
            alignItems="center"
            onClick={handleAddress}
            mt={2}
            gap={1}
            sx={{
              cursor: "pointer",
              svg: {
                transition: "all ease .5s",
              },
            }}
          >
            <Text
              fontWeight="book"
              whiteSpace="nowrap"
              color={palette.black[100]}
            >
              {t("enter_address_manually")}
            </Text>
            <ChevronRight
              width={24}
              height={24}
              rotate={values.date_and_location.location.manual ? -90 : 90}
            />
          </Stack>
        )}
 
        {values.date_and_location.location.manual &&
          values.date_and_location.location.type === "in person" && (
            <Grid mt={1} container spacing={2}>
              <Grid item xs={12} sm={6}>
                <RHFSelect
                  name="date_and_location.location.country"
                  label="Country"
                  placeholder="Select a country"
                  options={selectCountriesList}
                  fullWidth
                  disabled
                />
              </Grid>
              <Grid item xs={12}>
                <RHFInput
                  name="date_and_location.location.address"
                  label={t("address")}
                  placeholder={t("type_address")}
                  fullWidth
                />
              </Grid>
 
              <Grid item xs={12} sm={4}>
                <RHFInput
                  name="date_and_location.location.city"
                  label={t("city")}
                  placeholder={t("type_city")}
                  fullWidth
                />
              </Grid>
              <Grid item xs={12} sm={4}>
                {values.date_and_location.location.country ===
                showStateFieldCountry ? (
                  <RHFSelect
                    name="date_and_location.location.state"
                    label={t("state")}
                    placeholder={t("select_state")}
                    fullWidth
                    options={getUSNames().map((state) => ({
                      value: state,
                      label: state,
                    }))}
                  />
                ) : (
                  <RHFInput
                    name="date_and_location.location.state"
                    label="Province"
                    placeholder="Type in your Province"
                    fullWidth
                  />
                )}
              </Grid>
              <Grid item xs={12} sm={4}>
                <ZipCodeInput
                  name="date_and_location.location.zip"
                  label={t("zip")}
                  placeholder={t("type_zip")}
                />
              </Grid>
            </Grid>
          )}
      </FadeUpWrapper>
    </Box>
  );
};
 
const buttonsStyle = {
  borderRadius: "8px",
  border: `1px solid ${palette.neutral[20]}`,
  padding: "4px",
  marginInline: "16px",
  background: "#FFF",
};
 
const ButtonSwitch = ({
  title,
  label,
  currentTitle,
  isDesktop,
  onClick,
}: {
  title: LocationType | DateType;
  label: LocationType | DateType;
  currentTitle: LocationType | DateType;
  isDesktop: boolean;
  onClick?: React.MouseEventHandler<HTMLDivElement>;
}) => {
  return (
    <Box
      sx={{
        width: 144,
        height: 36,
        fontSize: 16,
        fontWeight: 400,
        textAlign: "center",
        padding: "10px 4px",
        position: "relative",
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
        borderRadius: "8px",
        textTransform: "capitalize",
 
        "&:first-of-type": {
          borderRadius: "8px",
        },
 
        ...(title === currentTitle && {
          color: palette.neutral.white,
          background: palette.black.main,
        }),
 
        ...(title !== currentTitle && {
          "&:hover": {
            cursor: "pointer",
          },
        }),
 
        ...(!isDesktop && {
          width: "100%",
        }),
      }}
      onClick={onClick}
    >
      {label}
    </Box>
  );
};
 
export default React.memo(EventDateLocation);