Skip to content

Commit 9f39c1e

Browse files
authored
Merge pull request #474 from IFRCGo/fix/country-region-redirect
Add proper redirects from country to region
2 parents 686be2b + 1d4503f commit 9f39c1e

File tree

3 files changed

+83
-5
lines changed

3 files changed

+83
-5
lines changed

src/App/routes/index.tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2437,9 +2437,10 @@ const allSurgeAlerts = customWrapRoute({
24372437
},
24382438
});
24392439

2440+
type AllDeployedPersonnelPath = 'deployed-personnels/all';
24402441
const allDeployedPersonnel = customWrapRoute({
24412442
parent: rootLayout,
2442-
path: 'deployed-personnels/all',
2443+
path: 'deployed-personnels/all' satisfies AllDeployedPersonnelPath,
24432444
component: {
24442445
render: () => import('#views/AllDeployedPersonnel'),
24452446
props: {},
@@ -2451,6 +2452,24 @@ const allDeployedPersonnel = customWrapRoute({
24512452
},
24522453
});
24532454

2455+
const allDeployedPersonnelOld = customWrapRoute({
2456+
parent: rootLayout,
2457+
path: 'personnels/all',
2458+
component: {
2459+
eagerLoad: true,
2460+
render: Navigate,
2461+
props: {
2462+
to: 'deployed-personnels/all' satisfies AllDeployedPersonnelPath,
2463+
replace: true,
2464+
},
2465+
},
2466+
wrapperComponent: Auth,
2467+
context: {
2468+
title: 'All Deployed Personnel',
2469+
visibility: 'anything',
2470+
},
2471+
});
2472+
24542473
const allDeployedEmergencyResponseUnits = customWrapRoute({
24552474
parent: rootLayout,
24562475
path: 'deployed-erus/all',
@@ -3033,6 +3052,7 @@ const wrappedRoutes = {
30333052
allSurgeAlerts,
30343053
allFlashUpdates,
30353054
allDeployedPersonnel,
3055+
allDeployedPersonnelOld,
30363056
allDeployedEmergencyResponseUnits,
30373057
newDrefApplicationForm,
30383058
drefApplicationForm,

src/utils/constants.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,10 @@ export const REGION_AMERICAS = 1;
157157
export const REGION_ASIA = 2;
158158
export const REGION_EUROPE = 3;
159159
export const REGION_MENA = 4;
160+
161+
type CountryRecordTypeEnum = components<'read'>['schemas']['RecordTypeEnum'];
162+
export const COUNTRY_RECORD_TYPE_COUNTRY = 1 satisfies CountryRecordTypeEnum;
163+
export const COUNTRY_RECORD_TYPE_CLUSTER = 2 satisfies CountryRecordTypeEnum;
164+
export const COUNTRY_RECORD_TYPE_REGION = 3 satisfies CountryRecordTypeEnum;
165+
export const COUNTRY_RECORD_TYPE_COUNTRY_OFFICE = 4 satisfies CountryRecordTypeEnum;
166+
export const COUNTRY_RECORD_TYPE_REPRESENTATIVE_OFFICE = 5 satisfies CountryRecordTypeEnum;

src/views/Country/index.tsx

Lines changed: 55 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
import { useParams, Outlet } from 'react-router-dom';
2-
import { useMemo } from 'react';
1+
import {
2+
useParams,
3+
Outlet,
4+
Navigate,
5+
generatePath,
6+
} from 'react-router-dom';
7+
import { useContext, useMemo } from 'react';
38
import {
49
DrefIcon,
510
AppealsIcon,
@@ -34,25 +39,47 @@ import { useRequest } from '#utils/restRequest';
3439
import { type CountryOutletContext } from '#utils/outletContext';
3540
import { resolveToString } from '#utils/translation';
3641
import { getPercentage } from '#utils/common';
42+
import {
43+
COUNTRY_AFRICA_REGION,
44+
COUNTRY_AMERICAS_REGION,
45+
COUNTRY_ASIA_REGION,
46+
COUNTRY_EUROPE_REGION,
47+
COUNTRY_MENA_REGION,
48+
REGION_AFRICA,
49+
REGION_AMERICAS,
50+
REGION_ASIA,
51+
REGION_EUROPE,
52+
REGION_MENA,
53+
} from '#utils/constants';
3754
import { adminUrl } from '#config';
55+
import RouteContext from '#contexts/route';
3856

3957
import i18n from './i18n.json';
4058
import styles from './styles.module.css';
4159

4260
// eslint-disable-next-line import/prefer-default-export
4361
export function Component() {
4462
const { countryId } = useParams<{ countryId: string }>();
63+
const { regionIndex } = useContext(RouteContext);
4564

4665
const strings = useTranslation(i18n);
4766
const country = useCountry({ id: Number(countryId) });
4867
const region = useRegion({ id: country?.region });
4968

69+
const numericCountryId = isDefined(countryId) ? Number(countryId) : undefined;
70+
71+
const isRegion = numericCountryId === COUNTRY_ASIA_REGION
72+
|| numericCountryId === COUNTRY_AFRICA_REGION
73+
|| numericCountryId === COUNTRY_AMERICAS_REGION
74+
|| numericCountryId === COUNTRY_EUROPE_REGION
75+
|| numericCountryId === COUNTRY_MENA_REGION;
76+
5077
const {
5178
pending: countryResponsePending,
5279
response: countryResponse,
5380
error: countryResponseError,
5481
} = useRequest({
55-
skip: isNotDefined(countryId),
82+
skip: isNotDefined(countryId) || isRegion,
5683
url: '/api/v2/country/{id}/',
5784
pathVariables: {
5885
id: Number(countryId),
@@ -65,7 +92,7 @@ export function Component() {
6592
pending: aggregatedAppealPending,
6693
response: aggregatedAppealResponse,
6794
} = useRequest({
68-
skip: isNotDefined(countryId),
95+
skip: isNotDefined(countryId) || isRegion,
6996
url: '/api/v2/appeal/aggregated',
7097
query: { country: Number(countryId) },
7198
});
@@ -95,6 +122,30 @@ export function Component() {
95122
{ countryName: country?.name ?? strings.countryPageTitleFallbackCountry },
96123
);
97124

125+
if (isRegion) {
126+
const countryIdToRegionIdMap: Record<number, number> = {
127+
[COUNTRY_AFRICA_REGION]: REGION_AFRICA,
128+
[COUNTRY_AMERICAS_REGION]: REGION_AMERICAS,
129+
[COUNTRY_ASIA_REGION]: REGION_ASIA,
130+
[COUNTRY_EUROPE_REGION]: REGION_EUROPE,
131+
[COUNTRY_MENA_REGION]: REGION_MENA,
132+
};
133+
134+
const regionId = countryIdToRegionIdMap[numericCountryId];
135+
136+
const regionPath = generatePath(
137+
regionIndex.absoluteForwardPath,
138+
{ regionId },
139+
);
140+
141+
return (
142+
<Navigate
143+
to={regionPath}
144+
replace
145+
/>
146+
);
147+
}
148+
98149
if (isDefined(countryResponseError)) {
99150
return (
100151
<Page

0 commit comments

Comments
 (0)