Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

fix: calling request with team before team is loaded

Marc Seitz0cc42aded6b3
diff --git a/lib/swr/use-billing.ts b/lib/swr/use-billing.ts
index 89b41fc1..10c907b7 100644
--- a/lib/swr/use-billing.ts
+++ b/lib/swr/use-billing.ts
@@ -41,7 +41,8 @@ export function usePlan() {
const teamInfo = useTeam();
const { data: plan, error } = useSWR<PlanResponse>(
- `/api/teams/${teamInfo?.currentTeam?.id}/billing/plan`,
+ teamInfo?.currentTeam?.id &&
+ `/api/teams/${teamInfo?.currentTeam?.id}/billing/plan`,
fetcher,
{
dedupingInterval: 60000,
diff --git a/lib/swr/use-brand.ts b/lib/swr/use-brand.ts
index 7e305218..01f3f668 100644
--- a/lib/swr/use-brand.ts
+++ b/lib/swr/use-brand.ts
@@ -9,7 +9,8 @@ export function useBrand() {
const teamInfo = useTeam();
const { data: brand, error } = useSWR<Brand>(
- `/api/teams/${teamInfo?.currentTeam?.id}/branding`,
+ teamInfo?.currentTeam?.id &&
+ `/api/teams/${teamInfo?.currentTeam?.id}/branding`,
fetcher,
{
dedupingInterval: 30000,
diff --git a/lib/swr/use-document.ts b/lib/swr/use-document.ts
index de1cb9e0..d092ca82 100644
--- a/lib/swr/use-document.ts
+++ b/lib/swr/use-document.ts
@@ -14,7 +14,8 @@ export function useDocument() {
};
const { data: document, error } = useSWR<DocumentWithVersion>(
- id &&
+ teamInfo?.currentTeam?.id &&
+ id &&
`/api/teams/${teamInfo?.currentTeam?.id}/documents/${encodeURIComponent(
id,
)}`,
@@ -41,7 +42,8 @@ export function useDocumentLinks() {
};
const { data: links, error } = useSWR<LinkWithViews[]>(
- id &&
+ teamInfo?.currentTeam?.id &&
+ id &&
`/api/teams/${teamInfo?.currentTeam?.id}/documents/${encodeURIComponent(
id,
)}/links`,
@@ -78,7 +80,8 @@ export function useDocumentVisits() {
};
const { data: views, error } = useSWR<ViewWithDuration[]>(
- id &&
+ teamInfo?.currentTeam?.id &&
+ id &&
`/api/teams/${teamInfo?.currentTeam?.id}/documents/${encodeURIComponent(
id,
)}/views`,
diff --git a/lib/swr/use-documents.ts b/lib/swr/use-documents.ts
index dc9ee27b..ee376bc5 100644
--- a/lib/swr/use-documents.ts
+++ b/lib/swr/use-documents.ts
@@ -1,17 +1,16 @@
-import { useRouter } from "next/router";
import useSWR from "swr";
import { fetcher } from "@/lib/utils";
import { DocumentWithLinksAndLinkCountAndViewCount } from "@/lib/types";
import { useTeam } from "@/context/team-context";
export default function useDocuments() {
- const router = useRouter();
const teamInfo = useTeam();
- const { data: documents, isValidating } = useSWR<
+ const { data: documents, error } = useSWR<
DocumentWithLinksAndLinkCountAndViewCount[]
>(
- router.isReady && `/api/teams/${teamInfo?.currentTeam?.id}/documents`,
+ teamInfo?.currentTeam?.id &&
+ `/api/teams/${teamInfo?.currentTeam?.id}/documents`,
fetcher,
{
revalidateOnFocus: false,
@@ -21,7 +20,7 @@ export default function useDocuments() {
return {
documents,
- loading: documents ? false : true,
- isValidating,
+ loading: !documents && !error,
+ error,
};
}
diff --git a/pages/api/teams/[teamId]/branding.ts b/pages/api/teams/[teamId]/branding.ts
index 4b30b865..6afd7651 100644
--- a/pages/api/teams/[teamId]/branding.ts
+++ b/pages/api/teams/[teamId]/branding.ts
@@ -49,10 +49,10 @@ export default async function handle(
});
if (!brand) {
- res.status(404).end("Brand not found");
+ res.status(200).json(null);
}
- return res.status(200).json(brand);
+ res.status(200).json(brand);
} else if (req.method === "POST") {
// POST /api/teams/:teamId/branding
const { logo, brandColor, accentColor } = req.body as {