Skip to content

Commit a4b6bd3

Browse files
committed
Add proper redirects from country to region
1 parent 77c804b commit a4b6bd3

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

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)