Skip to content

Commit 39cd542

Browse files
subinasrfrozenhelium
authored andcommitted
fix(alert-preferences): country/region selections
1 parent b8a86ba commit 39cd542

4 files changed

Lines changed: 99 additions & 21 deletions

File tree

app/src/views/AccountNotifications/EmailPreferences/SubscriptionModal/i18n.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"strings": {
44
"addSubscriptionHeading": "New subscription",
55
"editSubscriptionHeading": "Edit subscription",
6+
"earthquakeLabel": "Earthquake",
7+
"cycloneLabel": "Cyclone",
8+
"floodLabel": "Flood",
69
"updateButtonLabel": "Save",
710
"cancelButtonLabel": "Cancel",
811
"subscriptionTitleLabel": "Enter subscription title",

app/src/views/AccountNotifications/EmailPreferences/SubscriptionModal/index.tsx

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import {
3838
import NonFieldError from '#components/NonFieldError';
3939
import { type components } from '#generated/types';
4040
import useCountry from '#hooks/domain/useCountry';
41-
import useDisasterTypes, { type DisasterType } from '#hooks/domain/useDisasterType';
41+
import { type DisasterType } from '#hooks/domain/useDisasterType';
4242
import useGlobalEnums from '#hooks/domain/useGlobalEnums';
4343
import useAlert from '#hooks/useAlert';
4444
import {
@@ -62,7 +62,7 @@ function alertPerDayKeySelector(option: AlertPerDayEnums) {
6262
return option.key;
6363
}
6464

65-
const disasterTypeLabelSelector = (disasterType: DisasterType) => disasterType.name ?? '?';
65+
const disasterTypeLabelSelector = (disasterType: Pick<DisasterType, 'id' | 'name'>) => disasterType.name ?? '?';
6666

6767
interface Props {
6868
onClose: () => void;
@@ -85,7 +85,21 @@ function SubscriptionModal(props: Props) {
8585
const strings = useTranslation(i18n);
8686
const alert = useAlert();
8787

88-
const disasterTypeOptions = useDisasterTypes();
88+
// NOTE: Email alerts only supports 3 disasters at the moment
89+
const disasterTypeOptions = [
90+
{
91+
id: 2,
92+
name: strings.earthquakeLabel,
93+
},
94+
{
95+
id: 4,
96+
name: strings.cycloneLabel,
97+
},
98+
{
99+
id: 12,
100+
name: strings.floodLabel,
101+
},
102+
];
89103

90104
const defaultFormValue: PartialFormFields = useMemo(() => ({
91105
user: userId,
@@ -289,7 +303,9 @@ function SubscriptionModal(props: Props) {
289303
region,
290304
];
291305
}
292-
306+
if (regionIndex === -1) {
307+
return selectedRegions;
308+
}
293309
if (isSelected) {
294310
return selectedRegions;
295311
}
@@ -304,7 +320,7 @@ function SubscriptionModal(props: Props) {
304320
setFieldValue(
305321
(selectedCountries: number[] | undefined = []) => {
306322
const countryIndex = selectedCountries.findIndex(
307-
(selectedRegion) => selectedRegion === country,
323+
(selectedCountry) => selectedCountry === country,
308324
);
309325

310326
if (countryIndex === -1 && isSelected) {
@@ -325,8 +341,8 @@ function SubscriptionModal(props: Props) {
325341

326342
if (isSelected) {
327343
const countryDetails = countryDetailsById[country];
328-
if (isDefined(countryDetails)) {
329-
updateRegionSelection(false, countryDetails.id);
344+
if (isDefined(countryDetails?.region)) {
345+
updateRegionSelection(false, countryDetails?.region);
330346
}
331347
}
332348
}, [setFieldValue, countryDetailsById, updateRegionSelection]);
@@ -420,10 +436,10 @@ function SubscriptionModal(props: Props) {
420436
{regionOptions?.map((region) => {
421437
const regionCountries = regionGroupedCountries[region.key];
422438
const selectedCountries = regionCountries?.filter(
423-
(country) => selectedCountriesIdMap?.[country.id],
439+
(country) => !!selectedCountriesIdMap?.[country.id],
424440
);
425441
const hasSelectedCountries = isDefined(selectedCountries)
426-
&& selectedCountries?.length !== 0;
442+
&& selectedCountries?.length > 0;
427443

428444
return (
429445
<ExpandableContainer

app/src/views/AccountNotifications/EmailPreferences/i18n.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"namespace": "emailPreferences",
33
"strings": {
44
"heading": "Email Alert Subscriptions",
5+
"description": "Alerts are powered by Montandon data from GDACS and USGS. You'll receive email alerts with key impact figures and similar past events in the area.",
56
"emptyMessage": "No subscriptions",
67
"alertFrequencies": "Alert limit (per day)",
78
"unlimitedOption": "No limit",
@@ -10,6 +11,12 @@
1011
"disasters": "Disasters",
1112
"addSubscriptionLabel": "New Alert Subscription",
1213
"editSubscriptionLabel": "Edit",
13-
"editSubscriptionTitle": "Edit Alert Subscription"
14+
"editSubscriptionTitle": "Edit Alert Subscription",
15+
"deleteSubscriptionLabel": "Delete",
16+
"deleteSubscriptionTitle": "Delete Alert Subscription",
17+
"deleteSubscriptionConfirmationHeading": "Delete Alert Subscription",
18+
"deleteSubscriptionConfirmationMessage": "Are you sure you want to delete your subscription?",
19+
"subscriptionDeletedSuccessMessage": "Your subscription has been deleted.",
20+
"subscriptionDeletedFailureMessage": "Subscription could not be deleted."
1421
}
1522
}

app/src/views/AccountNotifications/EmailPreferences/index.tsx

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import {
44
} from 'react';
55
import {
66
AddLineIcon,
7+
DeleteBinTwoLineIcon,
78
PencilLineIcon,
89
} from '@ifrc-go/icons';
910
import {
1011
Button,
12+
ConfirmButton,
1113
Container,
1214
ListView,
1315
TextOutput,
@@ -22,7 +24,11 @@ import {
2224
} from '@togglecorp/fujs';
2325

2426
import useUserMe from '#hooks/domain/useUserMe';
25-
import { useRequest } from '#utils/restRequest';
27+
import useAlert from '#hooks/useAlert';
28+
import {
29+
useLazyRequest,
30+
useRequest,
31+
} from '#utils/restRequest';
2632

2733
import SubscriptionModal from './SubscriptionModal';
2834

@@ -31,6 +37,7 @@ import i18n from './i18n.json';
3137
function EmailPreferences() {
3238
const user = useUserMe();
3339
const strings = useTranslation(i18n);
40+
const alert = useAlert();
3441

3542
const {
3643
response: subscriptionListResponse,
@@ -41,6 +48,29 @@ function EmailPreferences() {
4148
method: 'GET',
4249
});
4350

51+
const {
52+
pending: subscriptionDeletePending,
53+
trigger: deleteSubscription,
54+
} = useLazyRequest({
55+
method: 'DELETE',
56+
url: '/api/v2/alert-subscription/{id}/',
57+
pathVariables: ({ id }) => ({ id }),
58+
onSuccess: () => {
59+
refetchSubscriptionList();
60+
alert.show(
61+
strings.subscriptionDeletedSuccessMessage,
62+
{ variant: 'success' },
63+
);
64+
},
65+
onFailure: () => {
66+
alert.show(
67+
strings.subscriptionDeletedFailureMessage,
68+
{ variant: 'danger' },
69+
);
70+
},
71+
72+
});
73+
4474
const subscriptionList = subscriptionListResponse?.results;
4575

4676
const [subscriptionModalVisible, {
@@ -61,6 +91,10 @@ function EmailPreferences() {
6191
setActiveSubscriptionId(subId);
6292
}, [setActiveSubscriptionId, showSubscriptionModal]);
6393

94+
const handleSubscriptionDelete = useCallback((subId: number) => {
95+
deleteSubscription({ id: subId });
96+
}, [deleteSubscription]);
97+
6498
const handleSubscriptionAddClick = useCallback(() => {
6599
showSubscriptionModal();
66100
setActiveSubscriptionId(undefined);
@@ -70,6 +104,7 @@ function EmailPreferences() {
70104
<>
71105
<Container
72106
heading={strings.heading}
107+
headerDescription={strings.description}
73108
pending={subscriptionListPending}
74109
empty={isNotDefined(subscriptionList) || subscriptionList?.length === 0}
75110
emptyMessage={strings.emptyMessage}
@@ -96,16 +131,33 @@ function EmailPreferences() {
96131
withDarkBackground
97132
withPadding
98133
headerActions={(
99-
<Button
100-
name={item.id}
101-
onClick={handleSubscriptionEditClick}
102-
title={strings.editSubscriptionTitle}
103-
before={<PencilLineIcon />}
104-
styleVariant="action"
105-
colorVariant="primary"
106-
>
107-
{strings.editSubscriptionLabel}
108-
</Button>
134+
<>
135+
<Button
136+
name={item.id}
137+
onClick={handleSubscriptionEditClick}
138+
title={strings.editSubscriptionTitle}
139+
before={<PencilLineIcon />}
140+
styleVariant="action"
141+
colorVariant="primary"
142+
>
143+
{strings.editSubscriptionLabel}
144+
</Button>
145+
<ConfirmButton
146+
name={item.id}
147+
styleVariant="action"
148+
colorVariant="primary"
149+
title={strings.deleteSubscriptionTitle}
150+
onConfirm={handleSubscriptionDelete}
151+
// eslint-disable-next-line max-len
152+
confirmHeading={strings.deleteSubscriptionConfirmationHeading}
153+
// eslint-disable-next-line max-len
154+
confirmMessage={strings.deleteSubscriptionConfirmationMessage}
155+
before={<DeleteBinTwoLineIcon />}
156+
disabled={subscriptionDeletePending}
157+
>
158+
{strings.deleteSubscriptionLabel}
159+
</ConfirmButton>
160+
</>
109161
)}
110162
headingLevel={5}
111163
withHeaderBorder

0 commit comments

Comments
 (0)