From 080fb5deeac1d88f4badc05475a659b190ef3147 Mon Sep 17 00:00:00 2001 From: dakshesh14 Date: Thu, 7 Sep 2023 14:12:01 +0530 Subject: [PATCH] fix: made editor authenticate route --- web/layouts/web-view-layout/index.tsx | 55 +++++++++++++ web/pages/[workspaceSlug]/editor.tsx | 86 -------------------- web/pages/m/[workspaceSlug]/editor.tsx | 104 +++++++++++++++++++++++++ yarn.lock | 12 +-- 4 files changed, 160 insertions(+), 97 deletions(-) create mode 100644 web/layouts/web-view-layout/index.tsx delete mode 100644 web/pages/[workspaceSlug]/editor.tsx create mode 100644 web/pages/m/[workspaceSlug]/editor.tsx diff --git a/web/layouts/web-view-layout/index.tsx b/web/layouts/web-view-layout/index.tsx new file mode 100644 index 000000000..2897203eb --- /dev/null +++ b/web/layouts/web-view-layout/index.tsx @@ -0,0 +1,55 @@ +// swr +import useSWR from "swr"; + +// services +import userService from "services/user.service"; + +// fetch keys +import { CURRENT_USER } from "constants/fetch-keys"; + +// icons +import { AlertCircle } from "lucide-react"; + +// ui +import { Spinner } from "components/ui"; + +type Props = { + fullScreen?: boolean; + children: React.ReactNode; +}; + +const WebViewLayout: React.FC = ({ children, fullScreen = false }) => { + const { data: currentUser, error } = useSWR(CURRENT_USER, () => userService.currentUser()); + + if (!currentUser && !error) { + return ( +
+
+

Loading your profile...

