Skip to content

Commit bb4b6f0

Browse files
committed
Add redirects from country to region
1 parent a23b9f6 commit bb4b6f0

File tree

4 files changed

+95
-16
lines changed

4 files changed

+95
-16
lines changed

src/App/routes/index.tsx

Lines changed: 63 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { Navigate, Outlet, useParams } from 'react-router-dom';
1+
import {
2+
Navigate,
3+
Outlet,
4+
generatePath,
5+
useParams,
6+
} from 'react-router-dom';
27
import { isDefined, isTruthyString } from '@togglecorp/fujs';
38

9+
import { Component as RootLayout } from '#views/RootLayout';
410
import {
511
wrapRoute,
612
unwrapRoute,
@@ -11,8 +17,18 @@ import type {
1117
MyOutputIndexRouteObject,
1218
MyOutputNonIndexRouteObject,
1319
} from '#utils/routes';
14-
15-
import { Component as RootLayout } from '#views/RootLayout';
20+
import {
21+
COUNTRY_AFRICA_REGION,
22+
COUNTRY_AMERICAS_REGION,
23+
COUNTRY_ASIA_REGION,
24+
COUNTRY_EUROPE_REGION,
25+
COUNTRY_MENA_REGION,
26+
REGION_AFRICA,
27+
REGION_AMERICAS,
28+
REGION_ASIA,
29+
REGION_EUROPE,
30+
REGION_MENA,
31+
} from '#utils/constants';
1632

1733
import Auth from '../Auth';
1834
import PageError from '../PageError';
@@ -342,24 +358,56 @@ const countriesLayout = customWrapRoute({
342358
},
343359
});
344360

345-
const countryIndex = customWrapRoute({
346-
parent: countriesLayout,
347-
index: true,
348-
component: {
349-
eagerLoad: true,
350-
render: SmartNavigate,
351-
props: {
352-
to: 'operations' satisfies DefaultCountriesChild,
353-
replace: true,
354-
hashToRouteMap: {
361+
// eslint-disable-next-line react-refresh/only-export-components
362+
function CountryNavigate() {
363+
const params = useParams<{ countryId: string }>();
364+
const countryIdToRegionIdMap: Record<number, number> = {
365+
[COUNTRY_AFRICA_REGION]: REGION_AFRICA,
366+
[COUNTRY_AMERICAS_REGION]: REGION_AMERICAS,
367+
[COUNTRY_ASIA_REGION]: REGION_ASIA,
368+
[COUNTRY_EUROPE_REGION]: REGION_EUROPE,
369+
[COUNTRY_MENA_REGION]: REGION_MENA,
370+
};
371+
372+
const countryId = isTruthyString(params.countryId) ? parseInt(params.countryId, 10) : undefined;
373+
const regionId = isDefined(countryId) ? countryIdToRegionIdMap[countryId] : undefined;
374+
375+
if (isDefined(regionId)) {
376+
const regionPath = generatePath(
377+
regionIndex.absoluteForwardPath,
378+
{ regionId },
379+
);
380+
return (
381+
<Navigate
382+
to={regionPath}
383+
replace
384+
/>
385+
);
386+
}
387+
388+
return (
389+
<SmartNavigate
390+
to={'operations' satisfies DefaultCountriesChild}
391+
replace
392+
hashToRouteMap={{
355393
'#3w': 'three-w',
356394
'#operations': 'operations',
357395
'#risk-watch': 'risk-watch',
358396
'#preparedness': 'preparedness',
359397
'#country-plan': 'plan',
360398
'#additional': 'additional-info',
361-
},
362-
},
399+
}}
400+
/>
401+
);
402+
}
403+
404+
const countryIndex = customWrapRoute({
405+
parent: countriesLayout,
406+
index: true,
407+
component: {
408+
eagerLoad: true,
409+
render: CountryNavigate,
410+
props: {},
363411
},
364412
context: {
365413
title: 'Country',

src/utils/constants.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,15 @@ export type DisasterCategory = components<'read'>['schemas']['DisasterCategoryEn
141141
export const DISASTER_CATEGORY_YELLOW = 0 satisfies DisasterCategory;
142142
export const DISASTER_CATEGORY_ORANGE = 1 satisfies DisasterCategory;
143143
export const DISASTER_CATEGORY_RED = 2 satisfies DisasterCategory;
144+
145+
export const COUNTRY_AMERICAS_REGION = 282;
146+
export const COUNTRY_ASIA_REGION = 283;
147+
export const COUNTRY_AFRICA_REGION = 285;
148+
export const COUNTRY_EUROPE_REGION = 286;
149+
export const COUNTRY_MENA_REGION = 287;
150+
export type Region = components<'read'>['schemas']['ApiRegionNameEnum'];
151+
export const REGION_AFRICA = 0;
152+
export const REGION_AMERICAS = 1;
153+
export const REGION_ASIA = 2;
154+
export const REGION_EUROPE = 3;
155+
export const REGION_MENA = 4;

src/views/Country/index.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import {
99
AppealsTwoIcon,
1010
PencilFillIcon,
1111
} from '@ifrc-go/icons';
12-
import { isDefined, isNotDefined, isTruthyString } from '@togglecorp/fujs';
12+
import {
13+
isDefined,
14+
isNotDefined,
15+
isTruthyString,
16+
isFalsyString,
17+
} from '@togglecorp/fujs';
1318

1419
import { resolveUrl } from '#utils/resolveUrl';
1520
import BlockLoading from '#components/BlockLoading';
@@ -104,6 +109,19 @@ export function Component() {
104109
);
105110
}
106111

112+
if (isDefined(countryResponse) && isFalsyString(countryResponse.iso3)) {
113+
return (
114+
<Page
115+
title={pageTitle}
116+
className={styles.country}
117+
>
118+
<Message
119+
title={strings.countryLoadingErrorMessage}
120+
/>
121+
</Page>
122+
);
123+
}
124+
107125
return (
108126
<Page
109127
className={styles.country}

src/views/RegionOperations/RecentEmergenciesTable/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ function EventItemsTable(props: Props) {
152152
<Container
153153
className={styles.recentEmergenciesTable}
154154
heading={heading}
155+
withHeaderBorder
155156
actions={(
156157
<Link
157158
to="allEmergencies"

0 commit comments

Comments
 (0)