From 64d224d041c46cf2b8e060554e240873ade4831d Mon Sep 17 00:00:00 2001
From: Suhas Vishwanath <suhasv@samanvayfoundation.org>
Date: Tue, 27 Jun 2023 12:45:16 +0530
Subject: [PATCH] #1020 | Fix selected gender toggle (cherry picked from commit
 b41810d534da9fbd52a98839ee724a0c3d6ee4ed)

---
 .../src/action/individual/IndividualSearchActions.js        | 6 ++++--
 packages/openchs-android/src/views/filter/FiltersViewV2.js  | 2 +-
 .../src/views/individual/IndividualSearchView.js            | 4 ++--
 3 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/packages/openchs-android/src/action/individual/IndividualSearchActions.js b/packages/openchs-android/src/action/individual/IndividualSearchActions.js
index 8a077bd59..3a535e669 100644
--- a/packages/openchs-android/src/action/individual/IndividualSearchActions.js
+++ b/packages/openchs-android/src/action/individual/IndividualSearchActions.js
@@ -8,7 +8,7 @@ import _ from "lodash";
 import PrivilegeService from "../../service/PrivilegeService";
 import {firebaseEvents, logEvent} from "../../utility/Analytics";
 import AddressLevelState from "../common/AddressLevelsState";
-import { CustomFilter } from "openchs-models";
+import {ArrayUtil, CustomFilter} from "openchs-models";
 
 export class IndividualSearchActions {
     static clone(state) {
@@ -176,7 +176,9 @@ export class IndividualSearchActions {
     }
 
     static genderChange(state, action) {
-        return {...state, selectedGenders: action.selectedGenders};
+        let selectedGenders = state.selectedGenders || [];
+        ArrayUtil.toggle(selectedGenders, action.selectedGender, (l,r) => l.uuid === r.uuid)
+        return {...state, selectedGenders};
     }
 
     static loadIndicator(state, action) {
diff --git a/packages/openchs-android/src/views/filter/FiltersViewV2.js b/packages/openchs-android/src/views/filter/FiltersViewV2.js
index 51e4f0a38..32bc90402 100644
--- a/packages/openchs-android/src/views/filter/FiltersViewV2.js
+++ b/packages/openchs-android/src/views/filter/FiltersViewV2.js
@@ -164,7 +164,7 @@ class FiltersViewV2 extends AbstractComponent {
 
                                 switch (filterConfig.type) {
                                     case CustomFilter.type.Gender:
-                                        return <GenderFilter selectedGenders={filterValue}
+                                        return <GenderFilter selectedGenders={filterValue || []}
                                                              filterLabel={this.I18n.t(filter.name)}
                                                              key={index}
                                                              deprecatedUsage={false}
diff --git a/packages/openchs-android/src/views/individual/IndividualSearchView.js b/packages/openchs-android/src/views/individual/IndividualSearchView.js
index ccae80f99..dc7175a08 100644
--- a/packages/openchs-android/src/views/individual/IndividualSearchView.js
+++ b/packages/openchs-android/src/views/individual/IndividualSearchView.js
@@ -129,8 +129,8 @@ class IndividualSearchView extends AbstractComponent {
                                 /> : null}
                             {this.customFilterService.filterTypePresent(filterScreenName, CustomFilter.type.Gender, subjectTypeUUID) &&
                             <GenderFilter
-                                selectedGenders={this.state.selectedGenders}
-                                onSelect={(selectedGenders) => this.dispatchAction(Actions.GENDER_CHANGE, {selectedGenders})}
+                                selectedGenders={this.state.selectedGenders || []}
+                                onSelect={(selectedGender) => this.dispatchAction(Actions.GENDER_CHANGE, {selectedGender})}
                             />}
                             {this.customFilterService.filterTypePresent(filterScreenName, CustomFilter.type.Address, subjectTypeUUID) &&
                             <AddressLevels