11import {
22 useCallback ,
33 useMemo ,
4+ useState ,
45} from 'react' ;
56import { CloseLineIcon } from '@ifrc-go/icons' ;
67import {
@@ -41,6 +42,7 @@ import {
4142 COLOR_TEXT_ON_DARK ,
4243 DEFAULT_MAP_PADDING ,
4344 DURATION_MAP_ZOOM ,
45+ MAX_PAGE_LIMIT ,
4446} from '#utils/constants' ;
4547import { useRequest } from '#utils/restRequest' ;
4648
@@ -68,13 +70,22 @@ function Admin2Input<const NAME>(props: Props<NAME>) {
6870 const countryDetails = useCountry ( { id : countryId } ) ;
6971 const iso3 = countryDetails ?. iso3 ;
7072
71- const valueDebounced = useDebouncedValue ( value , 1000 ) ;
73+ const [ selectedCodes , setSelectedCodes ] = useState < string [ ] > ( [ ] ) ;
74+
75+ const selectedCodesDebounced = useDebouncedValue ( selectedCodes , 300 ) ;
7276
7377 const { response : admin2Details } = useRequest ( {
74- skip : isNotDefined ( valueDebounced ) || valueDebounced . length === 0 ,
78+ skip : isNotDefined ( selectedCodesDebounced ) || selectedCodesDebounced . length === 0 ,
7579 url : '/api/v2/admin2/' ,
7680 query : {
77- id__in : valueDebounced ?? [ ] ,
81+ code__in : selectedCodesDebounced ?? [ ] ,
82+ limit : MAX_PAGE_LIMIT ,
83+ } ,
84+ onSuccess : ( response ) => {
85+ onChange (
86+ response . results . map ( ( { id } ) => id ) ,
87+ name ,
88+ ) ;
7889 } ,
7990 } ) ;
8091
@@ -84,6 +95,12 @@ function Admin2Input<const NAME>(props: Props<NAME>) {
8495 ( { name : admin2Name , district_name } ) => `${ admin2Name } (${ district_name } )` ,
8596 ) ;
8697
98+ const admin2CodeMap = listToMap (
99+ admin2Details ?. results ,
100+ ( { id } ) => id ,
101+ ( { code } ) => code ,
102+ ) ;
103+
87104 const bounds = useMemo ( ( ) => {
88105 if ( ! countryDetails ) {
89106 return undefined ;
@@ -150,9 +167,9 @@ function Admin2Input<const NAME>(props: Props<NAME>) {
150167 ? defaultColor
151168 : [
152169 'match' ,
153- [ 'get' , 'id ' ] ,
170+ [ 'get' , 'code ' ] ,
154171 ...value . map ( ( admin2Id ) => [
155- admin2Id ,
172+ admin2CodeMap ?. [ admin2Id ] ?? admin2Id ,
156173 COLOR_PRIMARY_RED ,
157174 ] ) . flat ( ) ,
158175 defaultColor ,
@@ -165,7 +182,7 @@ function Admin2Input<const NAME>(props: Props<NAME>) {
165182 } ,
166183 } ;
167184 return options ;
168- } , [ iso3 , value ] ) ;
185+ } , [ iso3 , value , admin2CodeMap ] ) ;
169186
170187 const adminTwoLabelLayerOptions = useMemo ( ( ) : Omit < SymbolLayer , 'id' > | undefined => {
171188 const textColor : NonNullable < SymbolLayer [ 'paint' ] > [ 'text-color' ] = (
@@ -207,20 +224,22 @@ function Admin2Input<const NAME>(props: Props<NAME>) {
207224 name ?: string ;
208225 } ;
209226
210- if ( isNotDefined ( properties . id ) ) {
227+ if ( isNotDefined ( properties . code ) ) {
211228 return false ;
212229 }
213230
214- const valueIndex = value ?. findIndex ( ( admin2Id ) => admin2Id === properties . id ) ?? - 1 ;
231+ setSelectedCodes ( ( prevCodes ) => {
232+ const codeIndex = prevCodes . findIndex ( ( prevCode ) => prevCode === properties . code ) ;
215233
216- if ( valueIndex === - 1 ) {
217- onChange ( [ ...( value ?? [ ] ) , properties . id ] , name ) ;
218- } else {
219- onChange ( value ?. toSpliced ( valueIndex , 1 ) , name ) ;
220- }
234+ if ( codeIndex === - 1 ) {
235+ return [ ...prevCodes , properties . code ] ;
236+ }
237+
238+ return prevCodes . toSpliced ( codeIndex , 1 ) ;
239+ } ) ;
221240
222241 return false ;
223- } , [ value , name , onChange ] ) ;
242+ } , [ ] ) ;
224243
225244 const [
226245 showModal ,
0 commit comments