+ +
+
+ ); + } + + return ( +
+ {error ? ( +
+ +

You are not authorized to view this page.

+
+ ) : ( + children + )} +
+ ); +}; + +export default WebViewLayout; diff --git a/web/pages/[workspaceSlug]/editor.tsx b/web/pages/[workspaceSlug]/editor.tsx deleted file mode 100644 index 0f2b791b9..000000000 --- a/web/pages/[workspaceSlug]/editor.tsx +++ /dev/null @@ -1,86 +0,0 @@ -import { TipTapEditor } from "components/tiptap"; -import type { NextPage } from "next"; -import { useEffect, useState } from "react"; -import { Controller, useForm } from "react-hook-form"; -import { PrimaryButton, Spinner } from "components/ui"; -import { useRouter } from "next/router"; -import Cookies from "js-cookie"; -const Editor: NextPage = () => { - const [isLoading, setIsLoading] = useState("false"); - const router = useRouter(); - const { workspaceSlug, editable } = router.query; - const { - watch, - setValue, - control, - formState: { errors }, - } = useForm({ - defaultValues: { - data: "", - data_html: "", - }, - }); - - useEffect(() => { - setIsLoading("true"); - if (!router.query["editable"]) return; - setIsLoading("false"); - const data_html = Cookies.get("data_html"); - setValue("data_html", data_html ?? ""); - }, [router.query, setValue]); - - return isLoading === "true" ? ( -
- -
- ) : ( -
- ( - { - onChange(description_html); - setValue("data_html", description_html); - setValue("data", JSON.stringify(description)); - }} - /> - )} - /> - {editable === "true" ? ( - { - console.log( - "submitted", - JSON.stringify({ - data_html: watch("data_html"), - }) - ); - }} - > - Submit - - ) : ( - <> - )} -
- ); -}; - -export default Editor; diff --git a/web/pages/m/[workspaceSlug]/editor.tsx b/web/pages/m/[workspaceSlug]/editor.tsx new file mode 100644 index 000000000..296b05ac9 --- /dev/null +++ b/web/pages/m/[workspaceSlug]/editor.tsx @@ -0,0 +1,104 @@ +import { useEffect, useState } from "react"; + +// next +import type { NextPage } from "next"; +import { useRouter } from "next/router"; + +// cookies +import Cookies from "js-cookie"; + +// react-hook-form +import { Controller, useForm } from "react-hook-form"; + +// layouts +import WebViewLayout from "layouts/web-view-layout"; + +// components +import { TipTapEditor } from "components/tiptap"; +import { PrimaryButton, Spinner } from "components/ui"; + +const Editor: NextPage = () => { + const [isLoading, setIsLoading] = useState(false); + + const router = useRouter(); + const { workspaceSlug, editable } = router.query; + + const isEditable = editable === "true"; + + const { + watch, + setValue, + control, + formState: { errors }, + } = useForm({ + defaultValues: { + data: "", + data_html: "", + }, + }); + + useEffect(() => { + setIsLoading(true); + if (!isEditable) return; + setIsLoading(false); + const data_html = Cookies.get("data_html"); + setValue("data_html", data_html ?? ""); + }, [isEditable, setValue]); + + return ( + + {isLoading ? ( +
+ +
+ ) : ( + <> + ( + { + onChange(description_html); + setValue("data_html", description_html); + setValue("data", JSON.stringify(description)); + }} + /> + )} + /> + {isEditable && ( + { + console.log( + "submitted", + JSON.stringify({ + data_html: watch("data_html"), + }) + ); + }} + > + Submit + + )} + + )} +
+ ); +}; + +export default Editor; diff --git a/yarn.lock b/yarn.lock index 1aa8e5bde..c0b96cdb9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1367,7 +1367,7 @@ resolved "https://registry.yarnpkg.com/@mui/core-downloads-tracker/-/core-downloads-tracker-5.14.7.tgz#95bed2487bf59632125a13b8eb8f4c21e460afec" integrity sha512-sCWTUNElBPgB30iLvWe3PU7SIlTKZNf6/E/sko85iHVeHCM6WPkDw+y89CrZYjhFNmPqt2fIQM/pZu+rP2lFLA== -"@mui/icons-material@^5.14.1", "@mui/icons-material@^5.14.7": +"@mui/icons-material@^5.14.1": version "5.14.7" resolved "https://registry.yarnpkg.com/@mui/icons-material/-/icons-material-5.14.7.tgz#d7f6bd188fe38adf35c89d9343b8a529c2306383" integrity sha512-mWp4DwMa8c1Gx9yOEtPgxM4b+e6hAbtZyzfSubdBwrnEE6G5D2rbAJ5MB+If6kfI48JaYaJ5j8+zAdmZLuZc0A== @@ -4892,11 +4892,6 @@ https-proxy-agent@^5.0.0: agent-base "6" debug "4" -husky@^8.0.3: - version "8.0.3" - resolved "https://registry.yarnpkg.com/husky/-/husky-8.0.3.tgz#4936d7212e46d1dea28fef29bb3a108872cd9184" - integrity sha512-+dQSyqPh4x1hlO1swXBiNb2HzTDN1I2IGLQx1GrBuiqFJfoMrnZWwVmatvSiO+Iz8fBUnf+lekwNo4c2LlXItg== - idb@^7.0.1: version "7.1.1" resolved "https://registry.yarnpkg.com/idb/-/idb-7.1.1.tgz#d910ded866d32c7ced9befc5bfdf36f572ced72b" @@ -6046,11 +6041,6 @@ next-pwa@^5.6.0: workbox-webpack-plugin "^6.5.4" workbox-window "^6.5.4" -next-theme@^0.1.5: - version "0.1.5" - resolved "https://registry.yarnpkg.com/next-theme/-/next-theme-0.1.5.tgz#aa6655c516892925e577349d7715a8ed54bad727" - integrity sha512-WR8UCLEFjWvRl+UO2lTM4pGo7R4jzGZqQ6YL3hiL1Ns587Qb91GhJZLPu/Aa4ExtGQ/5wlcDX8zDYZoCN9oDPw== - next-themes@^0.2.1: version "0.2.1" resolved "https://registry.yarnpkg.com/next-themes/-/next-themes-0.2.1.tgz#0c9f128e847979daf6c67f70b38e6b6567856e45"