2023-10-04 13:25:29 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
2023-05-30 13:44:35 +00:00
|
|
|
import Image from "next/image";
|
|
|
|
import type { NextPage } from "next";
|
2023-10-04 13:25:29 +00:00
|
|
|
import { useTheme } from "next-themes";
|
|
|
|
import useSWR from "swr";
|
|
|
|
import { observer } from "mobx-react-lite";
|
2023-05-30 13:44:35 +00:00
|
|
|
// layouts
|
|
|
|
import DefaultLayout from "layouts/default-layout";
|
2023-07-17 07:30:44 +00:00
|
|
|
// services
|
|
|
|
import authenticationService from "services/authentication.service";
|
2023-10-04 13:25:29 +00:00
|
|
|
import { AppConfigService } from "services/app-config.service";
|
2023-05-30 13:44:35 +00:00
|
|
|
// hooks
|
|
|
|
import useUserAuth from "hooks/use-user-auth";
|
|
|
|
import useToast from "hooks/use-toast";
|
2023-07-17 07:30:44 +00:00
|
|
|
// components
|
2023-05-30 13:44:35 +00:00
|
|
|
import {
|
|
|
|
GoogleLoginButton,
|
|
|
|
GithubLoginButton,
|
|
|
|
EmailCodeForm,
|
|
|
|
EmailPasswordForm,
|
2023-10-04 13:25:29 +00:00
|
|
|
EmailResetPasswordForm,
|
2023-05-30 13:44:35 +00:00
|
|
|
} from "components/account";
|
2023-04-08 18:32:33 +00:00
|
|
|
// ui
|
|
|
|
import { Spinner } from "components/ui";
|
2023-07-17 07:30:44 +00:00
|
|
|
// images
|
|
|
|
import BluePlaneLogoWithoutText from "public/plane-logos/blue-without-text.png";
|
2023-08-10 07:33:42 +00:00
|
|
|
// mobx store
|
|
|
|
import { useMobxStore } from "lib/mobx/store-provider";
|
2023-10-04 13:25:29 +00:00
|
|
|
// types
|
2023-08-10 07:33:42 +00:00
|
|
|
import { IUser } from "types";
|
|
|
|
|
2023-10-04 13:25:29 +00:00
|
|
|
const appConfig = new AppConfigService();
|
|
|
|
|
2023-06-16 13:30:04 +00:00
|
|
|
// types
|
|
|
|
type EmailPasswordFormValues = {
|
|
|
|
email: string;
|
|
|
|
password?: string;
|
|
|
|
medium?: string;
|
|
|
|
};
|
2023-04-13 10:08:00 +00:00
|
|
|
|
2023-08-10 07:33:42 +00:00
|
|
|
const HomePage: NextPage = observer(() => {
|
|
|
|
const store: any = useMobxStore();
|
2023-10-04 13:25:29 +00:00
|
|
|
// theme
|
2023-08-10 07:33:42 +00:00
|
|
|
const { setTheme } = useTheme();
|
2023-10-04 13:25:29 +00:00
|
|
|
// user
|
2023-07-17 07:30:44 +00:00
|
|
|
const { isLoading, mutateUser } = useUserAuth("sign-in");
|
2023-10-04 13:25:29 +00:00
|
|
|
// states
|
|
|
|
const [isResettingPassword, setIsResettingPassword] = useState(false);
|
|
|
|
// toast
|
2023-05-30 13:44:35 +00:00
|
|
|
const { setToastAlert } = useToast();
|
2023-10-04 13:25:29 +00:00
|
|
|
// fetch app config
|
|
|
|
const { data } = useSWR("APP_CONFIG", () => appConfig.envConfig());
|
2023-04-12 13:33:08 +00:00
|
|
|
|
2023-08-10 07:33:42 +00:00
|
|
|
const handleTheme = (user: IUser) => {
|
|
|
|
const currentTheme = user.theme.theme ?? "system";
|
|
|
|
setTheme(currentTheme);
|
|
|
|
store?.user?.setCurrentUserSettings();
|
2023-08-03 09:58:22 +00:00
|
|
|
};
|
|
|
|
|
2023-05-30 13:44:35 +00:00
|
|
|
const handleGoogleSignIn = async ({ clientId, credential }: any) => {
|
|
|
|
try {
|
|
|
|
if (clientId && credential) {
|
2023-06-09 19:56:38 +00:00
|
|
|
const socialAuthPayload = {
|
|
|
|
medium: "google",
|
|
|
|
credential,
|
|
|
|
clientId,
|
|
|
|
};
|
|
|
|
const response = await authenticationService.socialAuth(socialAuthPayload);
|
2023-08-03 09:58:22 +00:00
|
|
|
if (response && response?.user) {
|
|
|
|
mutateUser();
|
2023-08-10 07:33:42 +00:00
|
|
|
handleTheme(response?.user);
|
2023-08-03 09:58:22 +00:00
|
|
|
}
|
2023-04-13 10:08:00 +00:00
|
|
|
} else {
|
2023-05-30 13:44:35 +00:00
|
|
|
throw Error("Cant find credentials");
|
2023-04-13 10:08:00 +00:00
|
|
|
}
|
2023-06-23 14:00:32 +00:00
|
|
|
} catch (err: any) {
|
2023-05-30 13:44:35 +00:00
|
|
|
setToastAlert({
|
|
|
|
title: "Error signing in!",
|
|
|
|
type: "error",
|
2023-06-13 09:07:25 +00:00
|
|
|
message:
|
2023-06-23 14:00:32 +00:00
|
|
|
err?.error || "Something went wrong. Please try again later or contact the support team.",
|
2023-05-30 13:44:35 +00:00
|
|
|
});
|
2023-04-08 18:32:33 +00:00
|
|
|
}
|
2023-05-30 13:44:35 +00:00
|
|
|
};
|
2023-04-13 10:08:00 +00:00
|
|
|
|
2023-07-17 07:30:44 +00:00
|
|
|
const handleGitHubSignIn = async (credential: string) => {
|
2023-05-30 13:44:35 +00:00
|
|
|
try {
|
|
|
|
if (process.env.NEXT_PUBLIC_GITHUB_ID && credential) {
|
2023-06-09 19:56:38 +00:00
|
|
|
const socialAuthPayload = {
|
|
|
|
medium: "github",
|
|
|
|
credential,
|
|
|
|
clientId: process.env.NEXT_PUBLIC_GITHUB_ID,
|
|
|
|
};
|
|
|
|
const response = await authenticationService.socialAuth(socialAuthPayload);
|
2023-08-03 09:58:22 +00:00
|
|
|
if (response && response?.user) {
|
|
|
|
mutateUser();
|
2023-08-10 07:33:42 +00:00
|
|
|
handleTheme(response?.user);
|
2023-08-03 09:58:22 +00:00
|
|
|
}
|
2023-05-30 13:44:35 +00:00
|
|
|
} else {
|
|
|
|
throw Error("Cant find credentials");
|
|
|
|
}
|
2023-06-23 14:00:32 +00:00
|
|
|
} catch (err: any) {
|
2023-05-30 13:44:35 +00:00
|
|
|
setToastAlert({
|
|
|
|
title: "Error signing in!",
|
|
|
|
type: "error",
|
2023-06-13 09:07:25 +00:00
|
|
|
message:
|
2023-06-23 14:00:32 +00:00
|
|
|
err?.error || "Something went wrong. Please try again later or contact the support team.",
|
2023-05-30 13:44:35 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2022-11-19 14:21:26 +00:00
|
|
|
|
2023-06-16 13:30:04 +00:00
|
|
|
const handlePasswordSignIn = async (formData: EmailPasswordFormValues) => {
|
|
|
|
await authenticationService
|
|
|
|
.emailLogin(formData)
|
|
|
|
.then((response) => {
|
|
|
|
try {
|
2023-08-03 09:58:22 +00:00
|
|
|
if (response) {
|
|
|
|
mutateUser();
|
2023-08-10 07:33:42 +00:00
|
|
|
handleTheme(response?.user);
|
2023-08-03 09:58:22 +00:00
|
|
|
}
|
2023-06-23 14:00:32 +00:00
|
|
|
} catch (err: any) {
|
2023-06-16 13:30:04 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
2023-06-23 14:00:32 +00:00
|
|
|
title: "Error!",
|
2023-06-16 13:30:04 +00:00
|
|
|
message:
|
2023-06-23 14:00:32 +00:00
|
|
|
err?.error ||
|
2023-06-16 13:30:04 +00:00
|
|
|
"Something went wrong. Please try again later or contact the support team.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
2023-06-23 14:00:32 +00:00
|
|
|
.catch((err) =>
|
2023-06-16 13:30:04 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
2023-06-23 14:00:32 +00:00
|
|
|
title: "Error!",
|
|
|
|
message:
|
|
|
|
err?.error ||
|
|
|
|
"Something went wrong. Please try again later or contact the support team.",
|
2023-06-16 13:30:04 +00:00
|
|
|
})
|
|
|
|
);
|
2023-06-13 09:07:25 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleEmailCodeSignIn = async (response: any) => {
|
|
|
|
try {
|
2023-08-03 09:58:22 +00:00
|
|
|
if (response) {
|
|
|
|
mutateUser();
|
2023-08-10 07:33:42 +00:00
|
|
|
handleTheme(response?.user);
|
2023-08-03 09:58:22 +00:00
|
|
|
}
|
2023-06-23 14:00:32 +00:00
|
|
|
} catch (err: any) {
|
2023-06-13 09:07:25 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
2023-06-23 14:00:32 +00:00
|
|
|
title: "Error!",
|
2023-06-13 09:07:25 +00:00
|
|
|
message:
|
2023-06-23 14:00:32 +00:00
|
|
|
err?.error || "Something went wrong. Please try again later or contact the support team.",
|
2023-06-05 12:18:29 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-08-03 09:58:22 +00:00
|
|
|
useEffect(() => {
|
|
|
|
setTheme("system");
|
|
|
|
}, [setTheme]);
|
|
|
|
|
2023-04-08 18:32:33 +00:00
|
|
|
return (
|
2023-05-30 13:44:35 +00:00
|
|
|
<DefaultLayout>
|
|
|
|
{isLoading ? (
|
2023-07-17 07:30:44 +00:00
|
|
|
<div className="grid place-items-center h-screen">
|
|
|
|
<Spinner />
|
2023-05-30 13:44:35 +00:00
|
|
|
</div>
|
|
|
|
) : (
|
2023-07-17 07:30:44 +00:00
|
|
|
<>
|
|
|
|
<>
|
|
|
|
<div className="hidden sm:block sm:fixed border-r-[0.5px] border-custom-border-200 h-screen w-[0.5px] top-0 left-20 lg:left-32" />
|
|
|
|
<div className="fixed grid place-items-center bg-custom-background-100 sm:py-5 top-11 sm:top-12 left-7 sm:left-16 lg:left-28">
|
|
|
|
<div className="grid place-items-center bg-custom-background-100">
|
|
|
|
<div className="h-[30px] w-[30px]">
|
|
|
|
<Image src={BluePlaneLogoWithoutText} alt="Plane Logo" />
|
2023-05-30 13:44:35 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-07-17 07:30:44 +00:00
|
|
|
</div>
|
|
|
|
</>
|
2023-07-18 13:32:33 +00:00
|
|
|
<div className="grid place-items-center h-full overflow-y-auto py-5 px-7">
|
2023-07-17 07:30:44 +00:00
|
|
|
<div>
|
2023-10-04 13:25:29 +00:00
|
|
|
<h1 className="text-center text-2xl sm:text-2.5xl font-semibold text-custom-text-100">
|
|
|
|
{isResettingPassword ? "Reset your password" : "Sign in to Plane"}
|
|
|
|
</h1>
|
|
|
|
{isResettingPassword ? (
|
|
|
|
<EmailResetPasswordForm setIsResettingPassword={setIsResettingPassword} />
|
|
|
|
) : (
|
2023-07-17 07:30:44 +00:00
|
|
|
<>
|
2023-10-04 13:25:29 +00:00
|
|
|
{data?.email_password_login && (
|
|
|
|
<EmailPasswordForm
|
|
|
|
onSubmit={handlePasswordSignIn}
|
|
|
|
setIsResettingPassword={setIsResettingPassword}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{data?.magic_login && (
|
|
|
|
<div className="flex flex-col divide-y divide-custom-border-200">
|
|
|
|
<div className="pb-7">
|
|
|
|
<EmailCodeForm handleSignIn={handleEmailCodeSignIn} />
|
|
|
|
</div>
|
2023-05-30 13:44:35 +00:00
|
|
|
</div>
|
2023-10-04 13:25:29 +00:00
|
|
|
)}
|
|
|
|
<div className="flex flex-col items-center justify-center gap-4 pt-7 sm:w-[360px] mx-auto overflow-hidden">
|
|
|
|
{data?.google && (
|
|
|
|
<GoogleLoginButton
|
|
|
|
clientId={data?.google}
|
|
|
|
handleSignIn={handleGoogleSignIn}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{data?.github && (
|
|
|
|
<GithubLoginButton
|
|
|
|
clientId={data?.github}
|
|
|
|
handleSignIn={handleGitHubSignIn}
|
|
|
|
/>
|
|
|
|
)}
|
2023-07-17 07:30:44 +00:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
2023-10-04 13:25:29 +00:00
|
|
|
<p className="pt-16 text-custom-text-200 text-sm text-center">
|
|
|
|
By signing up, you agree to the{" "}
|
|
|
|
<a
|
|
|
|
href="https://plane.so/terms-and-conditions"
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
className="font-medium underline"
|
|
|
|
>
|
|
|
|
Terms & Conditions
|
|
|
|
</a>
|
|
|
|
</p>
|
2023-05-30 13:44:35 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2023-07-17 07:30:44 +00:00
|
|
|
</>
|
2023-05-30 13:44:35 +00:00
|
|
|
)}
|
|
|
|
</DefaultLayout>
|
2023-04-08 18:32:33 +00:00
|
|
|
);
|
2023-08-10 07:33:42 +00:00
|
|
|
});
|
2022-11-19 14:21:26 +00:00
|
|
|
|
2023-05-30 13:44:35 +00:00
|
|
|
export default HomePage;
|