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 | 4x 4x 4x 4x 4x 4x 4x 4x 4x 684x 342x 342x 342x 342x 342x 59x 59x 14x 342x 76x 342x 96x 342x 24x 24x 10x 342x 13x 342x 20x 56x 4x 52x 28x 5x 23x 23x 1x 22x 22x 22x 83x 3x 80x 80x 20x 60x 40x 4x 36x 36x 36x 36x 1x 35x 35x 35x 35x 13x 22x | import { styled } from "@mui/material";
import Datepicker from "components/Datepicker";
import Timepicker from "components/Timepicker";
import { useTranslation } from "i18n";
import { COMMUNITIES } from "i18n/namespaces";
import { Event } from "proto/events_pb";
import { UseFormReturn } from "react-hook-form";
import { Temporal } from "temporal-polyfill";
import { theme } from "theme";
import { timestampToPlainDateTime } from "utils/date";
import { timePattern } from "utils/validation";
import { CreateEventData } from "./EventForm";
const StyledContainer = styled("div")(() => ({
display: "grid",
gridTemplateColumns: "1fr",
gap: theme.spacing(3, 2),
[theme.breakpoints.up("md")]: {
gridTemplateColumns: "1fr 1fr",
},
}));
interface EventTimeChangerProps
extends Pick<UseFormReturn<CreateEventData>, "control" | "getValues" | "setValue" | "register"> {
dirtyFields: UseFormReturn<CreateEventData>["formState"]["dirtyFields"];
event?: Event.AsObject;
errors: UseFormReturn<CreateEventData>["formState"]["errors"];
}
export default function EventTimeChanger({
control,
dirtyFields,
errors,
event,
getValues,
setValue,
}: EventTimeChangerProps) {
const { t } = useTranslation([COMMUNITIES]);
const defaultStartDateTime = event?.startTime
? timestampToPlainDateTime(event.startTime, event?.timezone)
: undefined;
const defaultEndDateTime = event?.endTime ? timestampToPlainDateTime(event.endTime, event?.timezone) : undefined;
const handleStartDateAccept = (value: Temporal.PlainDate) => {
const endDate = getValues("endDate");
//limiting value.year>1000 to prevent autofilling year "2027" too early as "202 A.D."
if (!endDate && value && value.year > 1000) {
setValue("endDate", value, {
shouldDirty: true,
shouldValidate: true,
});
}
};
const handleStartDateChange = (value: Temporal.PlainDate) => {
setValue("startDate", value, {
shouldDirty: true,
shouldValidate: true,
});
};
const handleEndDateChange = (value: Temporal.PlainDate) => {
setValue("endDate", value, {
shouldDirty: true,
shouldValidate: true,
});
};
const handleStartTimeAccept = (value: Temporal.PlainTime) => {
const endTime = getValues("endTime");
if (!endTime) {
setValue("endTime", value, {
shouldDirty: true,
shouldValidate: true,
});
}
};
const handleStartTimeChange = (value: Temporal.PlainTime) => {
setValue("startTime", value, {
shouldDirty: true,
shouldValidate: true,
});
};
const handleEndTimeChange = (value: Temporal.PlainTime) => {
setValue("endTime", value, {
shouldDirty: true,
shouldValidate: true,
});
};
return (
<>
<StyledContainer>
<Datepicker
control={control}
defaultValue={defaultStartDateTime?.toPlainDate()}
error={!!errors.startDate?.message}
helperText={errors.startDate?.message}
id="startDate"
label={t("communities:start_date")}
name="startDate"
onAccept={handleStartDateAccept}
onPostChange={handleStartDateChange}
rules={{
required: t("communities:date_required"),
validate: (startDate: Temporal.PlainDate) => {
// Only disable validation temporarily if `event` exists/in the edit event context
if (event && !dirtyFields.startDate) {
return true;
}
return (
Temporal.PlainDate.compare(startDate, Temporal.Now.plainDateISO()) >= 0 ||
t("communities:past_date_error")
);
},
}}
testId="startDate"
/>
<Timepicker
control={control}
name="startTime"
onPostChange={handleStartTimeChange}
onAccept={handleStartTimeAccept}
defaultValue={defaultStartDateTime?.toPlainTime()}
rules={{
required: t("communities:time_required"),
pattern: {
message: t("communities:invalid_time"),
value: timePattern,
},
validate: (startTime: Temporal.PlainTime) => {
if (event && !dirtyFields.startTime) {
return true;
}
const startDate = getValues("startDate");
if (!startDate) {
return t("communities:date_required");
}
Iif (!startTime) {
return t("communities:time_required");
}
const startDateTime = startDate.toPlainDateTime(startTime);
return (
Temporal.PlainDateTime.compare(startDateTime, Temporal.Now.plainDateTimeISO()) >= 0 ||
t("communities:past_time_error")
);
},
}}
id="startTime"
label={t("communities:start_time")}
error={!!errors.startTime?.message}
helperText={errors.startTime?.message || ""}
testId="startTime"
/>
</StyledContainer>
<StyledContainer>
<Datepicker
control={control}
defaultValue={defaultEndDateTime?.toPlainDate()}
error={!!errors.endDate?.message}
helperText={errors.endDate?.message || ""}
id="endDate"
label={t("communities:end_date")}
name="endDate"
rules={{
required: t("communities:date_required"),
validate: (endDate: Temporal.PlainDate) => {
if (event && !dirtyFields.endDate) {
return true;
}
const startDate = getValues("startDate");
if (startDate && Temporal.PlainDate.compare(endDate, startDate) < 0) {
return t("communities:end_date_error");
}
return (
Temporal.PlainDate.compare(endDate, Temporal.Now.plainDateISO()) >= 0 ||
t("communities:past_date_error")
);
},
}}
testId="endDate"
onPostChange={handleEndDateChange}
/>
<Timepicker
control={control}
name="endTime"
onPostChange={handleEndTimeChange}
defaultValue={defaultEndDateTime?.toPlainTime()}
rules={{
required: t("communities:time_required"),
pattern: {
message: t("communities:invalid_time"),
value: timePattern,
},
validate: (endTime: Temporal.PlainTime) => {
if (event && !dirtyFields.endTime) {
return true;
}
const startTime = getValues("startTime");
const startDate = getValues("startDate");
const endDate = getValues("endDate");
if (!startTime || !endTime) {
return t("communities:time_required");
}
Iif (!startDate || !endDate) {
return t("communities:date_required");
}
const startDateTime = startDate.toPlainDateTime(startTime);
const endDateTime = endDate.toPlainDateTime(endTime);
if (Temporal.PlainDateTime.compare(endDateTime, startDateTime) <= 0) {
return t("communities:end_time_error");
}
return (
Temporal.PlainDateTime.compare(endDateTime, Temporal.Now.plainDateTimeISO()) >= 0 ||
t("communities:past_time_error")
);
},
}}
id="endTime"
label={t("communities:end_time")}
error={!!errors.endTime?.message}
helperText={errors.endTime?.message || ""}
testId="endTime"
/>
</StyledContainer>
</>
);
}
|