All files / app/features/publicTrips PublicTripDialog.tsx

76.92% Statements 50/65
71.66% Branches 43/60
66.66% Functions 6/9
80% Lines 48/60

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 2885x 5x 5x 5x           5x 5x 5x 5x 5x 5x 5x 5x           5x   5x   5x   12x           12x                                             60x 84x 84x 84x   84x 86x                                               84x         84x 84x 84x 84x   84x   84x 19x 2x                       84x 84x   84x       84x       84x   84x           84x       84x           84x   84x           84x                                 84x 84x                                                                                                                                                       2x     2x 2x                                                                              
import { Checkbox, FormControlLabel, styled, Typography } from "@mui/material";
import Alert from "components/Alert";
import Button from "components/Button";
import Datepicker from "components/Datepicker";
import {
  Dialog,
  DialogActions,
  DialogContent,
  DialogTitle,
} from "components/Dialog";
import TextField from "components/TextField";
import { useCommunity } from "features/communities/hooks";
import { useTranslation } from "i18n";
import { GLOBAL, PUBLIC_TRIPS } from "i18n/namespaces";
import { useEffect } from "react";
import { Controller, useForm, useWatch } from "react-hook-form";
import { Temporal } from "temporal-polyfill";
 
import {
  PublicTrip,
  useCreatePublicTrip,
  useUpdatePublicTrip,
} from "./useListPublicTrips";
 
const DATE_FIELD_ID = "public-trip-dates";
// Must match backend (PUBLIC_TRIP_DESCRIPTION_MIN_LENGTH_UTF16)
const DESCRIPTION_MIN_LENGTH = 150;
 
const FieldStack = styled("div")(({ theme }) => ({
  display: "flex",
  flexDirection: "column",
  gap: theme.spacing(2),
}));
 
const DateRow = styled("div")(({ theme }) => ({
  display: "flex",
  gap: theme.spacing(2),
  [theme.breakpoints.down("sm")]: {
    flexDirection: "column",
  },
}));
 
interface FormValues {
  fromDate: Temporal.PlainDate | null;
  toDate: Temporal.PlainDate | null;
  description: string;
  sameGenderOnly: boolean;
}
 
type PublicTripDialogProps = {
  open: boolean;
  onClose: () => void;
} & (
  | { mode: "create"; communityId: number; communityName: string }
  | { mode: "edit"; trip: PublicTrip }
);
 
