From 1b9dafbe476cd4c6c9e1a81583029d7daf44f81c Mon Sep 17 00:00:00 2001 From: Isaac Wise Date: Mon, 22 Jul 2024 17:39:38 -0500 Subject: [PATCH 1/2] Handle 400 error code when accesing a non public collection --- lib/client/getPublicCollectionData.ts | 2 ++ pages/public/collections/[id].tsx | 40 ++++++++++++++------------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/lib/client/getPublicCollectionData.ts b/lib/client/getPublicCollectionData.ts index 283733b..85deea6 100644 --- a/lib/client/getPublicCollectionData.ts +++ b/lib/client/getPublicCollectionData.ts @@ -9,6 +9,8 @@ const getPublicCollectionData = async ( ) => { const res = await fetch("/api/v1/public/collections/" + collectionId); + if (res.status === 400) return { response: "Collection not found.", status: 400 }; + const data = await res.json(); setData(data.response); diff --git a/pages/public/collections/[id].tsx b/pages/public/collections/[id].tsx index 47e074f..efe0554 100644 --- a/pages/public/collections/[id].tsx +++ b/pages/public/collections/[id].tsx @@ -68,12 +68,15 @@ export default function PublicCollections() { searchByTags: searchFilter.tags, }); - const [collection, setCollection] = - useState(); + const [collection, setCollection] = useState(); useEffect(() => { if (router.query.id) { - getPublicCollectionData(Number(router.query.id), setCollection); + getPublicCollectionData(Number(router.query.id), setCollection).then((res) => { + if (res.status === 400) { + router.push("/dashboard"); + } + }) } }, [collections]); @@ -108,9 +111,8 @@ export default function PublicCollections() {
{collection ? ( @@ -181,20 +183,20 @@ export default function PublicCollections() {

{collection.members.length > 0 && - collection.members.length === 1 + collection.members.length === 1 ? t("by_author_and_other", { + author: collectionOwner.name, + count: collection.members.length, + }) + : collection.members.length > 0 && + collection.members.length !== 1 + ? t("by_author_and_others", { author: collectionOwner.name, count: collection.members.length, }) - : collection.members.length > 0 && - collection.members.length !== 1 - ? t("by_author_and_others", { - author: collectionOwner.name, - count: collection.members.length, - }) : t("by_author", { - author: collectionOwner.name, - })} + author: collectionOwner.name, + })}

@@ -218,11 +220,11 @@ export default function PublicCollections() { placeholder={ collection._count?.links === 1 ? t("search_count_link", { - count: collection._count?.links, - }) + count: collection._count?.links, + }) : t("search_count_links", { - count: collection._count?.links, - }) + count: collection._count?.links, + }) } /> From 7d43ed52a4fe0dc32e1cfe999ebe693aac6cd1cc Mon Sep 17 00:00:00 2001 From: Isaac Wise Date: Mon, 22 Jul 2024 17:50:24 -0500 Subject: [PATCH 2/2] format --- .../migration/importFromHTMLFile.ts | 4 +- lib/client/getPublicCollectionData.ts | 3 +- next-i18next.config.js | 2 +- pages/public/collections/[id].tsx | 44 ++++++++++--------- 4 files changed, 29 insertions(+), 24 deletions(-) diff --git a/lib/api/controllers/migration/importFromHTMLFile.ts b/lib/api/controllers/migration/importFromHTMLFile.ts index b2b58a1..59a2787 100644 --- a/lib/api/controllers/migration/importFromHTMLFile.ts +++ b/lib/api/controllers/migration/importFromHTMLFile.ts @@ -63,7 +63,8 @@ async function processBookmarks( ) as Element; if (collectionName) { - const collectionNameContent = (collectionName.children[0] as TextNode)?.content; + const collectionNameContent = (collectionName.children[0] as TextNode) + ?.content; if (collectionNameContent) { collectionId = await createCollection( userId, @@ -274,4 +275,3 @@ function processNodes(nodes: Node[]) { nodes.forEach(findAndProcessDL); return nodes; } - diff --git a/lib/client/getPublicCollectionData.ts b/lib/client/getPublicCollectionData.ts index 85deea6..a8a74a3 100644 --- a/lib/client/getPublicCollectionData.ts +++ b/lib/client/getPublicCollectionData.ts @@ -9,7 +9,8 @@ const getPublicCollectionData = async ( ) => { const res = await fetch("/api/v1/public/collections/" + collectionId); - if (res.status === 400) return { response: "Collection not found.", status: 400 }; + if (res.status === 400) + return { response: "Collection not found.", status: 400 }; const data = await res.json(); diff --git a/next-i18next.config.js b/next-i18next.config.js index 06c3d23..b78b3b8 100644 --- a/next-i18next.config.js +++ b/next-i18next.config.js @@ -2,7 +2,7 @@ module.exports = { i18n: { defaultLocale: "en", - locales: ["en","it"], + locales: ["en", "it"], }, reloadOnPrerender: process.env.NODE_ENV === "development", }; diff --git a/pages/public/collections/[id].tsx b/pages/public/collections/[id].tsx index efe0554..7dba911 100644 --- a/pages/public/collections/[id].tsx +++ b/pages/public/collections/[id].tsx @@ -68,15 +68,18 @@ export default function PublicCollections() { searchByTags: searchFilter.tags, }); - const [collection, setCollection] = useState(); + const [collection, setCollection] = + useState(); useEffect(() => { if (router.query.id) { - getPublicCollectionData(Number(router.query.id), setCollection).then((res) => { - if (res.status === 400) { - router.push("/dashboard"); + getPublicCollectionData(Number(router.query.id), setCollection).then( + (res) => { + if (res.status === 400) { + router.push("/dashboard"); + } } - }) + ); } }, [collections]); @@ -111,8 +114,9 @@ export default function PublicCollections() {
{collection ? ( @@ -183,20 +187,20 @@ export default function PublicCollections() {

{collection.members.length > 0 && - collection.members.length === 1 + collection.members.length === 1 ? t("by_author_and_other", { - author: collectionOwner.name, - count: collection.members.length, - }) - : collection.members.length > 0 && - collection.members.length !== 1 - ? t("by_author_and_others", { author: collectionOwner.name, count: collection.members.length, }) + : collection.members.length > 0 && + collection.members.length !== 1 + ? t("by_author_and_others", { + author: collectionOwner.name, + count: collection.members.length, + }) : t("by_author", { - author: collectionOwner.name, - })} + author: collectionOwner.name, + })}

@@ -220,11 +224,11 @@ export default function PublicCollections() { placeholder={ collection._count?.links === 1 ? t("search_count_link", { - count: collection._count?.links, - }) + count: collection._count?.links, + }) : t("search_count_links", { - count: collection._count?.links, - }) + count: collection._count?.links, + }) } />