From 7805cc159194e53c83b75127706635d7211b2d6e Mon Sep 17 00:00:00 2001
From: Arild Matsson <arild.matsson@gu.se>
Date: Wed, 4 Dec 2024 16:23:48 +0100
Subject: [PATCH] fix: handle removed options in reduceSelect

---
 app/scripts/components/reduce-select.ts | 9 ++++++---
 app/scripts/components/searchtabs.ts    | 4 +---
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/app/scripts/components/reduce-select.ts b/app/scripts/components/reduce-select.ts
index 53788ad3..910baf7b 100644
--- a/app/scripts/components/reduce-select.ts
+++ b/app/scripts/components/reduce-select.ts
@@ -110,7 +110,7 @@ angular.module("korpApp").component("reduceSelect", {
                 }
 
                 // Only after initialization
-                if ($ctrl.items && $ctrl.selected && $ctrl.insensitive) validate()
+                if ($ctrl.items && $ctrl.selected && $ctrl.insensitive) updateSelected()
             }
 
             /** Report any changes upwards */
@@ -120,11 +120,14 @@ angular.module("korpApp").component("reduceSelect", {
                 const selected = $ctrl.items.filter((item) => item.selected).map((item) => item.value)
                 const insensitive = $ctrl.items.filter((item) => item.insensitive).map((item) => item.value)
 
-                $ctrl.onChange({
+                const changes = {
                     // Only set values that have changed
                     selected: !_.isEqual(selected, $ctrl.selected) ? selected : undefined,
                     insensitive: !_.isEqual(insensitive, $ctrl.insensitive) ? insensitive : undefined,
-                })
+                }
+
+                // Only notify if something changed
+                if (changes.selected || changes.insensitive) $ctrl.onChange(changes)
             }
 
             /** Fix state inconsistencies */
diff --git a/app/scripts/components/searchtabs.ts b/app/scripts/components/searchtabs.ts
index 65a401ab..5385d260 100644
--- a/app/scripts/components/searchtabs.ts
+++ b/app/scripts/components/searchtabs.ts
@@ -226,9 +226,7 @@ angular.module("korpApp").component("searchtabs", {
                 $ctrl.statCurrentAttrs = _.filter(allAttrs, (item) => !item["hide_statistics"])
                 $ctrl.statSelectedAttrs = ($location.search().stats_reduce || "word").split(",")
                 const insensitiveAttrs = $location.search().stats_reduce_insensitive
-                if (insensitiveAttrs) {
-                    $ctrl.statInsensitiveAttrs = insensitiveAttrs.split(",")
-                }
+                $ctrl.statInsensitiveAttrs = insensitiveAttrs?.split(",") || []
             })
 
             $ctrl.reduceOnChange = ({ selected, insensitive }) => {