Fix/connected calendar component (#3956)
* Fix for displaying connected calendars alert with test * Fix default calendar display string when externalId its too long * Update apps/web/components/ui/LinkText.tsx Co-authored-by: Omar López <[email protected]> Co-authored-by: Omar López <[email protected]>
This commit is contained in:
1 parent
5950b62b02
commit
1b541ff214
8 files changed
+177
-73
No files matched your search
@@ -70,7 +70,13 @@ const DestinationCalendarSelector = ({
|
||||
value: `${cal.integration}:${cal.externalId}`,
|
||||
})),
|
||||
})) ?? [];
|
||||
|
||||
const defaultCalendarSelectedString = destinationCalendar?.externalId
|
||||
? `(${
|
||||
destinationCalendar.externalId.length > 15
|
||||
? destinationCalendar.externalId.substring(0, 15) + "..."
|
||||
: destinationCalendar.externalId
|
||||
})`
|
||||
: "";
|
||||
return (
|
||||
<div className="relative" title={`${t("select_destination_calendar")}: ${selectedOption?.label || ""}`}>
|
||||
<Select
|
||||
@@ -79,8 +85,8 @@ const DestinationCalendarSelector = ({
|
||||
!hidePlaceholder ? (
|
||||
`${t("select_destination_calendar")}`
|
||||
) : (
|
||||
<span>
|
||||
{t("default_calendar_selected")} ({destinationCalendar?.externalId})
|
||||
<span className="whitespace-nowrap">
|
||||
{t("default_calendar_selected")} {defaultCalendarSelectedString}
|
||||
</span>
|
||||
)
|
||||
}
|
||||
@@ -104,7 +110,6 @@ const DestinationCalendarSelector = ({
|
||||
borderRadius: "2px",
|
||||
"@media only screen and (min-width: 640px)": {
|
||||
...(defaultStyles["@media only screen and (min-width: 640px)"] as object),
|
||||
maxWidth: "320px",
|
||||
},
|
||||
};
|
||||
},
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import Link from "next/link";
|
||||
|
||||
interface ILinkTextProps {
|
||||
href: string;
|
||||
children: React.ReactNode;
|
||||
classNameChildren?: string;
|
||||
}
|
||||
/**
|
||||
* This component had to be made in order to make i18n work with next/link
|
||||
* @see https://github.com/i18next/react-i18next/issues/1090#issuecomment-615426145
|
||||
**/
|
||||
export const LinkText = (props: ILinkTextProps) => {
|
||||
const { href, children, classNameChildren, ...moreProps } = props;
|
||||
return (
|
||||
<Link href={href || ""} {...moreProps}>
|
||||
<a className={classNameChildren}>{children}</a>
|
||||
</Link>
|
||||
);
|
||||
};
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
import { cleanup, screen, render } from "@testing-library/react";
|
||||
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
|
||||
import NoCalendarConnectedAlert from "..";
|
||||
|
||||
describe("Testing NoCalendarConnectedAlert", () => {
|
||||
describe("Render test", () => {
|
||||
it("should render without crashing", () => {
|
||||
// Disabled as its asking for full trpc useQuery response
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
jest.spyOn(trpc, "useQuery").mockReturnValue({
|
||||
isSuccess: true,
|
||||
isFetched: true,
|
||||
data: { connectedCalendars: [], destinationCalendar: undefined },
|
||||
});
|
||||
render(<NoCalendarConnectedAlert />);
|
||||
|
||||
const testProp = screen.getByText("missing_connected_calendar");
|
||||
expect(testProp).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should not render", () => {
|
||||
// Disabled as its asking for full trpc useQuery response
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore
|
||||
jest.spyOn(trpc, "useQuery").mockReturnValue({
|
||||
isSuccess: true,
|
||||
isFetched: true,
|
||||
data: { connectedCalendars: [1, 2], destinationCalendar: { a: "test" } },
|
||||
});
|
||||
render(<NoCalendarConnectedAlert />);
|
||||
|
||||
expect(screen.queryByText("missing_connected_calendar")).toBeNull();
|
||||
});
|
||||
|
||||
afterEach(cleanup);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
import { useTranslation } from "next-i18next";
|
||||
import { Trans } from "next-i18next";
|
||||
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
// @TODO: alert v2
|
||||
import { Alert } from "@calcom/ui/Alert";
|
||||
|
||||
import { LinkText } from "../LinkText";
|
||||
|
||||
const NoCalendarConnectedAlert = () => {
|
||||
const { t } = useTranslation();
|
||||
|
||||
const query = trpc.useQuery(["viewer.connectedCalendars"]);
|
||||
// We are not gonna show this alert till we fetch data from DB
|
||||
let defaultCalendarConnected = true;
|
||||
if (query.isSuccess && query.isFetched && query.data) {
|
||||
defaultCalendarConnected =
|
||||
query.data.connectedCalendars.length > 0 && query.data.destinationCalendar !== undefined;
|
||||
}
|
||||
return (
|
||||
<>
|
||||
{!defaultCalendarConnected && (
|
||||
<Alert
|
||||
severity="warning"
|
||||
className="mb-4"
|
||||
title={<>{t("missing_connected_calendar") as string}</>}
|
||||
message={
|
||||
<Trans i18nKey="connect_your_calendar_and_link">
|
||||
You can connect your calendar from
|
||||
<LinkText
|
||||
data-testid="no-calendar-connected-alert"
|
||||
href="/apps/categories/calendar"
|
||||
classNameChildren="underline">
|
||||
here
|
||||
</LinkText>
|
||||
.
|
||||
</Trans>
|
||||
}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
};
|
||||
export default NoCalendarConnectedAlert;
|
||||
@@ -7,7 +7,7 @@ const config: Config.InitialOptions = {
|
||||
verbose: true,
|
||||
roots: ["<rootDir>"],
|
||||
setupFiles: ["<rootDir>/test/jest-setup.js"],
|
||||
testMatch: ["**/test/lib/**/*.(spec|test).(ts|tsx|js)"],
|
||||
testMatch: ["**/test/lib/**/*.(spec|test).(ts|tsx|js)", "**/__tests__/**/*.(spec|test).(ts|tsx|js)"],
|
||||
testPathIgnorePatterns: ["<rootDir>/.next", "<rootDir>/playwright/"],
|
||||
transform: {
|
||||
"^.+\\.(js|jsx|ts|tsx)$": ["babel-jest", { presets: ["next/babel"] }],
|
||||
|
||||
@@ -128,6 +128,7 @@
|
||||
"@calcom/types": "*",
|
||||
"@microsoft/microsoft-graph-types-beta": "0.15.0-preview",
|
||||
"@playwright/test": "^1.25.0",
|
||||
"@testing-library/react": "^13.3.0",
|
||||
"@types/accept-language-parser": "1.5.2",
|
||||
"@types/async": "^3.2.15",
|
||||
"@types/bcryptjs": "^2.4.2",
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { UserPlan } from "@prisma/client";
|
||||
import { GetServerSidePropsContext, InferGetServerSidePropsType } from "next";
|
||||
import { Trans } from "next-i18next";
|
||||
import Head from "next/head";
|
||||
import Link from "next/link";
|
||||
@@ -9,7 +8,6 @@ import React, { Fragment, useEffect, useState } from "react";
|
||||
import { CAL_URL, WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import showToast from "@calcom/lib/notification";
|
||||
import prisma from "@calcom/prisma";
|
||||
import { inferQueryOutput, trpc } from "@calcom/trpc/react";
|
||||
import { Button } from "@calcom/ui";
|
||||
import { Alert } from "@calcom/ui/Alert";
|
||||
@@ -28,7 +26,6 @@ import Shell from "@calcom/ui/Shell";
|
||||
import { Tooltip } from "@calcom/ui/Tooltip";
|
||||
|
||||
import { withQuery } from "@lib/QueryCell";
|
||||
import { getSession } from "@lib/auth";
|
||||
import classNames from "@lib/classNames";
|
||||
import { HttpError } from "@lib/core/http/error";
|
||||
|
||||
@@ -38,13 +35,13 @@ import EventTypeDescription from "@components/eventtype/EventTypeDescription";
|
||||
import SkeletonLoader from "@components/eventtype/SkeletonLoader";
|
||||
import Avatar from "@components/ui/Avatar";
|
||||
import AvatarGroup from "@components/ui/AvatarGroup";
|
||||
import { LinkText } from "@components/ui/LinkText";
|
||||
import NoCalendarConnectedAlert from "@components/ui/NoCalendarConnectedAlert";
|
||||
|
||||
import { TRPCClientError } from "@trpc/react";
|
||||
|
||||
type EventTypeGroups = inferQueryOutput<"viewer.eventTypes">["eventTypeGroups"];
|
||||
type EventTypeGroupProfile = EventTypeGroups[number]["profile"];
|
||||
type ConnectedCalendars = inferQueryOutput<"viewer.connectedCalendars">["connectedCalendars"][number];
|
||||
|
||||
interface EventTypeListHeadingProps {
|
||||
profile: EventTypeGroupProfile;
|
||||
membershipCount: number;
|
||||
@@ -59,17 +56,7 @@ interface EventTypeListProps {
|
||||
types: EventType[];
|
||||
}
|
||||
|
||||
const Item = ({
|
||||
type,
|
||||
group,
|
||||
readOnly,
|
||||
connectedCalendars,
|
||||
}: {
|
||||
type: EventType;
|
||||
group: EventTypeGroup;
|
||||
readOnly: boolean;
|
||||
connectedCalendars: ConnectedCalendars[] | undefined;
|
||||
}) => {
|
||||
const Item = ({ type, group, readOnly }: { type: EventType; group: EventTypeGroup; readOnly: boolean }) => {
|
||||
const { t } = useLocale();
|
||||
|
||||
return (
|
||||
@@ -216,8 +203,6 @@ export const EventTypeList = ({ group, groupIndex, readOnly, types }: EventTypeL
|
||||
}
|
||||
}, []);
|
||||
|
||||
const connectedCalendarsQuery = trpc.useQuery(["viewer.connectedCalendars"]);
|
||||
|
||||
return (
|
||||
<div className="-mx-4 mb-16 overflow-hidden rounded-sm border border-gray-200 bg-white sm:mx-0">
|
||||
<ul className="divide-y divide-neutral-200" data-testid="event-types">
|
||||
@@ -254,12 +239,7 @@ export const EventTypeList = ({ group, groupIndex, readOnly, types }: EventTypeL
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
<MemoizedItem
|
||||
type={type}
|
||||
group={group}
|
||||
readOnly={readOnly}
|
||||
connectedCalendars={connectedCalendarsQuery.data?.connectedCalendars}
|
||||
/>
|
||||
<MemoizedItem type={type} group={group} readOnly={readOnly} />
|
||||
<div className="mt-4 hidden flex-shrink-0 sm:mt-0 sm:ml-5 sm:flex">
|
||||
<div className="flex justify-between space-x-2 rtl:space-x-reverse">
|
||||
{type.users?.length > 1 && (
|
||||
@@ -571,9 +551,8 @@ const CTA = () => {
|
||||
|
||||
const WithQuery = withQuery(["viewer.eventTypes"]);
|
||||
|
||||
const EventTypesPage = (props: InferGetServerSidePropsType<typeof getServerSideProps>) => {
|
||||
const EventTypesPage = () => {
|
||||
const { t } = useLocale();
|
||||
const { defaultCalendarConnected } = props;
|
||||
|
||||
return (
|
||||
<div>
|
||||
@@ -597,31 +576,17 @@ const EventTypesPage = (props: InferGetServerSidePropsType<typeof getServerSideP
|
||||
message={
|
||||
<Trans i18nKey="plan_upgrade_instructions">
|
||||
You can
|
||||
<a href="/api/upgrade" className="underline">
|
||||
<LinkText href="/api/upgrade" classNameChildren="underline">
|
||||
upgrade here
|
||||
</a>
|
||||
</LinkText>
|
||||
.
|
||||
</Trans>
|
||||
}
|
||||
className="mb-4"
|
||||
/>
|
||||
)}
|
||||
{!defaultCalendarConnected && (
|
||||
<Alert
|
||||
severity="warning"
|
||||
className="mb-4"
|
||||
title={<>{t("missing_connected_calendar") as string}</>}
|
||||
message={
|
||||
<Trans i18nKey="connect_your_calendar_and_link">
|
||||
You can connect your calendar from
|
||||
<a href="/apps/categories/calendar" className="underline">
|
||||
here
|
||||
</a>
|
||||
.
|
||||
</Trans>
|
||||
}
|
||||
/>
|
||||
)}
|
||||
|
||||
<NoCalendarConnectedAlert />
|
||||
|
||||
{data.eventTypeGroups.map((group, index) => (
|
||||
<Fragment key={group.profile.slug}>
|
||||
@@ -652,26 +617,4 @@ const EventTypesPage = (props: InferGetServerSidePropsType<typeof getServerSideP
|
||||
);
|
||||
};
|
||||
|
||||
export async function getServerSideProps(context: GetServerSidePropsContext) {
|
||||
const session = await getSession(context);
|
||||
let defaultCalendarConnected = false;
|
||||
|
||||
if (session && session.user) {
|
||||
const defaultCalendar = await prisma.destinationCalendar.findFirst({
|
||||
where: {
|
||||
userId: session.user.id,
|
||||
credentialId: {
|
||||
not: null,
|
||||
},
|
||||
},
|
||||
});
|
||||
defaultCalendarConnected = !!defaultCalendar;
|
||||
}
|
||||
return {
|
||||
props: {
|
||||
defaultCalendarConnected,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export default EventTypesPage;
|
||||
Reference in new issue
Block a user