refactor: move feature-flags, calendars, and schedules components from packages/features to apps/web/modules (#27222)
* refactor: move feature-flags, calendars, and schedules components from packages/features to apps/web/modules Co-Authored-By: [email protected] <[email protected]> * fix * fix * fix * fix: move DatePicker and DateOverrideInputDialog to apps/web/modules and update imports Co-Authored-By: [email protected] <[email protected]> * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * rename * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * wip * wip * wip * fix unit tests --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
18049bc1d6
commit
c7ee3984ef
36 files changed
+134
-53
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { _generateMetadata, getTranslate } from "app/_utils";
|
||||
|
||||
import { FlagListingView } from "@calcom/features/flags/pages/flag-listing-view";
|
||||
import { FlagListingView } from "@calcom/web/modules/feature-flags/views/flag-listing-view";
|
||||
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
|
||||
|
||||
export const generateMetadata = async () =>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import DestinationCalendarSelector from "@calcom/features/calendars/DestinationCalendarSelector";
|
||||
import DestinationCalendarSelector from "@calcom/features/calendars/components/DestinationCalendarSelector";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import type { RouterInputs } from "@calcom/trpc/react";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { useForm } from "react-hook-form";
|
||||
|
||||
import Schedule from "@calcom/features/schedules/components/Schedule";
|
||||
import Schedule from "@calcom/web/modules/schedules/components/Schedule";
|
||||
import { DEFAULT_SCHEDULE } from "@calcom/lib/availability";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
|
||||
@@ -11,7 +11,7 @@ import DynamicPostHogProvider from "~/ee/posthog/providerDynamic";
|
||||
import DynamicPostHogPageView from "~/ee/posthog/pageViewDynamic";
|
||||
import { OrgBrandingProvider } from "@calcom/features/ee/organizations/context/provider";
|
||||
import { FeatureProvider } from "@calcom/features/flags/context/provider";
|
||||
import { useFlags } from "@calcom/features/flags/hooks";
|
||||
import { useFlags } from "@calcom/web/modules/feature-flags/hooks/useFlags";
|
||||
import DynamicHelpscoutProvider from "@calcom/web/modules/ee/support/lib/helpscout/providerDynamic";
|
||||
import DynamicIntercomProvider from "@calcom/web/modules/ee/support/lib/intercom/providerDynamic";
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ import { useEffect } from "react";
|
||||
|
||||
import { OrgBrandingProvider } from "@calcom/features/ee/organizations/context/provider";
|
||||
import { FeatureProvider } from "@calcom/features/flags/context/provider";
|
||||
import { useFlags } from "@calcom/features/flags/hooks";
|
||||
import { useFlags } from "@calcom/web/modules/feature-flags/hooks/useFlags";
|
||||
import DynamicHelpscoutProvider from "@calcom/web/modules/ee/support/lib/helpscout/providerDynamic";
|
||||
import DynamicIntercomProvider from "@calcom/web/modules/ee/support/lib/intercom/providerDynamic";
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ import posthog from "posthog-js";
|
||||
|
||||
import { BulkEditDefaultForEventsModal } from "@calcom/web/modules/event-types/components/BulkEditDefaultForEventsModal";
|
||||
import type { BulkUpdatParams } from "@calcom/web/modules/event-types/components/BulkEditDefaultForEventsModal";
|
||||
import { NewScheduleButton } from "@calcom/features/schedules/components/NewScheduleButton";
|
||||
import { NewScheduleButton } from "@calcom/web/modules/schedules/components/NewScheduleButton";
|
||||
import { ScheduleListItem } from "@calcom/features/schedules/components/ScheduleListItem";
|
||||
import { useCompatSearchParams } from "@calcom/lib/hooks/useCompatSearchParams";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
|
||||
@@ -81,7 +81,7 @@ const mockEvent = {
|
||||
isPending: false,
|
||||
};
|
||||
|
||||
vi.mock("@calcom/features/calendars/NoAvailabilityDialog", () => ({
|
||||
vi.mock("@calcom/features/calendars/components/NoAvailabilityDialog", () => ({
|
||||
default: () => {
|
||||
return null;
|
||||
},
|
||||
|
||||
@@ -2,12 +2,12 @@ import React from "react";
|
||||
import { vi, afterEach } from "vitest";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { DatePicker as DatePickerComponent } from "@calcom/features/calendars/DatePicker";
|
||||
import { DatePicker as DatePickerComponent } from "@calcom/features/calendars/components/DatePicker";
|
||||
|
||||
import { render } from "@calcom/features/bookings/Booker/__tests__/test-utils";
|
||||
import { DatePicker } from "./DatePicker";
|
||||
|
||||
vi.mock("@calcom/features/calendars/DatePicker", () => {
|
||||
vi.mock("@calcom/features/calendars/components/DatePicker", () => {
|
||||
return {
|
||||
DatePicker: vi.fn(() => <div data-testid="mock-date-picker" />),
|
||||
};
|
||||
|
||||
@@ -4,7 +4,7 @@ import type { Dayjs } from "@calcom/dayjs";
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import { useBookerStoreContext } from "@calcom/features/bookings/Booker/BookerStoreProvider";
|
||||
import type { DatePickerClassNames } from "@calcom/features/bookings/Booker/types";
|
||||
import { DatePicker as DatePickerComponent } from "@calcom/features/calendars/DatePicker";
|
||||
import { DatePicker as DatePickerComponent } from "@calcom/features/calendars/components/DatePicker";
|
||||
import { useNonEmptyScheduleDays } from "@calcom/features/schedules/lib/use-schedule/useNonEmptyScheduleDays";
|
||||
import { weekdayToWeekIndex } from "@calcom/lib/dayjs";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
|
||||
@@ -22,7 +22,7 @@ import {
|
||||
useEvent,
|
||||
useScheduleForEvent,
|
||||
} from "@calcom/features/bookings/Booker/utils/event";
|
||||
import DatePicker from "@calcom/features/calendars/DatePicker";
|
||||
import DatePicker from "@calcom/features/calendars/components/DatePicker";
|
||||
import { Dialog } from "@calcom/features/components/controlled-dialog";
|
||||
import { TimezoneSelect } from "@calcom/web/modules/timezone/components/TimezoneSelect";
|
||||
import type { Slot } from "@calcom/features/schedules/lib/use-schedule/types";
|
||||
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
} from "@calcom/web/modules/calendars/components/SelectedCalendarsSettingsWebWrapper";
|
||||
import { Timezone as PlatformTimzoneSelect } from "@calcom/atoms/timezone";
|
||||
import getLocationsOptionsForSelect from "@calcom/features/bookings/lib/getLocationOptionsForSelect";
|
||||
import DestinationCalendarSelector from "@calcom/features/calendars/DestinationCalendarSelector";
|
||||
import DestinationCalendarSelector from "@calcom/features/calendars/components/DestinationCalendarSelector";
|
||||
import { TimezoneSelect as WebTimezoneSelect } from "@calcom/web/modules/timezone/components/TimezoneSelect";
|
||||
import useLockedFieldsManager from "@calcom/features/ee/managed-event-types/hooks/useLockedFieldsManager";
|
||||
import {
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
|
||||
import { useDebounce } from "@calcom/lib/hooks/useDebounce";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import type { RouterOutputs } from "@calcom/trpc/react";
|
||||
import { Avatar } from "@calcom/ui/components/avatar";
|
||||
import { Button } from "@calcom/ui/components/button";
|
||||
import { Checkbox, TextField } from "@calcom/ui/components/form";
|
||||
import {
|
||||
Sheet,
|
||||
SheetContent,
|
||||
SheetHeader,
|
||||
SheetTitle,
|
||||
SheetBody,
|
||||
SheetFooter,
|
||||
} from "@calcom/ui/components/sheet";
|
||||
import { SkeletonContainer, SkeletonText } from "@calcom/ui/components/skeleton";
|
||||
import { showToast } from "@calcom/ui/components/toast";
|
||||
|
||||
type Flag = RouterOutputs["viewer"]["features"]["list"][number];
|
||||
|
||||
interface AssignFeatureSheetProps {
|
||||
flag: Flag;
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
}
|
||||
|
||||
export function AssignFeatureSheet({ flag, open, onOpenChange }: AssignFeatureSheetProps) {
|
||||
const { t } = useLocale();
|
||||
const utils = trpc.useUtils();
|
||||
const [searchTerm, setSearchTerm] = useState("");
|
||||
const debouncedSearchTerm = useDebounce(searchTerm, 300);
|
||||
|
||||
const { data, fetchNextPage, hasNextPage, isFetchingNextPage, isPending } =
|
||||
trpc.viewer.admin.getTeamsForFeature.useInfiniteQuery(
|
||||
{
|
||||
featureId: flag.slug,
|
||||
limit: 20,
|
||||
searchTerm: debouncedSearchTerm || undefined,
|
||||
},
|
||||
{
|
||||
enabled: open,
|
||||
getNextPageParam: (lastPage) => lastPage.nextCursor,
|
||||
}
|
||||
);
|
||||
|
||||
const teams = data?.pages.flatMap((page) => page.teams) ?? [];
|
||||
|
||||
useEffect(() => {
|
||||
if (!open) {
|
||||
setSearchTerm("");
|
||||
}
|
||||
}, [open]);
|
||||
|
||||
const assignMutation = trpc.viewer.admin.assignFeatureToTeam.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.viewer.admin.getTeamsForFeature.invalidate({ featureId: flag.slug });
|
||||
showToast(t("feature_assigned_successfully"), "success");
|
||||
},
|
||||
onError: (err) => {
|
||||
showToast(err.message, "error");
|
||||
},
|
||||
});
|
||||
|
||||
const unassignMutation = trpc.viewer.admin.unassignFeatureFromTeam.useMutation({
|
||||
onSuccess: () => {
|
||||
utils.viewer.admin.getTeamsForFeature.invalidate({ featureId: flag.slug });
|
||||
showToast(t("feature_unassigned_successfully"), "success");
|
||||
},
|
||||
onError: (err) => {
|
||||
showToast(err.message, "error");
|
||||
},
|
||||
});
|
||||
|
||||
const handleToggleTeam = (teamId: number, currentlyHasFeature: boolean) => {
|
||||
if (currentlyHasFeature) {
|
||||
unassignMutation.mutate({
|
||||
teamId,
|
||||
featureId: flag.slug,
|
||||
});
|
||||
} else {
|
||||
assignMutation.mutate({
|
||||
teamId,
|
||||
featureId: flag.slug,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
const handleClose = () => {
|
||||
onOpenChange(false);
|
||||
};
|
||||
|
||||
const isLoading = assignMutation.isPending || unassignMutation.isPending;
|
||||
|
||||
return (
|
||||
<Sheet open={open} onOpenChange={handleClose}>
|
||||
<SheetContent className="bg-cal-muted">
|
||||
<SheetHeader>
|
||||
<SheetTitle>Assign: {flag.slug}</SheetTitle>
|
||||
</SheetHeader>
|
||||
<SheetBody>
|
||||
<div className="mb-4">
|
||||
<TextField
|
||||
type="text"
|
||||
placeholder={t("search")}
|
||||
value={searchTerm}
|
||||
onChange={(e) => setSearchTerm(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
{isPending ? (
|
||||
<SkeletonContainer>
|
||||
<div className="stack-y-3">
|
||||
{[...Array(5)].map((_, i) => (
|
||||
<SkeletonText key={i} className="h-16 w-full" />
|
||||
))}
|
||||
</div>
|
||||
</SkeletonContainer>
|
||||
) : teams && teams.length > 0 ? (
|
||||
<>
|
||||
<div className="stack-y-2">
|
||||
{teams.map((team) => (
|
||||
<button
|
||||
key={team.id}
|
||||
type="button"
|
||||
onClick={() => handleToggleTeam(team.id, team.hasFeature)}
|
||||
disabled={isLoading}
|
||||
className="bg-default border-subtle hover:bg-cal-muted flex w-full items-center justify-between rounded-lg border p-4 text-left transition-colors disabled:cursor-not-allowed disabled:opacity-50">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="relative">
|
||||
{team.isOrganization ? (
|
||||
<div className="h-8 w-8 overflow-hidden rounded">
|
||||
{team.logoUrl ? (
|
||||
<img
|
||||
src={team.logoUrl}
|
||||
alt={team.name || ""}
|
||||
className="h-full w-full object-cover"
|
||||
/>
|
||||
) : (
|
||||
<div className="bg-emphasis text-default flex h-full w-full items-center justify-center text-xs font-semibold">
|
||||
{team.name?.charAt(0).toUpperCase()}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<Avatar size="sm" alt={team.name || ""} imageSrc={team.logoUrl} />
|
||||
)}
|
||||
{team.parent && team.parentId && (
|
||||
<div className="border-emphasis absolute -bottom-1 -right-1 h-4 w-4 overflow-hidden rounded border">
|
||||
{team.parent.logoUrl ? (
|
||||
<img
|
||||
src={team.parent.logoUrl}
|
||||
alt={team.parent.name || ""}
|
||||
className="h-full w-full object-cover"
|
||||
/>
|
||||
) : (
|
||||
<div className="bg-emphasis text-default flex h-full w-full items-center justify-center text-[8px] font-semibold">
|
||||
{team.parent.name?.charAt(0).toUpperCase()}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<div>
|
||||
<p className="text-emphasis text-sm font-medium">{team.name}</p>
|
||||
{team.slug && <p className="text-subtle text-xs">{team.slug}</p>}
|
||||
{team.parent && (
|
||||
<p className="text-subtle text-xs">
|
||||
{t("organization")}: {team.parent.name}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<Checkbox
|
||||
checked={team.hasFeature}
|
||||
disabled={isLoading}
|
||||
onCheckedChange={() => {}}
|
||||
/>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
{hasNextPage && (
|
||||
<div className="mt-4 flex justify-center">
|
||||
<Button
|
||||
color="secondary"
|
||||
onClick={() => fetchNextPage()}
|
||||
loading={isFetchingNextPage}
|
||||
disabled={isFetchingNextPage}>
|
||||
{t("load_more")}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
) : (
|
||||
<p className="text-subtle text-center text-sm">{t("no_teams_found")}</p>
|
||||
)}
|
||||
</SheetBody>
|
||||
<SheetFooter>
|
||||
<Button color="secondary" onClick={handleClose}>
|
||||
{t("close")}
|
||||
</Button>
|
||||
</SheetFooter>
|
||||
</SheetContent>
|
||||
</Sheet>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
import { useState } from "react";
|
||||
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import type { RouterOutputs } from "@calcom/trpc/react";
|
||||
import { Badge } from "@calcom/ui/components/badge";
|
||||
import { Button } from "@calcom/ui/components/button";
|
||||
import { PanelCard } from "@calcom/ui/components/card";
|
||||
import { Switch } from "@calcom/ui/components/form";
|
||||
import { ListItem, ListItemText, ListItemTitle } from "@calcom/ui/components/list";
|
||||
import { List } from "@calcom/ui/components/list";
|
||||
import { showToast } from "@calcom/ui/components/toast";
|
||||
|
||||
import { AssignFeatureSheet } from "./AssignFeatureSheet";
|
||||
|
||||
export const FlagAdminList = () => {
|
||||
const [data] = trpc.viewer.features.list.useSuspenseQuery();
|
||||
const [selectedFlag, setSelectedFlag] = useState<Flag | null>(null);
|
||||
const [sheetOpen, setSheetOpen] = useState(false);
|
||||
|
||||
const groupedFlags = data.reduce((acc, flag) => {
|
||||
const type = flag.type || "OTHER";
|
||||
if (!acc[type]) {
|
||||
acc[type] = [];
|
||||
}
|
||||
acc[type].push(flag);
|
||||
return acc;
|
||||
}, {} as Record<string, typeof data>);
|
||||
|
||||
const sortedTypes = Object.keys(groupedFlags).sort();
|
||||
|
||||
const handleAssignClick = (flag: Flag) => {
|
||||
setSelectedFlag(flag);
|
||||
setSheetOpen(true);
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="stack-y-4">
|
||||
{sortedTypes.map((type) => (
|
||||
<PanelCard key={type} title={type.replace(/_/g, " ")} collapsible defaultCollapsed={false}>
|
||||
<List roundContainer noBorderTreatment>
|
||||
{groupedFlags[type].map((flag: Flag, index: number) => (
|
||||
<ListItem key={flag.slug} rounded={index === 0 || index === groupedFlags[type].length - 1}>
|
||||
<div className="flex flex-1 flex-col">
|
||||
<ListItemTitle component="h3">{flag.slug}</ListItemTitle>
|
||||
<ListItemText component="p">{flag.description}</ListItemText>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 py-2">
|
||||
<FlagToggle flag={flag} />
|
||||
<Button
|
||||
color="secondary"
|
||||
size="sm"
|
||||
variant="icon"
|
||||
onClick={() => handleAssignClick(flag)}
|
||||
StartIcon="users"></Button>
|
||||
</div>
|
||||
</ListItem>
|
||||
))}
|
||||
</List>
|
||||
</PanelCard>
|
||||
))}
|
||||
</div>
|
||||
{selectedFlag && (
|
||||
<AssignFeatureSheet flag={selectedFlag} open={sheetOpen} onOpenChange={setSheetOpen} />
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
type Flag = RouterOutputs["viewer"]["features"]["list"][number];
|
||||
|
||||
const FlagToggle = (props: { flag: Flag }) => {
|
||||
const {
|
||||
flag: { slug, enabled },
|
||||
} = props;
|
||||
const utils = trpc.useUtils();
|
||||
const mutation = trpc.viewer.admin.toggleFeatureFlag.useMutation({
|
||||
onSuccess: () => {
|
||||
showToast("Flags successfully updated", "success");
|
||||
utils.viewer.features.list.invalidate();
|
||||
utils.viewer.features.map.invalidate();
|
||||
},
|
||||
});
|
||||
return (
|
||||
<Switch
|
||||
defaultChecked={enabled}
|
||||
onCheckedChange={(checked) => {
|
||||
mutation.mutate({ slug, enabled: checked });
|
||||
}}
|
||||
/>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1,48 @@
|
||||
import type { AppFlags } from "@calcom/features/flags/config";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
|
||||
const initialData: AppFlags = {
|
||||
organizations: false,
|
||||
teams: false,
|
||||
"calendar-cache": false,
|
||||
"calendar-cache-serve": false,
|
||||
emails: false,
|
||||
insights: false,
|
||||
webhooks: false,
|
||||
workflows: false,
|
||||
"email-verification": false,
|
||||
"google-workspace-directory": false,
|
||||
"disable-signup": false,
|
||||
attributes: false,
|
||||
"organizer-request-email-v2": false,
|
||||
"delegation-credential": false,
|
||||
"salesforce-crm-tasker": false,
|
||||
"workflow-smtp-emails": false,
|
||||
"cal-video-log-in-overlay": false,
|
||||
"use-api-v2-for-team-slots": false,
|
||||
pbac: false,
|
||||
"restriction-schedule": false,
|
||||
"team-booking-page-cache": false,
|
||||
"cal-ai-voice-agents": false,
|
||||
"tiered-support-chat": false,
|
||||
"calendar-subscription-cache": false,
|
||||
"calendar-subscription-sync": false,
|
||||
"onboarding-v3": false,
|
||||
"booker-botid": false,
|
||||
"booking-calendar-view": false,
|
||||
"booking-email-sms-tasker": false,
|
||||
"bookings-v3": false,
|
||||
"booking-audit": false,
|
||||
"monthly-proration": false,
|
||||
"sidebar-tips": false,
|
||||
};
|
||||
|
||||
if (process.env.NEXT_PUBLIC_IS_E2E) {
|
||||
initialData.organizations = true;
|
||||
initialData.teams = true;
|
||||
}
|
||||
|
||||
export function useFlags(): Partial<AppFlags> {
|
||||
const query = trpc.viewer.features.map.useQuery();
|
||||
return query.data ?? initialData;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
"use client";
|
||||
|
||||
import { Suspense } from "react";
|
||||
|
||||
import NoSSR from "@calcom/lib/components/NoSSR";
|
||||
import { SkeletonText, SkeletonContainer } from "@calcom/ui/components/skeleton";
|
||||
|
||||
import { FlagAdminList } from "../components/FlagAdminList";
|
||||
|
||||
const SkeletonLoader = () => {
|
||||
return (
|
||||
<SkeletonContainer>
|
||||
<div className="divide-subtle mb-8 mt-6 stack-y-6">
|
||||
<SkeletonText className="h-8 w-full" />
|
||||
<SkeletonText className="h-8 w-full" />
|
||||
</div>
|
||||
</SkeletonContainer>
|
||||
);
|
||||
};
|
||||
|
||||
export const FlagListingView = () => {
|
||||
return (
|
||||
<NoSSR>
|
||||
<Suspense fallback={<SkeletonLoader />}>
|
||||
<FlagAdminList />
|
||||
</Suspense>
|
||||
</NoSSR>
|
||||
);
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
"use client";
|
||||
|
||||
import { useFlags } from "@calcom/features/flags/hooks";
|
||||
import { useFlags } from "@calcom/web/modules/feature-flags/hooks/useFlags";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { Button } from "@calcom/ui/components/button";
|
||||
import { Icon } from "@calcom/ui/components/icon";
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ import React from "react";
|
||||
import { useForm, useFieldArray } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
|
||||
import { useFlags } from "@calcom/features/flags/hooks";
|
||||
import { useFlags } from "@calcom/web/modules/feature-flags/hooks/useFlags";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { Button } from "@calcom/ui/components/button";
|
||||
import { Form } from "@calcom/ui/components/form";
|
||||
|
||||
@@ -4,7 +4,7 @@ import { useRouter } from "next/navigation";
|
||||
import posthog from "posthog-js";
|
||||
import React from "react";
|
||||
|
||||
import { useFlags } from "@calcom/features/flags/hooks";
|
||||
import { useFlags } from "@calcom/web/modules/feature-flags/hooks/useFlags";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { Button } from "@calcom/ui/components/button";
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import { useRouter, useSearchParams } from "next/navigation";
|
||||
import posthog from "posthog-js";
|
||||
import React, { useEffect } from "react";
|
||||
|
||||
import { useFlags } from "@calcom/features/flags/hooks";
|
||||
import { useFlags } from "@calcom/web/modules/feature-flags/hooks/useFlags";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { Button } from "@calcom/ui/components/button";
|
||||
|
||||
|
||||
@@ -0,0 +1,94 @@
|
||||
import { revalidateAvailabilityList } from "app/(use-page-wrapper)/(main-nav)/availability/actions";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useForm } from "react-hook-form";
|
||||
|
||||
import { Dialog } from "@calcom/features/components/controlled-dialog";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import { Button } from "@calcom/ui/components/button";
|
||||
import { DialogContent, DialogFooter, DialogTrigger, DialogClose } from "@calcom/ui/components/dialog";
|
||||
import { Form } from "@calcom/ui/components/form";
|
||||
import { InputField } from "@calcom/ui/components/form";
|
||||
import { showToast } from "@calcom/ui/components/toast";
|
||||
|
||||
export function NewScheduleButton({
|
||||
name = "new-schedule",
|
||||
fromEventType,
|
||||
}: {
|
||||
name?: string;
|
||||
fromEventType?: boolean;
|
||||
}) {
|
||||
const router = useRouter();
|
||||
const { t } = useLocale();
|
||||
|
||||
const form = useForm<{
|
||||
name: string;
|
||||
}>();
|
||||
const { register } = form;
|
||||
const utils = trpc.useUtils();
|
||||
|
||||
const createMutation = trpc.viewer.availability.schedule.create.useMutation({
|
||||
onSuccess: async ({ schedule }) => {
|
||||
await router.push(`/availability/${schedule.id}${fromEventType ? "?fromEventType=true" : ""}`);
|
||||
showToast(t("schedule_created_successfully", { scheduleName: schedule.name }), "success");
|
||||
revalidateAvailabilityList();
|
||||
utils.viewer.availability.list.setData(undefined, (data) => {
|
||||
const newSchedule = { ...schedule, isDefault: false, availability: [] };
|
||||
if (!data)
|
||||
return {
|
||||
schedules: [newSchedule],
|
||||
};
|
||||
return {
|
||||
...data,
|
||||
schedules: [...data.schedules, newSchedule],
|
||||
};
|
||||
});
|
||||
},
|
||||
onError: (err) => {
|
||||
if (err instanceof HttpError) {
|
||||
const message = `${err.statusCode}: ${err.message}`;
|
||||
showToast(message, "error");
|
||||
}
|
||||
|
||||
if (err.data?.code === "UNAUTHORIZED") {
|
||||
const message = `${err.data.code}: ${t("error_schedule_unauthorized_create")}`;
|
||||
showToast(message, "error");
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return (
|
||||
<Dialog name={name} clearQueryParamsOnClose={["copy-schedule-id"]}>
|
||||
<DialogTrigger asChild>
|
||||
<Button variant="fab" data-testid={name} StartIcon="plus" size="sm">
|
||||
{t("new")}
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent title={t("add_new_schedule")}>
|
||||
<Form
|
||||
form={form}
|
||||
handleSubmit={(values) => {
|
||||
createMutation.mutate(values);
|
||||
}}>
|
||||
<InputField
|
||||
label={t("name")}
|
||||
type="text"
|
||||
id="name"
|
||||
required
|
||||
placeholder={t("default_schedule_name")}
|
||||
{...register("name", {
|
||||
setValueAs: (v) => (!v || v.trim() === "" ? null : v),
|
||||
})}
|
||||
/>
|
||||
<DialogFooter>
|
||||
<DialogClose />
|
||||
<Button type="submit" loading={createMutation.isPending}>
|
||||
{t("continue")}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</Form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import React from "react";
|
||||
import type { Control, FieldValues } from "react-hook-form";
|
||||
|
||||
import {
|
||||
ScheduleComponent,
|
||||
type FieldPathByValue,
|
||||
type ScheduleLabelsType,
|
||||
} from "@calcom/features/schedules/components/ScheduleComponent";
|
||||
import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery";
|
||||
import type { TimeRange } from "@calcom/types/schedule";
|
||||
|
||||
const Schedule = <
|
||||
TFieldValues extends FieldValues,
|
||||
TPath extends FieldPathByValue<TFieldValues, TimeRange[][]>
|
||||
>(props: {
|
||||
name: TPath;
|
||||
control: Control<TFieldValues>;
|
||||
weekStart?: number;
|
||||
disabled?: boolean;
|
||||
labels?: ScheduleLabelsType;
|
||||
userTimeFormat?: number | null;
|
||||
}) => {
|
||||
const query = useMeQuery();
|
||||
const { timeFormat } = query.data || { timeFormat: null };
|
||||
|
||||
return <ScheduleComponent userTimeFormat={timeFormat} {...props} />;
|
||||
};
|
||||
|
||||
export default Schedule;
|
||||
@@ -0,0 +1,122 @@
|
||||
import { TooltipProvider } from "@radix-ui/react-tooltip";
|
||||
import { render, renderHook, screen } from "@testing-library/react";
|
||||
// eslint-disable-next-line no-restricted-imports
|
||||
import { noop } from "lodash";
|
||||
import { useFieldArray, useForm, FormProvider } from "react-hook-form";
|
||||
import { vi } from "vitest";
|
||||
|
||||
import dayjs from "@calcom/dayjs";
|
||||
import DateOverrideList from "@calcom/features/schedules/components/DateOverrideList";
|
||||
import type { TimeRange } from "@calcom/types/schedule";
|
||||
|
||||
vi.mock("next/navigation", async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import("next/navigation")>();
|
||||
return {
|
||||
...actual,
|
||||
useRouter: vi.fn(() => ({
|
||||
push: vi.fn(() => {
|
||||
return;
|
||||
}),
|
||||
})),
|
||||
};
|
||||
});
|
||||
|
||||
type FormValues = {
|
||||
dateOverrides: { ranges: TimeRange[] }[];
|
||||
};
|
||||
|
||||
describe("DateOverrideList", () => {
|
||||
it("renders DateOverrideList component in military time", () => {
|
||||
const { result } = renderHook(() =>
|
||||
useForm<FormValues>({
|
||||
values: {
|
||||
dateOverrides: [
|
||||
{
|
||||
ranges: [
|
||||
{
|
||||
start: dayjs.utc().startOf("day").add(540, "minute").toDate(),
|
||||
end: dayjs.utc().startOf("day").add(1020, "minute").toDate(),
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
);
|
||||
const methods = result.current;
|
||||
const { control } = methods;
|
||||
|
||||
const { result: fieldArrayResult } = renderHook(() => useFieldArray({ control, name: "dateOverrides" }));
|
||||
render(
|
||||
<TooltipProvider>
|
||||
<FormProvider {...methods}>
|
||||
<DateOverrideList
|
||||
hour12={false}
|
||||
userTimeFormat={24}
|
||||
fields={fieldArrayResult.current.fields}
|
||||
replace={fieldArrayResult.current.replace}
|
||||
workingHours={[]}
|
||||
handleAvailabilityUpdate={noop}
|
||||
/>
|
||||
</FormProvider>
|
||||
</TooltipProvider>
|
||||
);
|
||||
|
||||
const dateString = new Intl.DateTimeFormat("en", {
|
||||
weekday: "long",
|
||||
month: "long",
|
||||
day: "numeric",
|
||||
timeZone: "UTC",
|
||||
}).format(new Date());
|
||||
|
||||
expect(screen.getByText(dateString)).toBeInTheDocument();
|
||||
expect(screen.getByText(`09:00 - 17:00`)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("renders DateOverrideList component in AM/PM", () => {
|
||||
const { result } = renderHook(() =>
|
||||
useForm<FormValues>({
|
||||
values: {
|
||||
dateOverrides: [
|
||||
{
|
||||
ranges: [
|
||||
{
|
||||
start: dayjs.utc().startOf("day").add(540, "minute").toDate(),
|
||||
end: dayjs.utc().startOf("day").add(1020, "minute").toDate(),
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
})
|
||||
);
|
||||
const methods = result.current;
|
||||
const { control } = methods;
|
||||
|
||||
const { result: fieldArrayResult } = renderHook(() => useFieldArray({ control, name: "dateOverrides" }));
|
||||
render(
|
||||
<TooltipProvider>
|
||||
<FormProvider {...methods}>
|
||||
<DateOverrideList
|
||||
hour12={true}
|
||||
userTimeFormat={12}
|
||||
fields={fieldArrayResult.current.fields}
|
||||
replace={fieldArrayResult.current.replace}
|
||||
workingHours={[]}
|
||||
handleAvailabilityUpdate={noop}
|
||||
/>
|
||||
</FormProvider>
|
||||
</TooltipProvider>
|
||||
);
|
||||
|
||||
const dateString = new Intl.DateTimeFormat("en", {
|
||||
weekday: "long",
|
||||
month: "long",
|
||||
day: "numeric",
|
||||
timeZone: "UTC",
|
||||
}).format(new Date());
|
||||
|
||||
expect(screen.getByText(dateString)).toBeInTheDocument();
|
||||
expect(screen.getByText(`9:00 AM - 5:00 PM`)).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -4,7 +4,7 @@ import dayjs from "@calcom/dayjs";
|
||||
import { TimezoneSelect } from "@calcom/web/modules/timezone/components/TimezoneSelect";
|
||||
import DateOverrideInputDialog from "@calcom/features/schedules/components/DateOverrideInputDialog";
|
||||
import DateOverrideList from "@calcom/features/schedules/components/DateOverrideList";
|
||||
import Schedule from "@calcom/features/schedules/components/Schedule";
|
||||
import Schedule from "@calcom/web/modules/schedules/components/Schedule";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import type { RouterOutputs } from "@calcom/trpc/react";
|
||||
|
||||
Reference in new issue
Block a user