From 6a6f8a19e8a4f3ad74ed09e5d13a96dff2bd3dde Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sun, 7 Aug 2022 13:25:49 +0300 Subject: [PATCH] New event dates fix able to submit without one --- TODO.md | 3 +- app/routes/calendar/new.tsx | 141 ++++++++++++++---------------------- app/styles/calendar-new.css | 10 --- app/utils/dates.ts | 16 +++- 4 files changed, 72 insertions(+), 98 deletions(-) diff --git a/TODO.md b/TODO.md index df6be115f..5e568e800 100644 --- a/TODO.md +++ b/TODO.md @@ -23,9 +23,10 @@ Calendar ## Other +- [ ] date input take in account time zone difference between server and client - [x] User selector allow passing users from top level - [ ] Constants use CALENDAR_EVENT object -- [ ] New unfriendly crash if omits dates +- [x] New unfriendly crash if omits dates - [x] badges inside tag? - [x] Tags selector can remove - [x] Calendar new title diff --git a/app/routes/calendar/new.tsx b/app/routes/calendar/new.tsx index 2587679c2..55ad3b8cb 100644 --- a/app/routes/calendar/new.tsx +++ b/app/routes/calendar/new.tsx @@ -87,13 +87,10 @@ const newCalendarEventActionSchema = z.object({ falsyToNull, z.string().max(CALENDAR_EVENT_DESCRIPTION_MAX_LENGTH).nullable() ), - dates: z.preprocess( - safeJSONParse, - z - .array(z.preprocess(date, z.date().min(MIN_DATE).max(MAX_DATE))) - .min(1) - .max(CALENDAR_EVENT_MAX_AMOUNT_OF_DATES) - ), + date: z + .array(z.preprocess(date, z.date().min(MIN_DATE).max(MAX_DATE))) + .min(1) + .max(CALENDAR_EVENT_MAX_AMOUNT_OF_DATES), bracketUrl: z.string().url().max(CALENDAR_EVENT_BRACKET_URL_MAX_LENGTH), discordInviteCode: z.preprocess( falsyToNull, @@ -127,7 +124,7 @@ export const action: ActionFunction = async ({ request }) => { const commonArgs = { name: data.name, description: data.description, - startTimes: data.dates.map((date) => dateToDatabaseTimestamp(date)), + startTimes: data.date.map((date) => dateToDatabaseTimestamp(date)), bracketUrl: data.bracketUrl, discordInviteCode: data.discordInviteCode, tags: data.tags @@ -270,13 +267,8 @@ function DescriptionTextarea() { function DatesInput() { const { eventToEdit } = useLoaderData(); - const { i18n } = useTranslation(); - const [dateInputValue, setDateInputValue] = React.useState(); - const [dates, setDates] = React.useState( - (eventToEdit?.startTimes ?? []).map((startTime) => ({ - date: databaseTimestampToDate(startTime), - id: String(Math.random()), - })) + const [datesCount, setDatesCount] = React.useState( + eventToEdit?.startTimes.length ?? 1 ); const isMounted = useIsMounted(); @@ -286,85 +278,64 @@ function DatesInput() { return (
- {dates.length > 0 && ( - date.getTime()))} - /> - )}
-
- setDateInputValue(e.target.value)} - min={dateToYearMonthDayHourMinuteString(MIN_DATE)} - max={dateToYearMonthDayHourMinuteString(MAX_DATE)} - data-cy="date-input" - /> - +
+ {new Array(datesCount).fill(null).map((_, i) => { + const defaultStartTime = eventToEdit?.startTimes[i]; + + return ( +
+ + {i === datesCount - 1 && ( + <> + + {datesCount > 1 && ( + + )} + + )} +
+ ); + })}
Times in your local time zone: {usersTimeZone}
- {dates.length > 0 && ( -
- {dates.map(({ date, id }, i) => ( - -
- Day {i + 1} -
-
- {isMounted - ? date.toLocaleTimeString(i18n.language, { - hour: "numeric", - minute: "numeric", - year: "numeric", - month: "long", - day: "numeric", - weekday: "long", - }) - : "T"} -
-
- )}
); } diff --git a/app/styles/calendar-new.css b/app/styles/calendar-new.css index 0cf330458..fb4d9ad6b 100644 --- a/app/styles/calendar-new.css +++ b/app/styles/calendar-new.css @@ -1,13 +1,3 @@ -.calendar-new__dates-list { - display: grid; - padding: 0; - font-size: var(--fonts-sm); - font-weight: var(--semi-bold); - gap: var(--s-2); - grid-template-columns: max-content max-content 1fr; - list-style: none; -} - .calendar-new__select { max-width: 16rem; } diff --git a/app/utils/dates.ts b/app/utils/dates.ts index 01d4827c8..c3270c6bc 100644 --- a/app/utils/dates.ts +++ b/app/utils/dates.ts @@ -34,7 +34,19 @@ export function weekNumberToDate({ return result; } -/** Returns date as a string with the format YYYY-MM-DDThh:mm */ +/** Returns date as a string with the format YYYY-MM-DDThh:mm in user's time zone */ export function dateToYearMonthDayHourMinuteString(date: Date) { - return date.toISOString().slice(0, 16); + const year = date.getFullYear(); + const month = date.getMonth() + 1; + const day = date.getDate(); + const hour = date.getHours(); + const minute = date.getMinutes(); + + return `${year}-${prefixZero(month)}-${prefixZero(day)}T${prefixZero( + hour + )}:${prefixZero(minute)}`; +} + +function prefixZero(number: number) { + return number < 10 ? `0${number}` : number; }