From 3303b5ab726c7f189262a454dd40ff5f47211d79 Mon Sep 17 00:00:00 2001 From: Aman Agrawal Date: Sat, 5 Aug 2023 07:16:26 +0530 Subject: [PATCH] dropdown_widget: Show disabled if value not in options. It is possible that the current value of a dropdown widget is valid but not present in options since the current user doesn't have access it. So, we show [disabled] as value in that case. This can be reproduced by setting a private stream for notifications in org settings and then opening org settings as a user which doesn't have access to the private stream. --- web/src/dropdown_widget.js | 18 +++++++++++++++++- web/src/settings_org.js | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/web/src/dropdown_widget.js b/web/src/dropdown_widget.js index cf4a6847fd..b30862480f 100644 --- a/web/src/dropdown_widget.js +++ b/web/src/dropdown_widget.js @@ -41,6 +41,8 @@ export class DropdownWidget { // NOTE: Any value other than `null` will be rendered when class is initialized. default_id = null, unique_id_type = null, + // Show disabled state if the default_id is not in `get_options()`. + show_disabled_if_current_value_not_in_options = false, }) { this.widget_name = widget_name; this.widget_id = `#${CSS.escape(widget_name)}_widget`; @@ -62,6 +64,8 @@ export class DropdownWidget { this.current_value = default_id; this.unique_id_type = unique_id_type; this.$events_container = $events_container; + this.show_disabled_if_current_value_not_in_options = + show_disabled_if_current_value_not_in_options; } init() { @@ -254,7 +258,19 @@ export class DropdownWidget { this.current_value = value; } - const option = this.get_options().find((option) => option.unique_id === this.current_value); + const all_options = this.get_options(); + let option = all_options.find((option) => option.unique_id === this.current_value); + + // Show disabled if cannot find current option. + if (!option && this.show_disabled_if_current_value_not_in_options) { + option = all_options.find((option) => option.is_setting_disabled === true); + } + + if (!option) { + blueslip.error(`Cannot find current value: ${this.current_value} in provided options.`); + return; + } + if (option.is_setting_disabled) { $(this.widget_value_selector).html(render_dropdown_disabled_state({name: option.name})); } else if (option.stream) { diff --git a/web/src/settings_org.js b/web/src/settings_org.js index 68c8e159d0..230c6e2c70 100644 --- a/web/src/settings_org.js +++ b/web/src/settings_org.js @@ -1043,6 +1043,7 @@ export function init_dropdown_widgets() { }, default_id: page_params.realm_notifications_stream_id, unique_id_type: dropdown_widget.DATA_TYPES.NUMBER, + show_disabled_if_current_value_not_in_options: true, }); notifications_stream_widget.setup(); @@ -1062,6 +1063,7 @@ export function init_dropdown_widgets() { }, default_id: page_params.realm_signup_notifications_stream_id, unique_id_type: dropdown_widget.DATA_TYPES.NUMBER, + show_disabled_if_current_value_not_in_options: true, }); signup_notifications_stream_widget.setup();