Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

🐛 Keep mixed date and time options out of the poll form (#3613)

Luke Velladdca5040efe5
diff --git a/apps/web/src/features/poll/components/create-poll.tsx b/apps/web/src/features/poll/components/create-poll.tsx
index 548a45843..575a2b3a3 100644
--- a/apps/web/src/features/poll/components/create-poll.tsx
+++ b/apps/web/src/features/poll/components/create-poll.tsx
@@ -22,7 +22,10 @@ import type { ConferencingOptions } from "@/features/conferencing/components/con
import { toPollConferencing } from "@/features/conferencing/components/conferencing-field";
import { PollDetailsForm } from "@/features/poll/components/forms/poll-details-form";
import PollOptionsForm from "@/features/poll/components/forms/poll-options-form/poll-options-form";
-import { isAllDay } from "@/features/poll/components/forms/poll-options-form/utils";
+import {
+ hasMixedOptionTypes,
+ isAllDay,
+} from "@/features/poll/components/forms/poll-options-form/utils";
import { PollSettingsForm } from "@/features/poll/components/forms/poll-settings";
import type { NewEventData } from "@/features/poll/components/forms/types";
import { SelectedOptionsCount } from "@/features/poll/components/selected-options-count";
@@ -161,9 +164,27 @@ export const CreatePoll = ({
},
});
+ // Drafts saved before mixed changes were held back can mix date and time
+ // options, which the calendar can't render. Keep the time slots, matching
+ // how the calendar classified the list. Must be stable: the persist hook
+ // restores the draft again whenever this callback changes.
+ const { setValue } = form;
+ const handleDataRestored = React.useCallback(
+ (data: Partial<NewEventData>) => {
+ if (data.options && hasMixedOptionTypes(data.options)) {
+ setValue(
+ "options",
+ data.options.filter((option) => option.type === "timeSlot"),
+ );
+ }
+ },
+ [setValue],
+ );
+
const { clear } = useFormPersist("new-poll", {
watch: form.watch,
setValue: form.setValue,
+ onDataRestored: handleDataRestored,
});
const makePoll = trpc.polls.make.useMutation();
diff --git a/apps/web/src/features/poll/components/forms/poll-options-form/poll-options-form.tsx b/apps/web/src/features/poll/components/forms/poll-options-form/poll-options-form.tsx
index 4756ecd2e..9e25d2ae1 100644
--- a/apps/web/src/features/poll/components/forms/poll-options-form/poll-options-form.tsx
+++ b/apps/web/src/features/poll/components/forms/poll-options-form/poll-options-form.tsx
@@ -24,6 +24,7 @@ import { Trans, useTranslation } from "@/i18n/client";
import { getBrowserTimeZone } from "@/lib/utils/date-time-utils";
import type { NewEventData } from "../types";
import MonthCalendar from "./month-calendar/month-calendar";
+import type { DateTimeOption } from "./types";
import { hasMixedOptionTypes, isAllDay } from "./utils";
import WeekCalendar from "./week-calendar";
@@ -67,6 +68,12 @@ const PollOptionsForm = ({
const watchLockTimeZone = watch("lockTimeZone");
const dateOrTimeRangeDialog = useDialog();
+ // A change that would mix date and time options waits here until the user
+ // picks a kind, so the form never holds a mixed list. Dismissing the dialog
+ // discards the change.
+ const [pendingMixedOptions, setPendingMixedOptions] = React.useState<
+ DateTimeOption[]
+ >([]);
const allDay = isAllDay(watchOptions);
@@ -130,7 +137,9 @@ const PollOptionsForm = ({
onClick={() => {
setValue(
"options",
- watchOptions.filter((option) => option.type === "date"),
+ pendingMixedOptions.filter(
+ (option) => option.type === "date",
+ ),
);
setValue("timeZone", "");
dateOrTimeRangeDialog.dismiss();
@@ -145,7 +154,9 @@ const PollOptionsForm = ({
onClick={() => {
setValue(
"options",
- watchOptions.filter((option) => option.type === "timeSlot"),
+ pendingMixedOptions.filter(
+ (option) => option.type === "timeSlot",
+ ),
);
if (!watchTimeZone) {
setValue("timeZone", defaultTimeZone);
@@ -194,10 +205,12 @@ const PollOptionsForm = ({
setValue("navigationDate", date.toISOString());
}}
onChange={(options) => {
- field.onChange(options);
if (hasMixedOptionTypes(options)) {
+ setPendingMixedOptions(options);
dateOrTimeRangeDialog.trigger();
+ return;
}
+ field.onChange(options);
}}
duration={watchDuration}
timeZone={
diff --git a/apps/web/tests/mixed-poll-options.spec.ts b/apps/web/tests/mixed-poll-options.spec.ts
new file mode 100644
index 000000000..a0c5a241b
--- /dev/null
+++ b/apps/web/tests/mixed-poll-options.spec.ts
@@ -0,0 +1,81 @@
+import { expect, test } from "@playwright/test";
+
+test("a restored draft with mixed options keeps the time slots", async ({
+ page,
+}) => {
+ await page.addInitScript(() => {
+ // Pin the draft: in dev, StrictMode replays the restore effect after the
+ // persist hook has saved the empty form over it.
+ const setItem = Storage.prototype.setItem;
+ Storage.prototype.setItem = function (key, value) {
+ if (key !== "new-poll") {
+ setItem.call(this, key, value);
+ }
+ };
+ setItem.call(
+ window.sessionStorage,
+ "new-poll",
+ JSON.stringify({
+ view: "month",
+ options: [
+ { type: "date", date: "2030-01-10" },
+ {
+ type: "timeSlot",
+ start: "2030-01-11T12:00:00",
+ end: "2030-01-11T13:00:00",
+ },
+ ],
+ }),
+ );
+ });
+ await page.goto("/new");
+
+ // One day card, for the January 11 time slot; the January 10 date is gone.
+ await expect(
+ page.getByRole("button", { name: "Add time option" }),
+ ).toHaveCount(1);
+ await expect(page.locator("div").filter({ hasText: /^Jan11$/ })).toHaveCount(
+ 1,
+ );
+});
+
+test.describe("mixed date and time options", () => {
+ test.beforeEach(async ({ page }) => {
+ await page.goto("/new");
+ await page.getByTitle("Next month").click();
+ await page.getByText("10", { exact: true }).first().click();
+ await page.getByTestId("all-day-option").click();
+ await page.getByRole("tab", { name: "Week view" }).click();
+ // The selected date is next month, so any visible day is fine: adding a
+ // time slot to an all-day poll is what mixes the options.
+ await page
+ .locator('[data-slot="event-calendar-day-column"]')
+ .first()
+ .click();
+ });
+
+ test("dismissing the dialog discards the time slot", async ({ page }) => {
+ const dialog = page.getByRole("dialog");
+ await expect(dialog).toBeVisible();
+ await page.keyboard.press("Escape");
+ await expect(dialog).toBeHidden();
+
+ await page.getByRole("tab", { name: "Month view" }).click();
+
+ await expect(page.getByTestId("all-day-option")).toBeChecked();
+ await expect(
+ page.getByRole("button", { name: "Add time option" }),
+ ).toBeHidden();
+ });
+
+ test("keeping time options drops the dates", async ({ page }) => {
+ await page.getByRole("button", { name: "Keep time options" }).click();
+
+ await page.getByRole("tab", { name: "Month view" }).click();
+
+ await expect(page.getByTestId("all-day-option")).not.toBeChecked();
+ await expect(
+ page.getByRole("button", { name: "Add time option" }),
+ ).toBeVisible();
+ });
+});