From 30a019a064ba15a6033bf3687b3ef7debd11f069 Mon Sep 17 00:00:00 2001 From: "Kalle (Sendou)" <38327916+Sendouc@users.noreply.github.com> Date: Mon, 14 Jun 2021 21:37:35 +0300 Subject: [PATCH] User page fix bug with not being able to change page from one user to another --- pages/u/[identifier].tsx | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/pages/u/[identifier].tsx b/pages/u/[identifier].tsx index 2cc69cdf3..c0abfb6c6 100644 --- a/pages/u/[identifier].tsx +++ b/pages/u/[identifier].tsx @@ -36,14 +36,20 @@ const ProfilePage = (props: Props) => { const router = useRouter(); const [showProfileModal, setShowProfileModal] = useState(false); const [buildToEdit, setBuildToEdit] = useState(false); + const [userId, setUserId] = useState(undefined); + + const apiUrl = () => { + if (userId || (!!props.user?.id && props.user.id === loggedInUser?.id)) { + return `/api/users/${userId}`; + } + + return null; + }; const [loggedInUser] = useUser(); - const { data } = useSWR( - !!props.user?.id && props.user.id === loggedInUser?.id - ? `/api/users/${props.user.id}` - : null, - { initialData: props.user } - ); + const { data } = useSWR(apiUrl(), { + initialData: props.user, + }); const user = data ? data : props.user!; @@ -75,6 +81,10 @@ const ProfilePage = (props: Props) => { history.replaceState({}, "", `/u/${user.profile.customUrlPath}`); }, []); + useEffect(() => { + setUserId(props.user.id); + }, [props.user.id]); + return ( <>