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 | 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 115x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 1x 1x 1x 1x 1x 1x 116x 116x 116x 116x | import { styled } from "@mui/material";
import { useMutation, useQueryClient } from "@tanstack/react-query";
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 {
publicTripsBaseKey,
publicTripsByUserBaseKey,
} from "features/queryKeys";
import { useTranslation } from "i18n";
import { GLOBAL, PUBLIC_TRIPS } from "i18n/namespaces";
import { useRouter } from "next/router";
import { useForm } from "react-hook-form";
import { routeToHostRequest } from "routes";
import { service } from "service";
import { Temporal } from "temporal-polyfill";
import { theme } from "theme";
// Must match the backend host request minimum (and normal host requests).
const MESSAGE_MIN_LENGTH = 250;
const DATE_FIELD_ID = "offer-to-host-dates";
interface FormValues {
fromDate: Temporal.PlainDate | null;
toDate: Temporal.PlainDate | null;
text: string;
}
interface OfferToHostDialogProps {
open: boolean;
onClose: () => void;
tripId: number;
hostUserId: number;
hostName: string;
tripFromDate: Temporal.PlainDate;
tripToDate: Temporal.PlainDate;
}
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",
},
}));
export default function OfferToHostDialog({
open,
onClose,
tripId,
hostUserId,
hostName,
tripFromDate,
tripToDate,
}: OfferToHostDialogProps) {
const { t } = useTranslation([PUBLIC_TRIPS, GLOBAL]);
const router = useRouter();
const queryClient = useQueryClient();
const today = Temporal.Now.plainDateISO();
// The host can offer within the trip's window (shorten, not extend), and
// never in the past. The backend enforces these too.
const earliest =
Temporal.PlainDate.compare(tripFromDate, today) > 0 ? tripFromDate : today;
const {
control,
handleSubmit,
register,
reset,
watch,
formState: { errors },
} = useForm<FormValues>({
mode: "onBlur",
defaultValues: {
fromDate: tripFromDate,
toDate: tripToDate,
text: "",
},
});
const watchFromDate = watch("fromDate");
const text = watch("text") ?? "";
const charsRemaining = MESSAGE_MIN_LENGTH - text.length;
const {
mutate,
isPending,
error,
reset: resetMutation,
} = useMutation({
mutationFn: ({ fromDate, toDate, text }: FormValues) =>
service.requests.createHostRequest({
hostUserId,
// Both are required by the form, so non-null at submit time.
fromDate: fromDate!,
toDate: toDate!,
text: text.trim(),
publicTripId: tripId,
stayType: 0,
}),
onSuccess: (hostRequestId) => {
// Refetch trips so the card's viewerHostRequestId updates (the button
// flips to "Already offered") next time the list is shown.
queryClient.invalidateQueries({ queryKey: [publicTripsBaseKey] });
queryClient.invalidateQueries({ queryKey: [publicTripsByUserBaseKey] });
reset();
onClose();
router.push(routeToHostRequest(hostRequestId));
},
});
const handleClose = () => {
reset();
resetMutation();
onClose();
};
const onSubmit = handleSubmit((data) => mutate(data));
const titleId = "offer-to-host-dialog-title";
const formId = "offer-to-host-form";
return (
<Dialog aria-labelledby={titleId} open={open} onClose={handleClose}>
<DialogTitle id={titleId} onClose={handleClose}>
{t("publicTrips:offer_dialog_title", { name: hostName })}
</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"
defaultValue={tripFromDate}
minValue={earliest}
maxValue={tripToDate}
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"
defaultValue={tripToDate}
minValue={watchFromDate ?? earliest}
maxValue={tripToDate}
rules={{ required: t("publicTrips:to_date_required") }}
/>
</DateRow>
<TextField
id="offer-to-host-message"
{...register("text", {
required: t("publicTrips:offer_dialog_message_required"),
minLength: {
value: MESSAGE_MIN_LENGTH,
message: t("publicTrips:offer_dialog_chars_remaining", {
count: charsRemaining,
}),
},
})}
label={t("publicTrips:offer_dialog_message_label")}
placeholder={t("publicTrips:offer_dialog_message_placeholder")}
minRows={6}
multiline
fullWidth
error={!!errors.text}
helperText={
errors.text?.message
? errors.text.message
: charsRemaining > 0
? t("publicTrips:offer_dialog_chars_remaining", {
count: charsRemaining,
})
: ""
}
/>
</FieldStack>
</form>
</DialogContent>
<DialogActions sx={{ padding: theme.spacing(0, 3, 2) }}>
<Button variant="outlined" onClick={handleClose}>
{t("global:cancel")}
</Button>
<Button type="submit" form={formId} loading={isPending}>
{t("publicTrips:offer_dialog_submit")}
</Button>
</DialogActions>
</Dialog>
);
}
|