refactor: rewrite useCurrentUser with RQ
This commit is contained in:
@@ -2,6 +2,7 @@ import PropTypes from 'prop-types';
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { useNavigate } from 'react-router-dom';
|
import { useNavigate } from 'react-router-dom';
|
||||||
import { useMutation } from '@apollo/client';
|
import { useMutation } from '@apollo/client';
|
||||||
|
|
||||||
import * as URLS from 'config/urls';
|
import * as URLS from 'config/urls';
|
||||||
import ConfirmationDialog from 'components/ConfirmationDialog';
|
import ConfirmationDialog from 'components/ConfirmationDialog';
|
||||||
import apolloClient from 'graphql/client';
|
import apolloClient from 'graphql/client';
|
||||||
@@ -13,15 +14,18 @@ import useCurrentUser from 'hooks/useCurrentUser';
|
|||||||
function DeleteAccountDialog(props) {
|
function DeleteAccountDialog(props) {
|
||||||
const [deleteCurrentUser] = useMutation(DELETE_CURRENT_USER);
|
const [deleteCurrentUser] = useMutation(DELETE_CURRENT_USER);
|
||||||
const formatMessage = useFormatMessage();
|
const formatMessage = useFormatMessage();
|
||||||
const currentUser = useCurrentUser();
|
const { data: currentUser } = useCurrentUser();
|
||||||
|
|
||||||
const authentication = useAuthentication();
|
const authentication = useAuthentication();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const handleConfirm = React.useCallback(async () => {
|
const handleConfirm = React.useCallback(async () => {
|
||||||
await deleteCurrentUser();
|
await deleteCurrentUser();
|
||||||
authentication.updateToken('');
|
authentication.updateToken('');
|
||||||
await apolloClient.clearStore();
|
await apolloClient.clearStore();
|
||||||
navigate(URLS.LOGIN);
|
navigate(URLS.LOGIN);
|
||||||
}, [deleteCurrentUser, currentUser]);
|
}, [deleteCurrentUser, currentUser?.data]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ConfirmationDialog
|
<ConfirmationDialog
|
||||||
title={formatMessage('deleteAccountDialog.title')}
|
title={formatMessage('deleteAccountDialog.title')}
|
||||||
|
@@ -1,34 +1,40 @@
|
|||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { useTheme } from '@mui/material/styles';
|
import { useTheme } from '@mui/material/styles';
|
||||||
import useMediaQuery from '@mui/material/useMediaQuery';
|
import useMediaQuery from '@mui/material/useMediaQuery';
|
||||||
|
|
||||||
import appConfig from 'config/app';
|
import appConfig from 'config/app';
|
||||||
import useCurrentUser from 'hooks/useCurrentUser';
|
import useCurrentUser from 'hooks/useCurrentUser';
|
||||||
|
|
||||||
const Chatwoot = ({ ready }) => {
|
const Chatwoot = ({ ready }) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const currentUser = useCurrentUser();
|
const { data: currentUser } = useCurrentUser();
|
||||||
const matchSmallScreens = useMediaQuery(theme.breakpoints.down('md'));
|
const matchSmallScreens = useMediaQuery(theme.breakpoints.down('md'));
|
||||||
|
|
||||||
React.useEffect(function initiateChatwoot() {
|
React.useEffect(function initiateChatwoot() {
|
||||||
window.chatwootSDK.run({
|
window.chatwootSDK.run({
|
||||||
websiteToken: 'EFyq5MTsvS7XwUrwSH36VskT',
|
websiteToken: 'EFyq5MTsvS7XwUrwSH36VskT',
|
||||||
baseUrl: appConfig.chatwootBaseUrl,
|
baseUrl: appConfig.chatwootBaseUrl,
|
||||||
});
|
});
|
||||||
|
|
||||||
return function removeChatwoot() {
|
return function removeChatwoot() {
|
||||||
window.$chatwoot.reset();
|
window.$chatwoot.reset();
|
||||||
window.$chatwoot.toggleBubbleVisibility('hide');
|
window.$chatwoot.toggleBubbleVisibility('hide');
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
React.useEffect(
|
React.useEffect(
|
||||||
function initiateUser() {
|
function initiateUser() {
|
||||||
if (!currentUser?.id || !ready) return;
|
if (!currentUser?.data?.id || !ready) return;
|
||||||
window.$chatwoot.setUser(currentUser.id, {
|
window.$chatwoot.setUser(currentUser.data?.id, {
|
||||||
email: currentUser.email,
|
email: currentUser?.data?.email,
|
||||||
name: currentUser.fullName,
|
name: currentUser?.data?.fullName,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!matchSmallScreens) {
|
if (!matchSmallScreens) {
|
||||||
window.$chatwoot.toggleBubbleVisibility('show');
|
window.$chatwoot.toggleBubbleVisibility('show');
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[currentUser, ready, matchSmallScreens],
|
[currentUser?.data, ready, matchSmallScreens],
|
||||||
);
|
);
|
||||||
React.useLayoutEffect(
|
React.useLayoutEffect(
|
||||||
function hideChatwoot() {
|
function hideChatwoot() {
|
||||||
@@ -40,6 +46,7 @@ const Chatwoot = ({ ready }) => {
|
|||||||
},
|
},
|
||||||
[matchSmallScreens],
|
[matchSmallScreens],
|
||||||
);
|
);
|
||||||
|
|
||||||
return <React.Fragment />;
|
return <React.Fragment />;
|
||||||
};
|
};
|
||||||
export default Chatwoot;
|
export default Chatwoot;
|
||||||
|
@@ -21,7 +21,7 @@ import usePaddle from 'hooks/usePaddle.ee';
|
|||||||
|
|
||||||
export default function UpgradeFreeTrial() {
|
export default function UpgradeFreeTrial() {
|
||||||
const { data: plans, isLoading: isPaymentPlansLoading } = usePaymentPlans();
|
const { data: plans, isLoading: isPaymentPlansLoading } = usePaymentPlans();
|
||||||
const currentUser = useCurrentUser();
|
const { data: currentUser } = useCurrentUser();
|
||||||
const { loaded: paddleLoaded } = usePaddle();
|
const { loaded: paddleLoaded } = usePaddle();
|
||||||
const [selectedIndex, setSelectedIndex] = React.useState(0);
|
const [selectedIndex, setSelectedIndex] = React.useState(0);
|
||||||
const selectedPlan = plans?.data?.[selectedIndex];
|
const selectedPlan = plans?.data?.[selectedIndex];
|
||||||
@@ -30,13 +30,13 @@ export default function UpgradeFreeTrial() {
|
|||||||
const handleCheckout = React.useCallback(() => {
|
const handleCheckout = React.useCallback(() => {
|
||||||
window.Paddle.Checkout?.open({
|
window.Paddle.Checkout?.open({
|
||||||
product: selectedPlan.productId,
|
product: selectedPlan.productId,
|
||||||
email: currentUser.email,
|
email: currentUser?.data?.email,
|
||||||
passthrough: JSON.stringify({
|
passthrough: JSON.stringify({
|
||||||
id: currentUser.id,
|
id: currentUser?.data?.id,
|
||||||
email: currentUser.email,
|
email: currentUser?.data?.email,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
}, [selectedPlan, currentUser]);
|
}, [selectedPlan, currentUser?.data]);
|
||||||
|
|
||||||
if (isPaymentPlansLoading || !plans?.data?.length) return null;
|
if (isPaymentPlansLoading || !plans?.data?.length) return null;
|
||||||
|
|
||||||
|
@@ -1,6 +1,18 @@
|
|||||||
import { useQuery } from '@apollo/client';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { GET_CURRENT_USER } from 'graphql/queries/get-current-user';
|
|
||||||
|
import api from 'helpers/api';
|
||||||
|
|
||||||
export default function useCurrentUser() {
|
export default function useCurrentUser() {
|
||||||
const { data } = useQuery(GET_CURRENT_USER);
|
const query = useQuery({
|
||||||
return data?.getCurrentUser;
|
queryKey: ['currentUser'],
|
||||||
|
queryFn: async ({ signal }) => {
|
||||||
|
const { data } = await api.get(`/v1/users/me`, {
|
||||||
|
signal,
|
||||||
|
});
|
||||||
|
|
||||||
|
return data;
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return query;
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,8 @@
|
|||||||
import userAbility from 'helpers/userAbility';
|
import userAbility from 'helpers/userAbility';
|
||||||
import useCurrentUser from 'hooks/useCurrentUser';
|
import useCurrentUser from 'hooks/useCurrentUser';
|
||||||
|
|
||||||
export default function useCurrentUserAbility() {
|
export default function useCurrentUserAbility() {
|
||||||
const currentUser = useCurrentUser();
|
const { data: currentUser } = useCurrentUser();
|
||||||
return userAbility(currentUser);
|
|
||||||
|
return userAbility(currentUser?.data);
|
||||||
}
|
}
|
||||||
|
@@ -9,6 +9,7 @@ import { styled } from '@mui/material/styles';
|
|||||||
import useEnqueueSnackbar from 'hooks/useEnqueueSnackbar';
|
import useEnqueueSnackbar from 'hooks/useEnqueueSnackbar';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import * as yup from 'yup';
|
import * as yup from 'yup';
|
||||||
|
|
||||||
import Container from 'components/Container';
|
import Container from 'components/Container';
|
||||||
import DeleteAccountDialog from 'components/DeleteAccountDialog/index.ee';
|
import DeleteAccountDialog from 'components/DeleteAccountDialog/index.ee';
|
||||||
import Form from 'components/Form';
|
import Form from 'components/Form';
|
||||||
@@ -17,6 +18,7 @@ import TextField from 'components/TextField';
|
|||||||
import { UPDATE_CURRENT_USER } from 'graphql/mutations/update-current-user';
|
import { UPDATE_CURRENT_USER } from 'graphql/mutations/update-current-user';
|
||||||
import useCurrentUser from 'hooks/useCurrentUser';
|
import useCurrentUser from 'hooks/useCurrentUser';
|
||||||
import useFormatMessage from 'hooks/useFormatMessage';
|
import useFormatMessage from 'hooks/useFormatMessage';
|
||||||
|
|
||||||
const validationSchema = yup
|
const validationSchema = yup
|
||||||
.object({
|
.object({
|
||||||
fullName: yup.string().required(),
|
fullName: yup.string().required(),
|
||||||
@@ -27,27 +29,32 @@ const validationSchema = yup
|
|||||||
.oneOf([yup.ref('password')], 'Passwords must match'),
|
.oneOf([yup.ref('password')], 'Passwords must match'),
|
||||||
})
|
})
|
||||||
.required();
|
.required();
|
||||||
|
|
||||||
const StyledForm = styled(Form)`
|
const StyledForm = styled(Form)`
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: end;
|
align-items: end;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
`;
|
`;
|
||||||
|
|
||||||
function ProfileSettings() {
|
function ProfileSettings() {
|
||||||
const [showDeleteAccountConfirmation, setShowDeleteAccountConfirmation] =
|
const [showDeleteAccountConfirmation, setShowDeleteAccountConfirmation] =
|
||||||
React.useState(false);
|
React.useState(false);
|
||||||
const enqueueSnackbar = useEnqueueSnackbar();
|
const enqueueSnackbar = useEnqueueSnackbar();
|
||||||
const currentUser = useCurrentUser();
|
const { data: currentUser } = useCurrentUser();
|
||||||
const formatMessage = useFormatMessage();
|
const formatMessage = useFormatMessage();
|
||||||
const [updateCurrentUser] = useMutation(UPDATE_CURRENT_USER);
|
const [updateCurrentUser] = useMutation(UPDATE_CURRENT_USER);
|
||||||
|
|
||||||
const handleProfileSettingsUpdate = async (data) => {
|
const handleProfileSettingsUpdate = async (data) => {
|
||||||
const { fullName, password, email } = data;
|
const { fullName, password, email } = data;
|
||||||
const mutationInput = {
|
const mutationInput = {
|
||||||
fullName,
|
fullName,
|
||||||
email,
|
email,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (password) {
|
if (password) {
|
||||||
mutationInput.password = password;
|
mutationInput.password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
await updateCurrentUser({
|
await updateCurrentUser({
|
||||||
variables: {
|
variables: {
|
||||||
input: mutationInput,
|
input: mutationInput,
|
||||||
@@ -55,12 +62,13 @@ function ProfileSettings() {
|
|||||||
optimisticResponse: {
|
optimisticResponse: {
|
||||||
updateCurrentUser: {
|
updateCurrentUser: {
|
||||||
__typename: 'User',
|
__typename: 'User',
|
||||||
id: currentUser.id,
|
id: currentUser?.data?.id,
|
||||||
fullName,
|
fullName,
|
||||||
email,
|
email,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
enqueueSnackbar(formatMessage('profileSettings.updatedProfile'), {
|
enqueueSnackbar(formatMessage('profileSettings.updatedProfile'), {
|
||||||
variant: 'success',
|
variant: 'success',
|
||||||
SnackbarProps: {
|
SnackbarProps: {
|
||||||
@@ -68,6 +76,7 @@ function ProfileSettings() {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Container sx={{ py: 3, display: 'flex', justifyContent: 'center' }}>
|
<Container sx={{ py: 3, display: 'flex', justifyContent: 'center' }}>
|
||||||
<Grid container item xs={12} sm={9} md={8} lg={6}>
|
<Grid container item xs={12} sm={9} md={8} lg={6}>
|
||||||
@@ -78,7 +87,7 @@ function ProfileSettings() {
|
|||||||
<Grid item xs={12} justifyContent="flex-end">
|
<Grid item xs={12} justifyContent="flex-end">
|
||||||
<StyledForm
|
<StyledForm
|
||||||
defaultValues={{
|
defaultValues={{
|
||||||
...currentUser,
|
...currentUser?.data,
|
||||||
password: '',
|
password: '',
|
||||||
confirmPassword: '',
|
confirmPassword: '',
|
||||||
}}
|
}}
|
||||||
|
Reference in New Issue
Block a user