From 7c67c5b31290515d4b7c3bbe7c3c82491a8dffc4 Mon Sep 17 00:00:00 2001 From: Elias Schneider Date: Sun, 8 May 2022 14:16:19 +0200 Subject: [PATCH] Improve share error handling --- functions/cleanShares/src/index.js | 1 - functions/createShare/src/index.js | 2 +- ...reNotFoundModal.tsx => showErrorModal.tsx} | 18 +++++---- .../share/showVisitorLimitExceededModal.tsx | 39 ------------------- src/pages/share/[shareId].tsx | 23 +++++++++-- 5 files changed, 30 insertions(+), 53 deletions(-) rename src/components/share/{showShareNotFoundModal.tsx => showErrorModal.tsx} (68%) delete mode 100644 src/components/share/showVisitorLimitExceededModal.tsx diff --git a/functions/cleanShares/src/index.js b/functions/cleanShares/src/index.js index 4433027..d0de1e0 100644 --- a/functions/cleanShares/src/index.js +++ b/functions/cleanShares/src/index.js @@ -4,7 +4,6 @@ module.exports = async function (req, res) { const client = new sdk.Client(); let database = new sdk.Database(client); - let storage = new sdk.Storage(client); client diff --git a/functions/createShare/src/index.js b/functions/createShare/src/index.js index 01ef8f5..b665d78 100644 --- a/functions/createShare/src/index.js +++ b/functions/createShare/src/index.js @@ -35,7 +35,7 @@ module.exports = async function (req, res) { } let userIds; - if (payload.emails) { + if (payload.emails.length > 0) { const creatorEmail = (await users.get(userId)).email userIds = [] userIds.push(userId) diff --git a/src/components/share/showShareNotFoundModal.tsx b/src/components/share/showErrorModal.tsx similarity index 68% rename from src/components/share/showShareNotFoundModal.tsx rename to src/components/share/showErrorModal.tsx index b64241f..f80bbc5 100644 --- a/src/components/share/showShareNotFoundModal.tsx +++ b/src/components/share/showErrorModal.tsx @@ -3,26 +3,28 @@ import { useModals } from "@mantine/modals"; import { ModalsContextProps } from "@mantine/modals/lib/context"; import { useRouter } from "next/router"; -const showShareNotFoundModal = (modals: ModalsContextProps) => { +const showErrorModal = ( + modals: ModalsContextProps, + title: string, + text: string +) => { return modals.openModal({ closeOnClickOutside: false, withCloseButton: false, closeOnEscape: false, - title: Not found, + title: {title}, - children: , + children: , }); }; -const Body = () => { +const Body = ({ text }: { text: string }) => { const modals = useModals(); const router = useRouter(); return ( <> - - This share can't be found. Please check your link. - + {text} - - - ); -}; - -export default showVisitorLimitExceededModal; diff --git a/src/pages/share/[shareId].tsx b/src/pages/share/[shareId].tsx index 1242ebd..8657ede 100644 --- a/src/pages/share/[shareId].tsx +++ b/src/pages/share/[shareId].tsx @@ -6,8 +6,7 @@ import Meta from "../../components/Meta"; import DownloadAllButton from "../../components/share/DownloadAllButton"; import FileList from "../../components/share/FileList"; import showEnterPasswordModal from "../../components/share/showEnterPasswordModal"; -import showShareNotFoundModal from "../../components/share/showShareNotFoundModal"; -import showVisitorLimitExceededModal from "../../components/share/showVisitorLimitExceededModal"; +import showErrorModal from "../../components/share/showErrorModal"; import authService from "../../services/auth.service"; import shareService from "../../services/share.service"; import { AppwriteFileWithPreview } from "../../types/File.type"; @@ -40,11 +39,27 @@ const Share = () => { .catch((e) => { const error = e.response.data.message; if (e.response.status == 404) { - showShareNotFoundModal(modals); + showErrorModal( + modals, + "Not found", + "This share can't be found. Please check your link." + ); } else if (error == "password_required") { showEnterPasswordModal(modals, submitPassword); } else if (error == "visitor_limit_exceeded") { - showVisitorLimitExceededModal(modals); + showErrorModal( + modals, + "Visitor limit exceeded", + "The visitor limit from this share has been exceeded." + ); + } else if (error == "forbidden") { + showErrorModal( + modals, + "Forbidden", + "You're not allowed to see this share. Are you logged in with the correct account?" + ); + } else { + showErrorModal(modals, "Error", "An unknown error occurred."); } }); };