settings_streams: Fix error thrown when adding 'default-channels'.

The 'Select channel' dropdown in 'Add default channels' modal is
the LAST dropdown element on the modal and it does not contain
`data-stream-id` attribute.

.attr('data-stream-id') would return undefined for this element
and as a result the returned `Set` would have its last element a
`NaN` --- passing a `NaN` inside 'data' field of 'channel.post' threw
an error.

We tweaked the selector string to selectively map over the elements with
'data-stream-id' attribute.
Also we removed '.toString()' converting stream-id to a string (stream-id is a
'number' type).
This commit is contained in:
Varun Singh 2024-05-28 11:47:13 +05:30 committed by Tim Abbott
parent 4deecfa58d
commit 50e0f336f0
3 changed files with 8 additions and 5 deletions

View File

@ -28,8 +28,11 @@ function add_choice_row($widget) {
function get_chosen_default_streams() { function get_chosen_default_streams() {
// Return the set of stream id's of streams chosen in the default stream modal. // Return the set of stream id's of streams chosen in the default stream modal.
return new Set( return new Set(
$("#default-stream-choices .choice-row .dropdown_widget_value") // Note: We selectively map through the dropdown elements that contain the
.map((_i, elem) => Number($(elem).attr("data-stream-id")).toString()) // `data-stream-id` attribute to avoid adding an element with undefined value
// into the returned `Set`.
$("#default-stream-choices .choice-row .dropdown_widget_value[data-stream-id]")
.map((_i, elem) => Number($(elem).attr("data-stream-id")))
.get(), .get(),
); );
} }

View File

@ -296,12 +296,12 @@ export function delete_sub(stream_id: number): void {
stream_info.delete(stream_id); stream_info.delete(stream_id);
} }
export function get_non_default_stream_names(): {name: string; unique_id: string}[] { export function get_non_default_stream_names(): {name: string; unique_id: number}[] {
let subs = [...stream_info.values()]; let subs = [...stream_info.values()];
subs = subs.filter((sub) => !is_default_stream_id(sub.stream_id) && !sub.invite_only); subs = subs.filter((sub) => !is_default_stream_id(sub.stream_id) && !sub.invite_only);
const names = subs.map((sub) => ({ const names = subs.map((sub) => ({
name: sub.name, name: sub.name,
unique_id: sub.stream_id.toString(), unique_id: sub.stream_id,
})); }));
return names; return names;
} }

View File

@ -491,7 +491,7 @@ test("default_stream_names", () => {
stream_data.add_sub(general); stream_data.add_sub(general);
const names = stream_data.get_non_default_stream_names(); const names = stream_data.get_non_default_stream_names();
assert.deepEqual(names.sort(), [{name: "public", unique_id: "102"}]); assert.deepEqual(names.sort(), [{name: "public", unique_id: 102}]);
const default_stream_ids = stream_data.get_default_stream_ids(); const default_stream_ids = stream_data.get_default_stream_ids();
assert.deepEqual(default_stream_ids.sort(), [announce.stream_id, general.stream_id]); assert.deepEqual(default_stream_ids.sort(), [announce.stream_id, general.stream_id]);