Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 33 additions & 14 deletions app/src/components/domain/Admin2Input/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
useCallback,
useMemo,
useState,
} from 'react';
import { CloseLineIcon } from '@ifrc-go/icons';
import {
Expand Down Expand Up @@ -41,6 +42,7 @@ import {
COLOR_TEXT_ON_DARK,
DEFAULT_MAP_PADDING,
DURATION_MAP_ZOOM,
MAX_PAGE_LIMIT,
} from '#utils/constants';
import { useRequest } from '#utils/restRequest';

Expand Down Expand Up @@ -68,13 +70,22 @@ function Admin2Input<const NAME>(props: Props<NAME>) {
const countryDetails = useCountry({ id: countryId });
const iso3 = countryDetails?.iso3;

const valueDebounced = useDebouncedValue(value, 1000);
const [selectedCodes, setSelectedCodes] = useState<string[]>([]);

const selectedCodesDebounced = useDebouncedValue(selectedCodes, 300);

const { response: admin2Details } = useRequest({
skip: isNotDefined(valueDebounced) || valueDebounced.length === 0,
skip: isNotDefined(selectedCodesDebounced) || selectedCodesDebounced.length === 0,
url: '/api/v2/admin2/',
query: {
id__in: valueDebounced ?? [],
code__in: selectedCodesDebounced ?? [],
limit: MAX_PAGE_LIMIT,
},
onSuccess: (response) => {
onChange(
response.results.map(({ id }) => id),
name,
);
},
});

Expand All @@ -84,6 +95,12 @@ function Admin2Input<const NAME>(props: Props<NAME>) {
({ name: admin2Name, district_name }) => `${admin2Name} (${district_name})`,
);

const admin2CodeMap = listToMap(
admin2Details?.results,
({ id }) => id,
({ code }) => code,
);

const bounds = useMemo(() => {
if (!countryDetails) {
return undefined;
Expand Down Expand Up @@ -150,9 +167,9 @@ function Admin2Input<const NAME>(props: Props<NAME>) {
? defaultColor
: [
'match',
['get', 'id'],
['get', 'code'],
...value.map((admin2Id) => [
admin2Id,
admin2CodeMap?.[admin2Id] ?? admin2Id,
COLOR_PRIMARY_RED,
]).flat(),
defaultColor,
Expand All @@ -165,7 +182,7 @@ function Admin2Input<const NAME>(props: Props<NAME>) {
},
};
return options;
}, [iso3, value]);
}, [iso3, value, admin2CodeMap]);

const adminTwoLabelLayerOptions = useMemo((): Omit<SymbolLayer, 'id'> | undefined => {
const textColor: NonNullable<SymbolLayer['paint']>['text-color'] = (
Expand Down Expand Up @@ -207,20 +224,22 @@ function Admin2Input<const NAME>(props: Props<NAME>) {
name?: string;
};

if (isNotDefined(properties.id)) {
if (isNotDefined(properties.code)) {
return false;
}

const valueIndex = value?.findIndex((admin2Id) => admin2Id === properties.id) ?? -1;
setSelectedCodes((prevCodes) => {
const codeIndex = prevCodes.findIndex((prevCode) => prevCode === properties.code);

if (valueIndex === -1) {
onChange([...(value ?? []), properties.id], name);
} else {
onChange(value?.toSpliced(valueIndex, 1), name);
}
if (codeIndex === -1) {
return [...prevCodes, properties.code];
}

return prevCodes.toSpliced(codeIndex, 1);
});

return false;
}, [value, name, onChange]);
}, []);

const [
showModal,
Expand Down
2 changes: 1 addition & 1 deletion go-api
Submodule go-api updated 2 files
+1 −0 api/filter_set.py
+1 −1 assets
Loading