Skip to content

Commit

Permalink
fix: infinite rerender fix, refetch fix
Browse files Browse the repository at this point in the history
  • Loading branch information
naumovski-filip committed Jul 17, 2023
1 parent 1c7a537 commit c8acc0f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
7 changes: 4 additions & 3 deletions src/components/Root/Root.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { FC, useEffect, useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import { useQuery } from 'react-query';
import { Navigate, useSearchParams } from 'react-router-dom';
import { Navigate, useLocation, useSearchParams } from 'react-router-dom';

import ErrorPage from '#components/ErrorPage/ErrorPage';
import AccountModal from '#src/containers/AccountModal/AccountModal';
Expand All @@ -25,6 +25,7 @@ const Root: FC = () => {
});

const [searchParams, setSearchParams] = useSearchParams();
const location = useLocation();

const configSource = useMemo(() => getConfigSource(searchParams, settingsQuery.data), [searchParams, settingsQuery.data]);

Expand All @@ -46,7 +47,7 @@ const Root: FC = () => {
registerCustomScreens();
}, []);

const userData = useAccountStore((s) => ({ loading: s.loading, user: s.user, profile: s.profile }));
const userData = useAccountStore((s) => ({ loading: s.loading, user: s.user, profile: s.profile, canManageProfiles: s.canManageProfiles }));

if (userData.user && !userData.loading && window.location.href.includes('#token')) {
return <Navigate to="/" />; // component instead of hook to prevent extra re-renders
Expand All @@ -59,7 +60,7 @@ const Root: FC = () => {
return <LoadingOverlay />;
}

if (!userData.profile) {
if (userData.canManageProfiles && !userData.profile && !location.pathname.includes('/u/profiles')) {
return <Navigate to="/u/profiles" />;
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/UserMenu/UserMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const UserMenu = ({ showPaymentsItem, small = false, onClick }: Props) => {
const { data, isFetching } = useListProfiles();
const profiles = data?.responseData.collection;

if (!profiles?.length) {
if (canManageProfiles && !profiles?.length) {
unpersistProfile();
navigate('/u/profiles');
}
Expand Down
6 changes: 5 additions & 1 deletion src/containers/Profiles/Profiles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ const Profiles = ({ editMode = false }: Props) => {
if (!canManageProfiles) navigate('/');
}, [canManageProfiles, navigate]);

const { data, isLoading, isFetching } = useListProfiles();
const { data, isLoading, isFetching, refetch } = useListProfiles();
const activeProfiles = data?.responseData.collection.length || 0;
const canAddNew = activeProfiles < MAX_PROFILES;

const selectProfile = useSelectProfile();

useEffect(() => {
refetch();
}, [refetch]);

if (loading || isLoading || isFetching) return <LoadingOverlay inline />;

return (
Expand Down

0 comments on commit c8acc0f

Please sign in to comment.