Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/unsubscribe #213

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion src/views/Unsubscribe/i18n.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"namespace": "unsubscribe",
"strings": {
"unsubscriptionFailed": "Failed to unsubscribe. Please try again later.",
"unsubscriptionFailed": "Failed to unsubscribe!",
"unsubscribeSuccessfully": "Subscription successfully deactivated! Redirecting to the homepage...",
"unsubscribeConfirmHeading": "Unsubscribe",
"unsubscribeConfirmMessage": "Are you sure you want to unsubscribe?",
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
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.