Skip to content

Commit

Permalink
🐛 Fix login as refetching
Browse files Browse the repository at this point in the history
  • Loading branch information
zivavu committed May 6, 2024
1 parent 01eda2c commit 8367ac7
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/common/misc/userDataManagment/useChangeLoggedUser.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
import { closeAllChats } from '@/redux/features/openedChatsSlice';
import { useGetLoggedUserQuery } from '@/redux/services/loggedUserAPI';
import { useGetLoggedUserQuery, useGetUserChatsQuery } from '@/redux/services/loggedUserAPI';
import { useState } from 'react';
import { useDispatch } from 'react-redux';

export default function useChangeLoggedUser(userId?: string) {
const dispatch = useDispatch();

const { refetch: refetchLoggedUser } = useGetLoggedUserQuery({});
const { refetch: refetchChats } = useGetUserChatsQuery({});

const [isLoading, setIsLoading] = useState(false);

async function switchLoggedUser() {
setIsLoading(true);

dispatch(closeAllChats());

await refetchLoggedUser();

if (localStorage) {
if (userId) localStorage.setItem('loggedUser', JSON.stringify(userId));
// Redux logged user query handles random user fetching if no userId is stored in localStorage
else localStorage.removeItem('loggedUser');
}

await refetchChats();
await refetchLoggedUser();

setIsLoading(false);
}

Expand Down
1 change: 1 addition & 0 deletions src/components/pages/Home/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ContactsSidebar from '@/components/molecules/ContactsSidebar';
import HomeWall from '@/components/organisms/HomeWall';
import ShortcutsSidebar from '@/components/organisms/ShortcutsSidebar';
import { useGetLoggedUserQuery } from '@/redux/services/loggedUserAPI';
import { BoxProps } from '@mui/material';
import { StyledRoot } from './styles';

Expand Down
1 change: 1 addition & 0 deletions src/components/pages/Profile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { ProfileProps, TProfileTabs } from './types';
export default function Profile({ userId, useTabsRouting = true, sx, ...rootProps }: ProfileProps) {
const theme = useTheme();
const router = useRouter();

const { data: profileData, refetch } = useUserDataByIdQuery(userId);

const [selectedTab, setSelectedTab] = useState<TProfileTabs>(useTabsRouting ? null : 'posts');
Expand Down
1 change: 1 addition & 0 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import { LocalizationProvider } from '@mui/x-date-pickers';
import { ThemeProvider as NextThemesProvider } from 'next-themes';
import { Provider as StoreProvider } from 'react-redux';

import { useGetLoggedUserQuery } from '@/redux/services/loggedUserAPI';
import '@fortawesome/fontawesome-svg-core/styles.css';
const { library, config } = require('@fortawesome/fontawesome-svg-core');

Expand Down
1 change: 1 addition & 0 deletions src/pages/_document.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import createEmotionCache from '@/design/createEmotionCache';
import createEmotionServer from '@emotion/server/create-instance';
import { AppType } from 'next/app';
import { EmotionAppProps } from './_app';
import { useGetLoggedUserQuery } from '@/redux/services/loggedUserAPI';

interface EmotionDocumentProps extends DocumentProps {
emotionStyleTags: JSX.Element[];
Expand Down

0 comments on commit 8367ac7

Please sign in to comment.