export default function PublicTripDialog(props: PublicTripDialogProps) {
  const { open, onClose } = props;
  const { t } = useTranslation([PUBLIC_TRIPS, GLOBAL]);
  const isEdit = props.mode === "edit";
 
  const getDefaults = (): FormValues =>
    props.mode === "edit"
      ? {
          fromDate: props.trip.fromDate
            ? Temporal.PlainDate.from(props.trip.fromDate)
            : null,
          toDate: props.trip.toDate
            ? Temporal.PlainDate.from(props.trip.toDate)
            : null,
          description: props.trip.description,
          sameGenderOnly: props.trip.sameGenderOnly ?? false,
        }
      : {
          fromDate: null,
          toDate: null,
          description: "",
          sameGenderOnly: false,
        };
 
  const {
    control,
    handleSubmit,
    register,
    reset,
    formState: { errors },
  } = useForm<FormValues>({
    mode: "onBlur",
    defaultValues: getDefaults(),
  });
 
  const tripKey = props.mode === "edit" ? props.trip.tripId : null;
  const tripFromDate = props.mode === "edit" ? props.trip.fromDate : null;
  const tripToDate = props.mode === "edit" ? props.trip.toDate : null;
  const tripDescription = props.mode === "edit" ? props.trip.description : null;
  const tripSameGenderOnly =
    props.mode === "edit" ? props.trip.sameGenderOnly : null;
 
  useEffect(() => {
    if (open) {
      reset(getDefaults());
    }
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [
    open,
    tripKey,
    tripFromDate,
    tripToDate,
    tripDescription,
    tripSameGenderOnly,
  ]);
 
  const watchFromDate = useWatch({ control, name: "fromDate" });
  const watchDescription = useWatch({ control, name: "description" }) ?? "";
  const descriptionCharsRemaining =
    DESCRIPTION_MIN_LENGTH - watchDescription.length;
 
  // PublicTrip proto only carries community_id, so fetch the community name in
  // edit mode. Hook is no-op when id is 0 (create mode already has the name).
  const { data: editCommunity } = useCommunity(
    props.mode === "edit" ? props.trip.communityId : 0,
  );
  const communityName =
    props.mode === "create" ? props.communityName : editCommunity?.name;
 
  const handleSuccess = () => {
    if (!isEdit) reset();
    onClose();
  };
 
  // Both hooks are always called (rules of hooks); only the active one fires.
  const createMutation = useCreatePublicTrip(
    props.mode === "create" ? props.communityId : 0,
    handleSuccess,
  );
  const updateMutation = useUpdatePublicTrip(handleSuccess);
 
  const {
    isPending,
    error,
    reset: resetMutation,
  } = isEdit ? updateMutation : createMutation;
 
  const handleClose = () => {
    if (!isEdit) reset();
    resetMutation();
    onClose();
  };
 
  const onSubmit = handleSubmit(
    ({ fromDate, toDate, description, sameGenderOnly }) => {
      if (!fromDate || !toDate) return;
      const payload = {
        fromDate: fromDate.toString(),
        toDate: toDate.toString(),
        description: description.trim(),
        sameGenderOnly,
      };
      if (props.mode === "edit") {
        updateMutation.mutate({ tripId: props.trip.tripId, ...payload });
      } else {
        createMutation.mutate({ communityId: props.communityId, ...payload });
      }
    },
  );
 
  const formId = isEdit ? "edit-public-trip-form" : "create-public-trip-form";
  const titleId = isEdit
    ? "edit-public-trip-dialog-title"
    : "create-public-trip-dialog-title";
 
  return (
    <Dialog aria-labelledby={titleId} open={open} onClose={handleClose}>
      <DialogTitle id={titleId} onClose={handleClose}>
        {isEdit
          ? t("publicTrips:edit_dialog_title")
          : t("publicTrips:create_dialog_title")}
      </DialogTitle>
      <DialogContent>
        {error && (
          <Alert severity="error" sx={{ mb: 2 }}>
            {error.message}
          </Alert>
        )}
        <form id={formId} onSubmit={onSubmit} noValidate>
          <FieldStack>
            <DateRow>
              <Datepicker
                control={control}
                error={!!errors.fromDate}
                helperText={errors.fromDate?.message}
                id={`${DATE_FIELD_ID}-from`}
                label={t("publicTrips:from_date_label")}
                name="fromDate"
                rules={{
                  required: t("publicTrips:from_date_required"),
                }}
              />
              <Datepicker
                control={control}
                error={!!errors.toDate}
                helperText={errors.toDate?.message}
                id={`${DATE_FIELD_ID}-to`}
                label={t("publicTrips:to_date_label")}
                name="toDate"
                minValue={watchFromDate ?? Temporal.Now.plainDateISO()}
                rules={{
                  required: t("publicTrips:to_date_required"),
                }}
              />
            </DateRow>
            {communityName && (
              <Typography variant="body2">
                <Typography component="span" fontWeight={600}>
                  {t("publicTrips:location_label")}
                </Typography>{" "}
                {communityName}
              </Typography>
            )}
            <Controller
              control={control}
              name="sameGenderOnly"
              render={({ field: { value, onChange } }) => (
                <div>
                  <FormControlLabel
                    control={<Checkbox checked={value} onChange={onChange} />}
                    label={t("publicTrips:same_gender_only_label")}
                  />
                  <Typography
                    variant="body2"
                    color="textSecondary"
                    sx={{ pl: 4 }}
                  >
                    {t("publicTrips:same_gender_only_helper")}
                  </Typography>
                </div>
              )}
            />
            <TextField
              id="public-trip-description"
              {...register("description", {
                required: t("publicTrips:description_required"),
                validate: (value) => {
                  Iif (value.trim().length === 0) {
                    return t("publicTrips:description_required");
                  }
                  Eif (value.length < DESCRIPTION_MIN_LENGTH) {
                    return t("publicTrips:description_chars_remaining", {
                      count: DESCRIPTION_MIN_LENGTH - value.length,
                    });
                  }
                  return true;
                },
              })}
              label={t("publicTrips:description_label")}
              placeholder={t("publicTrips:description_placeholder")}
              multiline
              minRows={4}
              fullWidth
              error={!!errors.description}
              helperText={
                errors.description?.message
                  ? errors.description.message
                  : descriptionCharsRemaining > 0
                    ? t("publicTrips:description_chars_remaining", {
                        count: descriptionCharsRemaining,
                      })
                    : ""
              }
            />
          </FieldStack>
        </form>
      </DialogContent>
      <DialogActions>
        <Button variant="outlined" onClick={handleClose}>
          {t("global:cancel")}
        </Button>
        <Button type="submit" form={formId} loading={isPending}>
          {isEdit
            ? t("publicTrips:edit_dialog_submit")
            : t("publicTrips:create_dialog_submit")}
        </Button>
      </DialogActions>
    </Dialog>
  );
}