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' ;
38import {
49 DrefIcon ,
510 AppealsIcon ,
@@ -34,25 +39,47 @@ import { useRequest } from '#utils/restRequest';
3439import { type CountryOutletContext } from '#utils/outletContext' ;
3540import { resolveToString } from '#utils/translation' ;
3641import { 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' ;
3754import { adminUrl } from '#config' ;
55+ import RouteContext from '#contexts/route' ;
3856
3957import i18n from './i18n.json' ;
4058import styles from './styles.module.css' ;
4159
4260// eslint-disable-next-line import/prefer-default-export
4361export 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