Skip to content

Commit

Permalink
set fetchPolicy to network only in chechAuth and used localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
kanhaiya04 committed Feb 29, 2024
1 parent 9773f47 commit c9e1aca
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 41 deletions.
10 changes: 1 addition & 9 deletions src/GraphQl/Mutations/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,13 @@ export const ADDRESS_DETAILS_FRAGMENT = gql`

export const UPDATE_USER_MUTATION = gql`
mutation UpdateUserProfile(
$id: ID
$firstName: String
$lastName: String
$email: EmailAddress
$applangcode: String
$file: String
) {
updateUserProfile(
data: {
firstName: $firstName
lastName: $lastName
email: $email
id: $id
applangcode: $applangcode
}
data: { firstName: $firstName, lastName: $lastName, email: $email }
file: $file
) {
_id
Expand Down
18 changes: 0 additions & 18 deletions src/components/UserPortal/UserNavbar/UserNavbar.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { StaticMockLink } from 'utils/StaticMockLink';
import UserNavbar from './UserNavbar';
import userEvent from '@testing-library/user-event';
import { REVOKE_REFRESH_TOKEN } from 'GraphQl/Mutations/mutations';
import { CHECK_AUTH } from 'GraphQl/Queries/Queries';

async function wait(ms = 100): Promise<void> {
await act(() => {
Expand All @@ -29,23 +28,6 @@ const MOCKS = [
},
result: {},
},
{
request: {
query: CHECK_AUTH,
},
result: {
data: {
checkAuth: {
_id: '123',
firstName: 'John',
lastName: 'Doe',
image: 'https://example.com/profile.jpg',
email: '[email protected]',
userType: 'user',
},
},
},
},
];

const link = new StaticMockLink(MOCKS, true);
Expand Down
17 changes: 5 additions & 12 deletions src/components/UserPortal/UserNavbar/UserNavbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import cookies from 'js-cookie';
import PermIdentityIcon from '@mui/icons-material/PermIdentity';
import LanguageIcon from '@mui/icons-material/Language';
import { useTranslation } from 'react-i18next';
import { useMutation, useQuery } from '@apollo/client';
import { useMutation } from '@apollo/client';
import { REVOKE_REFRESH_TOKEN } from 'GraphQl/Mutations/mutations';
import { useHistory } from 'react-router-dom';
import { CHECK_AUTH } from 'GraphQl/Queries/Queries';
import useLocalStorage from 'utils/useLocalstorage';

function userNavbar(): JSX.Element {
const { getItem } = useLocalStorage();
const history = useHistory();

const { t } = useTranslation('translation', {
Expand All @@ -27,21 +28,13 @@ function userNavbar(): JSX.Element {
cookies.get('i18next') || 'en',
);

const [userName, setUserName] = React.useState('');

const { data } = useQuery(CHECK_AUTH);

React.useEffect(() => {
if (data) {
setUserName(`${data.checkAuth.firstName} ${data.checkAuth.lastName}`);
}
}, [data]);
const userName = getItem('name');

/* istanbul ignore next */
const handleLogout = (): void => {
revokeRefreshToken();
localStorage.clear();
window.location.replace('/user');
history.push('/user');
};

return (
Expand Down
25 changes: 24 additions & 1 deletion src/screens/UserPortal/Organizations/Organizations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import UserSidebar from 'components/UserPortal/UserSidebar/UserSidebar';
import { Dropdown, Form, InputGroup } from 'react-bootstrap';
import PaginationList from 'components/PaginationList/PaginationList';
import {
CHECK_AUTH,
USER_CREATED_ORGANIZATIONS,
USER_JOINED_ORGANIZATIONS,
USER_ORGANIZATION_CONNECTION,
Expand All @@ -16,7 +17,7 @@ import { useTranslation } from 'react-i18next';
import HourglassBottomIcon from '@mui/icons-material/HourglassBottom';
import useLocalStorage from 'utils/useLocalstorage';

const { getItem } = useLocalStorage();
const { getItem, setItem } = useLocalStorage();

interface InterfaceOrganizationCardProps {
id: string;
Expand Down Expand Up @@ -59,6 +60,10 @@ export default function organizations(): JSX.Element {
variables: { id: userId },
});

const { data: userData, loading } = useQuery(CHECK_AUTH, {
fetchPolicy: 'network-only',
});

/* istanbul ignore next */
const handleChangePage = (
_event: React.MouseEvent<HTMLButtonElement> | null,
Expand Down Expand Up @@ -121,6 +126,24 @@ export default function organizations(): JSX.Element {
}
}, [mode]);

/* istanbul ignore next */
React.useEffect(() => {
if (userData) {
setItem(
'name',
`${userData.checkAuth.firstName} ${userData.checkAuth.lastName}`,
);
setItem('id', userData.checkAuth._id);
setItem('email', userData.checkAuth.email);
setItem('IsLoggedIn', 'TRUE');
setItem('UserType', userData.checkAuth.userType);
setItem('FirstName', userData.checkAuth.firstName);
setItem('LastName', userData.checkAuth.lastName);
setItem('UserImage', userData.checkAuth.image);
setItem('Email', userData.checkAuth.email);
}
}, [userData, loading]);

return (
<>
<UserNavbar />
Expand Down
2 changes: 1 addition & 1 deletion src/screens/UserPortal/Settings/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function settings(): JSX.Element {

const { setItem } = useLocalStorage();

const { data } = useQuery(CHECK_AUTH);
const { data } = useQuery(CHECK_AUTH, { fetchPolicy: 'network-only' });
const [image, setImage] = React.useState('');
const [updateUserDetails] = useMutation(UPDATE_USER_MUTATION);
const [firstName, setFirstName] = React.useState('');
Expand Down

0 comments on commit c9e1aca

Please sign in to comment.