Skip to content

Commit

Permalink
Merge pull request #210 from IFRCGo/feature/subscription-redirect
Browse files Browse the repository at this point in the history
Fix subscription redirect issue
  • Loading branch information
roshni73 authored and barshathakuri committed Dec 18, 2024
2 parents 99503a6 + 63680d3 commit 3c48b1c
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 96 deletions.
34 changes: 34 additions & 0 deletions src/App/redirects/SubscriptionDetailsRedirect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
generatePath,
useNavigate,
useParams,
} from 'react-router-dom';

import routes from '#routes';

interface SubscriptionDetailsParams {
subscriptionId: string | undefined;
[key: string]: string | undefined;
}

// eslint-disable-next-line import/prefer-default-export
export function Component() {
const { subscriptionId } = useParams<SubscriptionDetailsParams>();
const navigate = useNavigate();

const subscriptionDetailsLink = (subscriptionId) ? ({
pathname: (generatePath(
routes.subscriptionDetail.absolutePath,
{ subscriptionId },
)),
})
: routes.pageNotFound.path;

if (subscriptionDetailsLink) {
navigate(subscriptionDetailsLink);
}

return null;
}

Component.displayName = 'SubscriptionDetailsRedirect';
19 changes: 17 additions & 2 deletions src/App/routes/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ const subscriptionDetail = customWrapRoute({
},
});

const subscriptionDetailRedirect = customWrapRoute({
parent: rootLayout,
path: 'permalink/subscription-detail/:subscriptionId',
component: {
render: () => import('../redirects/SubscriptionDetailsRedirect'),
props: {},
},
wrapperComponent: Auth,
context: {
title: 'Subscription Details',
visibility: 'is-authenticated',
},
});

const homeIndex = customWrapRoute({
parent: homeLayout,
index: true,
Expand Down Expand Up @@ -192,7 +206,7 @@ const alertDetailRedirect = customWrapRoute({
parent: rootLayout,
path: 'permalink/alert-detail/:alertId',
component: {
render: () => import('../redirects/AlertDetailsRedirect.tsx'),
render: () => import('../redirects/AlertDetailsRedirect'),
props: {},
},
wrapperComponent: Auth,
Expand Down Expand Up @@ -316,7 +330,7 @@ const resetPasswordRedirect = customWrapRoute({
parent: rootLayout,
path: 'permalink/user-password-reset/:userId/:resetToken',
component: {
render: () => import('../redirects/RecoverAccountRedirect.tsx'),
render: () => import('../redirects/RecoverAccountRedirect'),
props: {},
},
wrapperComponent: Auth,
Expand Down Expand Up @@ -409,6 +423,7 @@ const wrappedRoutes = {
alertDetailRedirect,
unsubscribeRedirect,
unsubscribe,
subscriptionDetailRedirect,
};

export const unwrappedRoutes = unwrapRoute(Object.values(wrappedRoutes));
Expand Down
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

0 comments on commit 3c48b1c

Please sign in to comment.