refactor: rewrite useSubscription with RQ

This commit is contained in:
Rıdvan Akca
2024-03-13 14:42:29 +03:00
parent 89ad685f3a
commit 70b8817643
7 changed files with 46 additions and 57 deletions

View File

@@ -0,0 +1,38 @@
import { useQuery } from '@tanstack/react-query';
import { DateTime } from 'luxon';
import useFormatMessage from './useFormatMessage';
import api from 'helpers/api';
export default function useSubscription() {
const formatMessage = useFormatMessage();
const { data, isLoading: isSubscriptionLoading } = useQuery({
queryKey: ['subscription'],
queryFn: async ({ signal }) => {
const { data } = await api.get(`/v1/users/me/subscription`, {
signal,
});
return data;
},
});
const subscription = data?.data;
const cancellationEffectiveDate = subscription?.cancellationEffectiveDate;
const hasCancelled = !!cancellationEffectiveDate;
if (isSubscriptionLoading || !hasCancelled) return null;
const cancellationEffectiveDateObject = DateTime.fromISO(
cancellationEffectiveDate,
);
return {
message: formatMessage('subscriptionCancelledAlert.text', {
date: cancellationEffectiveDateObject.toFormat('DDD'),
}),
cancellationEffectiveDate: cancellationEffectiveDateObject,
};
}