From bb42eca932fac378877938832ce4d22113dcb9f6 Mon Sep 17 00:00:00 2001 From: "Kalle (Sendou)" <38327916+Sendouc@users.noreply.github.com> Date: Thu, 19 Nov 2020 11:03:27 +0200 Subject: [PATCH] useSWR fetching logic fixes --- components/layout/index.tsx | 13 ++++++++----- components/u/ProfileModal.tsx | 4 ++-- hooks/u.ts | 2 +- lib/postData.ts | 18 ++++++++++++++++-- pages/api/me/profile.ts | 29 ++++++++++++++++------------- pages/u/[identifier].tsx | 10 +++------- 6 files changed, 46 insertions(+), 30 deletions(-) diff --git a/components/layout/index.tsx b/components/layout/index.tsx index e6398a26d..6f02bd5bd 100644 --- a/components/layout/index.tsx +++ b/components/layout/index.tsx @@ -1,6 +1,5 @@ import { Box, Container, Flex, useToast } from "@chakra-ui/react"; import { t } from "@lingui/macro"; -import { getToastOptions } from "lib/getToastOptions"; import { AppProps } from "next/app"; import { SWRConfig } from "swr"; import Footer from "./Footer"; @@ -36,10 +35,14 @@ const Layout = ({ Component, pageProps }: AppProps) => { }), revalidateOnFocus: false, revalidateOnReconnect: false, - onError: (error) => { - toast( - getToastOptions(error.message ?? t`An error occurred`, "error") - ); + onError: () => { + toast({ + duration: null, + isClosable: true, + position: "top-right", + status: "error", + description: t`An error occurred`, + }); }, }} > diff --git a/components/u/ProfileModal.tsx b/components/u/ProfileModal.tsx index 661fa526c..bad2b9575 100644 --- a/components/u/ProfileModal.tsx +++ b/components/u/ProfileModal.tsx @@ -86,7 +86,6 @@ const ProfileModal: React.FC = ({ onClose, user }) => { : undefined, }); - // FIXME: bio length show const watchBio = watch("bio", user.profile?.bio ?? ""); const toast = useToast(); @@ -114,7 +113,8 @@ const ProfileModal: React.FC = ({ onClose, user }) => { } // FIXME: error handling - await sendData("PUT", "/api/me/profile", mutationData); + const success = await sendData("PUT", "/api/me/profile", mutationData); + if (!success) return; mutate(`/api/users/${user.id}`); diff --git a/hooks/u.ts b/hooks/u.ts index 79df8632b..79b4590fb 100644 --- a/hooks/u.ts +++ b/hooks/u.ts @@ -6,7 +6,7 @@ export function useBuildsByUser(userId?: number) { const [weapon, setWeapon] = useState(null); const { data = [] } = useSWR( - `/api/users/${userId}/builds` + userId ? `/api/users/${userId}/builds` : null ); const weaponCounts = data.reduce((acc: [string, number][], build) => { diff --git a/lib/postData.ts b/lib/postData.ts index 486900d15..596267c6d 100644 --- a/lib/postData.ts +++ b/lib/postData.ts @@ -1,3 +1,6 @@ +import { createStandaloneToast } from "@chakra-ui/react"; +import { t } from "@lingui/macro"; + export async function sendData(method = "POST", url = "", data = {}) { // Default options are marked with * const response = await fetch(url, { @@ -9,7 +12,18 @@ export async function sendData(method = "POST", url = "", data = {}) { }); if (response.status < 200 || response.status > 299) { - // FIXME: different messages for different status codes and translated - throw Error("Invalid request"); + const toast = createStandaloneToast(); + + toast({ + duration: null, + isClosable: true, + position: "top-right", + status: "error", + description: t`An error occurred`, + }); + + return false; } + + return true; } diff --git a/pages/api/me/profile.ts b/pages/api/me/profile.ts index 1573bafb2..ab2ad08a4 100644 --- a/pages/api/me/profile.ts +++ b/pages/api/me/profile.ts @@ -34,19 +34,8 @@ const profileHandler = async (req: NextApiRequest, res: NextApiResponse) => { return res.status(400).end(); } - if (argsForDb.customUrlPath) { - const profileWithSameCustomUrl = await prisma.profile.findOne({ - where: { - customUrlPath: argsForDb.customUrlPath, - }, - }); - - if ( - profileWithSameCustomUrl && - profileWithSameCustomUrl.userId !== user.id - ) { - return res.status(400).json({ message: "custom url already in use" }); - } + if (isDuplicateCustomUrl(argsForDb.customUrlPath, user.id)) { + return res.status(400).json({ message: "custom url already in use" }); } await prisma.profile.upsert({ @@ -66,4 +55,18 @@ const profileHandler = async (req: NextApiRequest, res: NextApiResponse) => { } }; +async function isDuplicateCustomUrl(customUrlPath: string, userId: number) { + if (!customUrlPath) return false; + + const profileWithSameCustomUrl = await prisma.profile.findOne({ + where: { + customUrlPath, + }, + }); + + if (profileWithSameCustomUrl && profileWithSameCustomUrl.userId !== userId) { + return false; + } +} + export default profileHandler; diff --git a/pages/u/[identifier].tsx b/pages/u/[identifier].tsx index 7079121a3..69bbe9f08 100644 --- a/pages/u/[identifier].tsx +++ b/pages/u/[identifier].tsx @@ -81,13 +81,9 @@ const ProfilePage = (props: Props) => { const [loggedInUser] = useUser(); const { data: user } = useSWR( - () => { - // no need to load user if it's not the same as currently logged in user - const userId = props.user?.id; - if (!!userId && userId === loggedInUser?.id) return null; - - return `/api/users/${userId}`; - }, + !!props.user?.id && props.user.id === loggedInUser?.id + ? `/api/users/${props.user.id}` + : null, { initialData: props.user } ); const { data: builds, weaponCounts, setWeapon, buildCount } = useBuildsByUser(