Skip to content

Commit c6ab6b3

Browse files
fix: keep dialog state by decoupling it from list (calcom#24914)
Co-authored-by: Udit Takkar <53316345+Udit-takkar@users.noreply.github.com>
1 parent c741d81 commit c6ab6b3

4 files changed

Lines changed: 94 additions & 61 deletions

File tree

apps/web/app/(use-page-wrapper)/settings/(settings-layout)/my-account/out-of-office/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { _generateMetadata } from "app/_utils";
22

3-
import OutOfOfficeEntriesList from "@calcom/features/settings/outOfOffice/OutOfOfficeEntriesList";
3+
import OutOfOfficeView from "~/settings/my-account/out-of-office-view";
44

55
export const generateMetadata = async () =>
66
await _generateMetadata(
@@ -12,7 +12,7 @@ export const generateMetadata = async () =>
1212
);
1313

1414
const Page = () => {
15-
return <OutOfOfficeEntriesList />;
15+
return <OutOfOfficeView />;
1616
};
1717

1818
export default Page;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"use client";
2+
3+
import { useEffect, useState } from "react";
4+
5+
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
6+
7+
import OutOfOfficeEntriesList from "@calcom/features/settings/outOfOffice/OutOfOfficeEntriesList";
8+
import { CreateOrEditOutOfOfficeEntryModal } from "@calcom/features/settings/outOfOffice/CreateOrEditOutOfOfficeModal";
9+
import type { BookingRedirectForm } from "@calcom/features/settings/outOfOffice/CreateOrEditOutOfOfficeModal";
10+
11+
export default function OutOfOfficeView() {
12+
const [openModal, setOpenModal] = useState(false);
13+
const [currentlyEditingOutOfOfficeEntry, setCurrentlyEditingOutOfOfficeEntry] =
14+
useState<BookingRedirectForm | null>(null);
15+
16+
const params = useCompatSearchParams();
17+
const openModalOnStart = !!params?.get("om");
18+
19+
useEffect(() => {
20+
if (openModalOnStart) {
21+
setOpenModal(true);
22+
}
23+
}, [openModalOnStart]);
24+
25+
const handleOpenCreateDialog = () => {
26+
setCurrentlyEditingOutOfOfficeEntry(null);
27+
setOpenModal(true);
28+
};
29+
30+
const handleOpenEditDialog = (entry: BookingRedirectForm) => {
31+
setCurrentlyEditingOutOfOfficeEntry(entry);
32+
setOpenModal(true);
33+
};
34+
35+
const handleCloseModal = () => {
36+
setOpenModal(false);
37+
setCurrentlyEditingOutOfOfficeEntry(null);
38+
};
39+
40+
return (
41+
<>
42+
<OutOfOfficeEntriesList
43+
onOpenCreateDialog={handleOpenCreateDialog}
44+
onOpenEditDialog={handleOpenEditDialog}
45+
/>
46+
{openModal && (
47+
<CreateOrEditOutOfOfficeEntryModal
48+
openModal={openModal}
49+
closeModal={handleCloseModal}
50+
currentlyEditingOutOfOfficeEntry={currentlyEditingOutOfOfficeEntry}
51+
/>
52+
)}
53+
</>
54+
);
55+
}
56+
Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
"use client";
22

3-
import { useState, useEffect } from "react";
4-
53
import { checkAdminOrOwner } from "@calcom/features/auth/lib/checkAdminOrOwner";
64
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
75
import { useLocale } from "@calcom/lib/hooks/useLocale";
@@ -10,14 +8,15 @@ import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery";
108
import type { ButtonProps } from "@calcom/ui/components/button";
119
import { Button } from "@calcom/ui/components/button";
1210

13-
import { CreateOrEditOutOfOfficeEntryModal } from "./CreateOrEditOutOfOfficeModal";
1411
import { OutOfOfficeTab } from "./OutOfOfficeToggleGroup";
1512

16-
const CreateNewOutOfOfficeEntry = ({
13+
const CreateNewOutOfOfficeEntryButton = ({
1714
size,
15+
onClick,
1816
...rest
1917
}: {
2018
size?: ButtonProps["size"];
19+
onClick: () => void;
2120
"data-testid"?: string;
2221
}) => {
2322
const { t } = useLocale();
@@ -27,39 +26,20 @@ const CreateNewOutOfOfficeEntry = ({
2726
const hasTeamOOOAdminAccess = isOrgAdminOrOwner || me?.data?.isTeamAdminOrOwner;
2827

2928
const params = useCompatSearchParams();
30-
const openModalOnStart = !!params?.get("om");
31-
useEffect(() => {
32-
if (openModalOnStart) {
33-
setOpenModal(true);
34-
}
35-
}, [openModalOnStart]);
36-
37-
const [openModal, setOpenModal] = useState(false);
3829
const selectedTab = params?.get("type") ?? OutOfOfficeTab.MINE;
3930

4031
return (
41-
<>
42-
<Button
43-
color="primary"
44-
size={size ?? "base"}
45-
className="flex items-center justify-between px-4"
46-
StartIcon="plus"
47-
onClick={() => setOpenModal(true)}
48-
data-testid={rest["data-testid"]}
49-
disabled={selectedTab === OutOfOfficeTab.TEAM && !hasTeamOOOAdminAccess}>
50-
{t("add")}
51-
</Button>
52-
{openModal && (
53-
<CreateOrEditOutOfOfficeEntryModal
54-
openModal={openModal}
55-
closeModal={() => {
56-
setOpenModal(false);
57-
}}
58-
currentlyEditingOutOfOfficeEntry={null}
59-
/>
60-
)}
61-
</>
32+
<Button
33+
color="primary"
34+
size={size ?? "base"}
35+
className="flex items-center justify-between px-4"
36+
StartIcon="plus"
37+
onClick={onClick}
38+
data-testid={rest["data-testid"]}
39+
disabled={selectedTab === OutOfOfficeTab.TEAM && !hasTeamOOOAdminAccess}>
40+
{t("add")}
41+
</Button>
6242
);
6343
};
6444

65-
export default CreateNewOutOfOfficeEntry;
45+
export default CreateNewOutOfOfficeEntryButton;

packages/features/settings/outOfOffice/OutOfOfficeEntriesList.tsx

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import { showToast } from "@calcom/ui/components/toast";
3838
import { Tooltip } from "@calcom/ui/components/tooltip";
3939

4040
import CreateNewOutOfOfficeEntryButton from "./CreateNewOutOfOfficeEntryButton";
41-
import { CreateOrEditOutOfOfficeEntryModal } from "./CreateOrEditOutOfOfficeModal";
4241
import type { BookingRedirectForm } from "./CreateOrEditOutOfOfficeModal";
4342
import { OutOfOfficeTab, OutOfOfficeToggleGroup } from "./OutOfOfficeToggleGroup";
4443

@@ -63,7 +62,13 @@ interface OutOfOfficeEntry {
6362
canEditAndDelete: boolean;
6463
}
6564

66-
export default function OutOfOfficeEntriesList() {
65+
export default function OutOfOfficeEntriesList({
66+
onOpenCreateDialog,
67+
onOpenEditDialog,
68+
}: {
69+
onOpenCreateDialog: () => void;
70+
onOpenEditDialog: (entry: BookingRedirectForm) => void;
71+
}) {
6772
const { t } = useLocale();
6873
const pathname = usePathname();
6974

@@ -76,27 +81,29 @@ export default function OutOfOfficeEntriesList() {
7681
CTA={
7782
<div className="flex gap-2">
7883
<OutOfOfficeToggleGroup />
79-
<CreateNewOutOfOfficeEntryButton data-testid="add_entry_ooo" />
84+
<CreateNewOutOfOfficeEntryButton data-testid="add_entry_ooo" onClick={onOpenCreateDialog} />
8085
</div>
8186
}>
8287
<DataTableProvider tableIdentifier={pathname} useSegments={useSegments}>
83-
<OutOfOfficeEntriesListContent />
88+
<OutOfOfficeEntriesListContent
89+
onOpenCreateDialog={onOpenCreateDialog}
90+
onOpenEditDialog={onOpenEditDialog}
91+
/>
8492
</DataTableProvider>
8593
</SettingsHeader>
8694
);
8795
}
8896

89-
function OutOfOfficeEntriesListContent() {
97+
function OutOfOfficeEntriesListContent({
98+
onOpenCreateDialog,
99+
onOpenEditDialog,
100+
}: {
101+
onOpenCreateDialog: () => void;
102+
onOpenEditDialog: (entry: BookingRedirectForm) => void;
103+
}) {
90104
const { t } = useLocale();
91105
const tableContainerRef = useRef<HTMLDivElement>(null);
92106
const [deletedEntry, setDeletedEntry] = useState(0);
93-
const [currentlyEditingOutOfOfficeEntry, setCurrentlyEditingOutOfOfficeEntry] =
94-
useState<BookingRedirectForm | null>(null);
95-
const [openModal, setOpenModal] = useState(false);
96-
const editOutOfOfficeEntry = (entry: BookingRedirectForm) => {
97-
setCurrentlyEditingOutOfOfficeEntry(entry);
98-
setOpenModal(true);
99-
};
100107

101108
const { searchTerm } = useDataTable();
102109
const searchParams = useCompatSearchParams();
@@ -288,7 +295,7 @@ function OutOfOfficeEntriesListContent() {
288295
forUserAvatar: item.user?.avatarUrl,
289296
toUserName: item.toUser?.name || item.toUser?.username,
290297
};
291-
editOutOfOfficeEntry(outOfOfficeEntryData);
298+
onOpenEditDialog(outOfOfficeEntryData);
292299
}}
293300
disabled={isPending || isFetching || !item.canEditAndDelete}
294301
/>
@@ -324,7 +331,7 @@ function OutOfOfficeEntriesListContent() {
324331
},
325332
}),
326333
];
327-
}, [selectedTab, isPending, isFetching]);
334+
}, [selectedTab, isPending, isFetching, onOpenEditDialog, t]);
328335

329336
const table = useReactTable({
330337
data: flatData,
@@ -385,7 +392,7 @@ function OutOfOfficeEntriesListContent() {
385392
? t("ooo_team_empty_description")
386393
: t("ooo_empty_description")
387394
}
388-
buttonRaw={<CreateNewOutOfOfficeEntryButton size="sm" />}
395+
buttonRaw={<CreateNewOutOfOfficeEntryButton size="sm" onClick={onOpenCreateDialog} />}
389396
customIcon={
390397
<div className="mt-4 h-[102px]">
391398
<div className="flex h-full flex-col items-center justify-center p-2 md:mt-0 md:p-0">
@@ -410,16 +417,6 @@ function OutOfOfficeEntriesListContent() {
410417
/>
411418
}
412419
/>
413-
{openModal && (
414-
<CreateOrEditOutOfOfficeEntryModal
415-
openModal={openModal}
416-
closeModal={() => {
417-
setOpenModal(false);
418-
setCurrentlyEditingOutOfOfficeEntry(null);
419-
}}
420-
currentlyEditingOutOfOfficeEntry={currentlyEditingOutOfOfficeEntry}
421-
/>
422-
)}
423420
</>
424421
);
425422
}

0 commit comments

Comments
 (0)