From 7bc6e085a8612614627612f7f5486becdd2e3353 Mon Sep 17 00:00:00 2001 From: barshathakuri Date: Wed, 18 Dec 2024 12:21:42 +0545 Subject: [PATCH] Fix unsubscribe --- src/views/Login/index.tsx | 3 +- src/views/MySubscriptions/index.tsx | 4 +- src/views/NewSubscriptionModal/index.tsx | 4 +- src/views/RecoverAccountConfirm/index.tsx | 7 +- src/views/Unsubscribe/index.tsx | 108 +++++----------------- 5 files changed, 34 insertions(+), 92 deletions(-) diff --git a/src/views/Login/index.tsx b/src/views/Login/index.tsx index 020bf5b..42a7137 100644 --- a/src/views/Login/index.tsx +++ b/src/views/Login/index.tsx @@ -14,6 +14,7 @@ import { } from '@ifrc-go/ui'; import { useTranslation } from '@ifrc-go/ui/hooks'; import { resolveToComponent } from '@ifrc-go/ui/utils'; +import { isDefined } from '@togglecorp/fujs'; import { createSubmitHandler, emailCondition, @@ -129,7 +130,7 @@ export function Component() { } else { const errorMessages = response?.errors ?.map((errors: { messages: string; }) => errors.messages) - .filter((message: string) => message) + .filter(isDefined) .join(', '); alert.show(errorMessages, { variant: 'danger' }); } diff --git a/src/views/MySubscriptions/index.tsx b/src/views/MySubscriptions/index.tsx index b5f0725..24b67e6 100644 --- a/src/views/MySubscriptions/index.tsx +++ b/src/views/MySubscriptions/index.tsx @@ -224,7 +224,7 @@ export function Component() { } else { const errorMessages = response?.errors ?.map((error: { messages: string; }) => error.messages) - .filter((message: string) => message) + .filter(isDefined) .join(', '); alert.show(errorMessages, { variant: 'danger' }); } @@ -263,7 +263,7 @@ export function Component() { } else { const errorMessages = response?.errors ?.map((error: { messages: string; }) => error.messages) - .filter((message: string) => message) + .filter(isDefined) .join(', '); alert.show(errorMessages, { variant: 'danger' }); } diff --git a/src/views/NewSubscriptionModal/index.tsx b/src/views/NewSubscriptionModal/index.tsx index b933e6d..d2ea001 100644 --- a/src/views/NewSubscriptionModal/index.tsx +++ b/src/views/NewSubscriptionModal/index.tsx @@ -327,7 +327,7 @@ function NewSubscriptionModal(props: Props) { } else { const errorMessages = response?.errors ?.map((error: { messages: string; }) => error.messages) - .filter((message: string) => message) + .filter(isDefined) .join(', '); alert.show(errorMessages, { variant: 'danger' }); } @@ -363,7 +363,7 @@ function NewSubscriptionModal(props: Props) { } else { const errorMessages = response?.errors ?.map((error: { messages: string; }) => error.messages) - .filter((message: string) => message) + .filter(isDefined) .join(', '); alert.show(errorMessages, { variant: 'danger' }); } diff --git a/src/views/RecoverAccountConfirm/index.tsx b/src/views/RecoverAccountConfirm/index.tsx index 3e348c6..ff3fad3 100644 --- a/src/views/RecoverAccountConfirm/index.tsx +++ b/src/views/RecoverAccountConfirm/index.tsx @@ -12,7 +12,10 @@ import { PasswordInput, } from '@ifrc-go/ui'; import { useTranslation } from '@ifrc-go/ui/hooks'; -import { isTruthyString } from '@togglecorp/fujs'; +import { + isDefined, + isTruthyString, +} from '@togglecorp/fujs'; import { addCondition, createSubmitHandler, @@ -139,7 +142,7 @@ export function Component() { )); const errorMessages = data.public.passwordResetConfirm?.errors ?.map((error: { messages: string; }) => error.messages) - .filter((message: string) => message) + .filter(isDefined) .join(', '); alert.show(errorMessages, { variant: 'danger' }); } diff --git a/src/views/Unsubscribe/index.tsx b/src/views/Unsubscribe/index.tsx index d65eea1..e8ccc47 100644 --- a/src/views/Unsubscribe/index.tsx +++ b/src/views/Unsubscribe/index.tsx @@ -1,6 +1,5 @@ import { useCallback, - useMemo, useState, } from 'react'; import { @@ -10,59 +9,32 @@ import { import { gql, useMutation, - useQuery, } from '@apollo/client'; import { ConfirmButton, Message, } from '@ifrc-go/ui'; import { useTranslation } from '@ifrc-go/ui/hooks'; -import { isNotDefined } from '@togglecorp/fujs'; +import { isDefined } from '@togglecorp/fujs'; import Page from '#components/Page'; import { - AlertSubscriptionQuery, - AlertSubscriptionQueryVariables, - UnsubscriptionMutation, - UnsubscriptionMutationVariables, + UnsubscribeAlertSubscriptionMutation, + UnsubscribeAlertSubscriptionMutationVariables, } from '#generated/types/graphql'; import useAlert from '#hooks/useAlert'; import i18n from './i18n.json'; import styles from './styles.module.css'; -const ALERT_SUBSCRIPTION = gql` - query AlertSubscription( - $pk: ID! +const UNSUBSCRIBE_ALERT = gql` + mutation UnsubscribeAlertSubscription( + $data: UserAlertSubscriptionUnsubscribeInput! ) { - private { - userAlertSubscription(pk: $pk) { - id - filterAlertCountryId - filterAlertAdmin1s - name - isActive - } - } - } -`; - -const UPDATE_SUBSCRIPTION = gql` - mutation Unsubscription( - $subscriptionId: ID!, - $data: UserAlertSubscriptionInput!, - ) { - private { - updateUserAlertSubscription( - id: $subscriptionId, - data: $data, - ) { - errors + public { + unsubscribeUserAlertSubscription(data: $data) { ok - result { - id - isActive - } + errors } } } @@ -70,57 +42,22 @@ const UPDATE_SUBSCRIPTION = gql` // eslint-disable-next-line import/prefer-default-export export function Component() { - const { subscriptionId } = useParams<{ subscriptionId?: string, token?: string }>(); + const { subscriptionId, token } = useParams<{ subscriptionId?: string, token?: string }>(); const alert = useAlert(); const navigate = useNavigate(); const strings = useTranslation(i18n); const [isErrored, setIsError] = useState(false); - const decodedSubscriptionId = typeof subscriptionId === 'string' - ? atob(subscriptionId) : undefined; - - const variables = useMemo(() => (decodedSubscriptionId - ? { pk: decodedSubscriptionId } : undefined - ), [decodedSubscriptionId]); - - const { - data: alertSubscription, - } = useQuery< - AlertSubscriptionQuery, - AlertSubscriptionQueryVariables - >( - ALERT_SUBSCRIPTION, - { - skip: isNotDefined(variables), - variables, - onCompleted: (data) => { - if (!data?.private?.userAlertSubscription) { - alert.show( - strings.subscribeNotAvailable, - { variant: 'info' }, - ); - navigate('/'); - } - }, - onError: () => { - alert.show( - strings.unsubscriptionFailed, - { variant: 'danger' }, - ); - }, - }, - ); - const [ subscriptionUpdate, ] = useMutation< - UnsubscriptionMutation, - UnsubscriptionMutationVariables + UnsubscribeAlertSubscriptionMutation, + UnsubscribeAlertSubscriptionMutationVariables >( - UPDATE_SUBSCRIPTION, + UNSUBSCRIBE_ALERT, { onCompleted: (response) => { - const subscriptionResponse = response?.private.updateUserAlertSubscription; + const subscriptionResponse = response?.public.unsubscribeUserAlertSubscription; if (!response) { return; } @@ -132,6 +69,11 @@ export function Component() { navigate('/'); } else { setIsError(true); + const errorMessages = subscriptionResponse?.errors + ?.map((error: { messages: string; }) => error.messages) + .filter(isDefined) + .join(', '); + alert.show(errorMessages, { variant: 'danger' }); } }, onError: () => { @@ -144,24 +86,20 @@ export function Component() { ); const handleUnsubscribe = useCallback(() => { - const data = alertSubscription?.private?.userAlertSubscription; - if (subscriptionId && data) { + if (subscriptionId && token) { subscriptionUpdate({ variables: { - subscriptionId, data: { - isActive: false, - filterAlertAdmin1s: data.filterAlertAdmin1s, - filterAlertCountry: data.filterAlertCountryId, - name: data.name, + uuid: subscriptionId, + token, }, }, }); } }, [ - alertSubscription?.private?.userAlertSubscription, subscriptionId, subscriptionUpdate, + token, ]); if (isErrored) {