fix: Autodetection of time zone only updated default time zone (#15392)

* fix: Autodetection of time zone only updated default time zone

* fix: description text

---------

Co-authored-by: sean-brydon <[email protected]>
This commit is contained in:
Amit Sharmaandsean-brydon authored and GitHub committed 2024-06-28 10:12:33 +01:00
1 parent c458b297ae
commit 285d1ec4c9
8 files changed
+148 -139

No files matched your search

@@ -1,90 +0,0 @@
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { trpc } from "@calcom/trpc/react";
import { Dialog, DialogContent, Form, DialogFooter, DialogClose, Button, CheckboxField } from "@calcom/ui";
export const BulkUpdateEventSchema = z.object({
eventTypeIds: z.array(z.number()),
});
export function BulkEditDefaultForEventsModal(props: {
open: boolean;
setOpen: (open: boolean) => void;
bulkUpdateFunction: ({ eventTypeIds }: { eventTypeIds: number[] }) => void;
isPending: boolean;
}) {
const { t } = useLocale();
const utils = trpc.useUtils();
const { data, isFetching } = trpc.viewer.eventTypes.bulkEventFetch.useQuery();
const form = useForm({
resolver: zodResolver(BulkUpdateEventSchema),
defaultValues: {
eventTypeIds: data?.eventTypes.map((e) => e.id) ?? [],
},
});
const eventTypesSelected = form.watch("eventTypeIds");
if (isFetching || !open || !data?.eventTypes) return null;
return (
<Dialog name="Bulk Default Location Update" open={props.open} onOpenChange={props.setOpen}>
<DialogContent
type="creation"
title={t("default_conferencing_bulk_title")}
description={t("default_conferencing_bulk_description")}
enableOverflow>
<Form
form={form}
handleSubmit={(values) => {
props.bulkUpdateFunction(values);
}}>
<div className="flex flex-col space-y-2">
{data.eventTypes.length > 0 && (
<div className="flex items-center space-x-2 rounded-md px-3 pb-2.5 pt-1">
<CheckboxField
description={t("select_all")}
descriptionAsLabel
onChange={(e) => {
form.setValue("eventTypeIds", e.target.checked ? data.eventTypes.map((e) => e.id) : []);
}}
checked={eventTypesSelected.length === data.eventTypes.length}
/>
</div>
)}
{data.eventTypes.map((eventType) => (
<div key={eventType.id} className="bg-muted flex items-center space-x-2 rounded-md px-3 py-2.5">
<CheckboxField
description={eventType.title}
descriptionAsLabel
checked={eventTypesSelected.includes(eventType.id)}
onChange={(e) => {
form.setValue(
"eventTypeIds",
e.target.checked
? [...eventTypesSelected, eventType.id]
: eventTypesSelected.filter((id) => id !== eventType.id)
);
}}
/>
</div>
))}
</div>
<DialogFooter showDivider className="mt-10">
<DialogClose
onClick={() => {
utils.viewer.getUsersDefaultConferencingApp.invalidate();
}}
/>
<Button type="submit" color="primary" loading={props.isPending}>
{t("update")}
</Button>
</DialogFooter>
</Form>
</DialogContent>
</Dialog>
);
}
@@ -0,0 +1,63 @@
import type { Dispatch, SetStateAction } from "react";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { Dialog, DialogContent, DialogFooter, DialogClose, Button, CheckboxField } from "@calcom/ui";
export function BulkEditDefaultModal(props: {
open: boolean;
setOpen: (open: boolean) => void;
ids: number[];
setIds: Dispatch<SetStateAction<number[]>>;
isPending: boolean;
data?: {
id: number;
title: string;
default?: boolean;
}[];
title: string;
description: string;
handleSubmit: () => void;
}) {
const { title, description, handleSubmit, data, ids, setIds, open } = props;
const { t } = useLocale();
if (!open || !data || !ids) return null;
return (
<Dialog name="Bulk Default Update" open={props.open} onOpenChange={props.setOpen}>
<DialogContent type="creation" title={title} description={description} enableOverflow>
<div className="flex flex-col space-y-2">
{data.length > 0 && (
<div className="flex items-center space-x-2 rounded-md px-3 pb-2.5 pt-1">
<CheckboxField
description={t("select_all")}
descriptionAsLabel
onChange={(e) => {
setIds(e.target.checked ? data.map((e) => e.id) : []);
}}
checked={ids.length === data.length}
/>
</div>
)}
{data.map((item) => (
<div key={item.id} className="bg-muted flex items-center space-x-2 rounded-md px-3 py-2.5">
<CheckboxField
description={item.title}
descriptionAsLabel
checked={ids.includes(item.id)}
onChange={(e) => {
setIds(e.target.checked ? [...ids, item.id] : ids.filter((id) => id !== item.id));
}}
/>
</div>
))}
</div>
<DialogFooter showDivider className="mt-10">
<DialogClose />
<Button type="submit" color="primary" loading={props.isPending} onClick={handleSubmit}>
{t("update")}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
@@ -4,6 +4,7 @@ import { useSession } from "next-auth/react";
import { useEffect, useState } from "react";
import dayjs from "@calcom/dayjs";
import { BulkEditDefaultModal } from "@calcom/features/eventtypes/components/BulkEditDefaultModal";
import { useLocale } from "@calcom/lib/hooks/useLocale";
import { trpc } from "@calcom/trpc/react";
import { Dialog, DialogClose, DialogContent, DialogFooter, showToast } from "@calcom/ui";
@@ -16,7 +17,6 @@ const TimezoneChangeDialogContent = ({
onAction: (action?: "update" | "cancel") => void;
}) => {
const { t } = useLocale();
const utils = trpc.useUtils();
const formattedCurrentTz = browserTimezone.replace("_", " ");
// save cookie to not show again
@@ -28,28 +28,6 @@ const TimezoneChangeDialogContent = ({
toast && showToast(t("we_wont_show_again"), "success");
}
const onSuccessMutation = async () => {
showToast(t("updated_timezone_to", { formattedCurrentTz }), "success");
await utils.viewer.me.invalidate();
};
const onErrorMutation = () => {
showToast(t("couldnt_update_timezone"), "error");
};
// update timezone in db
const mutation = trpc.viewer.updateProfile.useMutation({
onSuccess: onSuccessMutation,
onError: onErrorMutation,
});
function updateTimezone() {
onAction("update");
mutation.mutate({
timeZone: browserTimezone,
});
}
return (
<DialogContent
title={t("update_timezone_question")}
@@ -64,7 +42,7 @@ const TimezoneChangeDialogContent = ({
<DialogClose onClick={() => onCancel([3, "months"], true)} color="secondary">
{t("dont_update")}
</DialogClose>
<DialogClose onClick={() => updateTimezone()} color="primary">
<DialogClose onClick={() => onAction("update")} color="primary">
{t("update_timezone")}
</DialogClose>
</DialogFooter>
@@ -104,9 +82,56 @@ export function useOpenTimezoneDialog() {
export default function TimezoneChangeDialog() {
const { open, setOpen, browserTimezone } = useOpenTimezoneDialog();
const { t } = useLocale();
const utils = trpc.useUtils();
const [selectAvailability, setSelectAvailability] = useState(false);
const [ids, setIds] = useState<number[]>([]);
const { data: availability, isPending } = trpc.viewer.availability.list.useQuery();
const formattedCurrentTz = browserTimezone.replace("_", " ");
const data = availability?.schedules?.map((a) => ({
id: a.id,
title: a.name,
default: a.isDefault,
}));
const mutation = trpc.viewer.updateProfile.useMutation({
onSuccess: async () => {
showToast(t("updated_timezone_to", { formattedCurrentTz }), "success");
await utils.viewer.me.invalidate();
setSelectAvailability(false);
},
onError: () => {
showToast(t("couldnt_update_timezone"), "error");
},
});
return (
<Dialog open={open} onOpenChange={setOpen}>
<TimezoneChangeDialogContent browserTimezone={browserTimezone} onAction={() => setOpen(false)} />
<TimezoneChangeDialogContent
browserTimezone={browserTimezone}
onAction={() => {
setOpen(false);
setSelectAvailability(true);
}}
/>
<BulkEditDefaultModal
title={t("default_timezone_bulk_title")}
description={t("default_timezone_bulk_description")}
ids={ids}
isPending={isPending}
open={selectAvailability}
setOpen={setSelectAvailability}
setIds={setIds}
data={data}
handleSubmit={() => {
mutation.mutate({
timeZone: browserTimezone,
availabilityIds: ids,
});
}}
/>
</Dialog>
);
}