-
Notifications
You must be signed in to change notification settings - Fork 8.6k
/
Copy path_app.tsx
141 lines (129 loc) · 4.81 KB
/
_app.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import type { Data } from "@/pages/api/get-managed-users";
import "@/styles/globals.css";
import type { AppProps } from "next/app";
import { Poppins } from "next/font/google";
import { usePathname } from "next/navigation";
import { useRouter } from "next/navigation";
import { useEffect, useState } from "react";
import Select from "react-select";
import { CalProvider, BookerEmbed, Router } from "@calcom/atoms";
import "@calcom/atoms/globals.min.css";
const poppins = Poppins({ subsets: ["latin"], weight: ["400", "800"] });
type TUser = Data["users"][0];
function generateRandomEmail() {
const localPartLength = 10;
const domain = ["example.com", "example.net", "example.org"];
const randomLocalPart = Array.from({ length: localPartLength }, () =>
String.fromCharCode(Math.floor(Math.random() * 26) + 97)
).join("");
const randomDomain = domain[Math.floor(Math.random() * domain.length)];
return `${randomLocalPart}@${randomDomain}`;
}
// note(Lauris): needed because useEffect kicks in twice creating 2 parallel requests
let seeding = false;
export default function App({ Component, pageProps }: AppProps) {
const [accessToken, setAccessToken] = useState("");
const [email, setUserEmail] = useState("");
const [username, setUsername] = useState("");
const [selectedUser, setSelectedUser] = useState<TUser | null>(null);
const [options, setOptions] = useState([]);
const router = useRouter();
const pathname = usePathname();
useEffect(() => {
fetch("/api/get-managed-users", {
method: "get",
}).then(async (res) => {
const data = await res.json();
setOptions(
data.users.map((item: Data["users"][0]) => ({ ...item, value: item.id, label: item.username }))
);
});
}, []);
useEffect(() => {
const randomEmailOne = generateRandomEmail();
const randomEmailTwo = generateRandomEmail();
if (!seeding) {
seeding = true;
fetch("/api/managed-user", {
method: "POST",
body: JSON.stringify({ emails: [randomEmailOne, randomEmailTwo] }),
}).then(async (res) => {
const data = await res.json();
setAccessToken(data.accessToken);
setUserEmail(data.email);
setUsername(data.username);
});
}
}, []);
useEffect(() => {
if (!!selectedUser) {
setAccessToken(selectedUser.accessToken);
setUserEmail(selectedUser.email);
setUsername(selectedUser.username);
}
}, [selectedUser]);
return (
<div className={`${poppins.className} text-black`}>
{options.length > 0 && (
<Select defaultValue={selectedUser} onChange={setSelectedUser} options={options} />
)}
<CalProvider
accessToken={accessToken}
// eslint-disable-next-line turbo/no-undeclared-env-vars
clientId={process.env.NEXT_PUBLIC_X_CAL_ID ?? ""}
// eslint-disable-next-line turbo/no-undeclared-env-vars
options={{ apiUrl: process.env.NEXT_PUBLIC_CALCOM_API_URL ?? "", refreshUrl: "/api/refresh" }}>
{email ? (
<>
<Component {...pageProps} calUsername={username} calEmail={email} />
</>
) : (
<>
<main className={`flex min-h-screen flex-col items-center justify-between p-24 `}>
<div className="z-10 w-full max-w-5xl items-center justify-between font-mono text-sm lg:flex" />
</main>
</>
)}
</CalProvider>{" "}
{pathname === "/embed" && (
<div>
<BookerEmbed
customClassNames={{
bookerContainer: "!bg-[#F5F2FE] [&_button:!rounded-full] border-subtle border",
datePickerCustomClassNames: {
datePickerDatesActive: "!bg-[#D7CEF5]",
},
eventMetaCustomClassNames: {
eventMetaTitle: "text-[#7151DC]",
},
availableTimeSlotsCustomClassNames: {
availableTimeSlotsHeaderContainer: "!bg-[#F5F2FE]",
availableTimes: "!bg-[#D7CEF5]",
},
}}
username={username}
eventSlug="thirty-minutes"
onCreateBookingSuccess={(data) => {
router.push(`/${data.data.uid}`);
}}
/>
</div>
)}
{pathname === "/router" && (
<div className="p-4">
<Router
formId="a63e6fce-899a-404e-8c38-e069710589c5"
formResponsesURLParams={new URLSearchParams({ isBookingDryRun: "true", Territory: "Europe" })}
onDisplayBookerEmbed={() => {
console.log("render booker embed");
}}
bookerBannerUrl="https://i0.wp.com/mahala.co.uk/wp-content/uploads/2014/12/img_banner-thin_mountains.jpg?fit=800%2C258&ssl=1"
bookerCustomClassNames={{
bookerWrapper: "dark",
}}
/>
</div>
)}
</div>
);
}