Skip to content

Commit

Permalink
Fix unsubscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
barshathakuri committed Dec 18, 2024
1 parent 7d688d1 commit 7bc6e08
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 92 deletions.
3 changes: 2 additions & 1 deletion src/views/Login/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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' });
}
Expand Down
4 changes: 2 additions & 2 deletions src/views/MySubscriptions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
}
Expand Down Expand Up @@ -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' });
}
Expand Down
4 changes: 2 additions & 2 deletions src/views/NewSubscriptionModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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' });
}
Expand Down Expand Up @@ -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' });
}
Expand Down
7 changes: 5 additions & 2 deletions src/views/RecoverAccountConfirm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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' });
}
Expand Down
108 changes: 23 additions & 85 deletions src/views/Unsubscribe/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
useCallback,
useMemo,
useState,
} from 'react';
import {
Expand All @@ -10,117 +9,55 @@ 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
}
}
}
`;

// 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;
}
Expand All @@ -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: () => {
Expand All @@ -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) {
Expand Down

0 comments on commit 7bc6e08

Please sign in to comment.