stream_data: Extract function to get stream options for dropdown.

To be used by various dropdown widgets in the app.
This commit is contained in:
Aman Agrawal
2023-06-20 15:00:30 +05:30
committed by Tim Abbott
parent 0024e88fcb
commit 29b3769b59
3 changed files with 152 additions and 16 deletions

View File

@@ -246,22 +246,7 @@ function item_click_callback(event, dropdown) {
} }
function get_options_for_recipient_widget() { function get_options_for_recipient_widget() {
const options = stream_data const options = stream_data.get_options_for_dropdown_widget();
.subscribed_subs()
.map((stream) => ({
name: stream.name,
unique_id: stream.stream_id,
stream,
}))
.sort((a, b) => {
if (a.name.toLowerCase() < b.name.toLowerCase()) {
return -1;
}
if (a.name.toLowerCase() > b.name.toLowerCase()) {
return 1;
}
return 0;
});
const direct_messages_option = { const direct_messages_option = {
is_direct_message: true, is_direct_message: true,

View File

@@ -849,3 +849,25 @@ export function initialize(params) {
export function remove_default_stream(stream_id) { export function remove_default_stream(stream_id) {
default_stream_ids.delete(stream_id); default_stream_ids.delete(stream_id);
} }
export function get_options_for_dropdown_widget() {
return (
subscribed_subs()
.map((stream) => ({
name: stream.name,
unique_id: stream.stream_id,
stream,
}))
// eslint-disable-next-line consistent-return, array-callback-return
.sort((a, b) => {
if (a.name.toLowerCase() < b.name.toLowerCase()) {
return -1;
}
if (a.name.toLowerCase() > b.name.toLowerCase()) {
return 1;
}
// Not possible since two streams cannot have same name.
// return 0;
})
);
}

View File

@@ -169,6 +169,35 @@ test("basics", () => {
assert.equal(sub_store.get(-3), undefined); assert.equal(sub_store.get(-3), undefined);
assert.equal(sub_store.get(undefined), undefined); assert.equal(sub_store.get(undefined), undefined);
assert.equal(sub_store.get(1), denmark); assert.equal(sub_store.get(1), denmark);
assert.deepEqual(stream_data.get_options_for_dropdown_widget(), [
{
name: "social",
stream: {
color: "red",
history_public_to_subscribers: false,
invite_only: true,
is_muted: false,
name: "social",
stream_id: 2,
stream_post_policy: 2,
subscribed: true,
},
unique_id: 2,
},
{
name: "test",
stream: {
color: "yellow",
invite_only: false,
is_muted: true,
name: "test",
stream_id: 3,
subscribed: true,
},
unique_id: 3,
},
]);
}); });
test("get_subscribed_streams_for_user", () => { test("get_subscribed_streams_for_user", () => {
@@ -981,3 +1010,103 @@ test("can_unsubscribe_others", () => {
page_params.is_admin = false; page_params.is_admin = false;
assert.equal(stream_data.can_unsubscribe_others(sub), false); assert.equal(stream_data.can_unsubscribe_others(sub), false);
}); });
test("options for dropdown widget", () => {
const denmark = {
subscribed: true,
color: "blue",
name: "Denmark",
stream_id: 1,
is_muted: true,
invite_only: true,
history_public_to_subscribers: true,
};
const social = {
subscribed: true,
color: "red",
name: "social",
stream_id: 2,
is_muted: false,
invite_only: true,
history_public_to_subscribers: false,
stream_post_policy: stream_data.stream_post_policy_values.admins.code,
};
const test = {
subscribed: true,
color: "yellow",
name: "test",
stream_id: 3,
is_muted: true,
invite_only: false,
};
const web_public_stream = {
subscribed: true,
color: "yellow",
name: "web_public_stream",
stream_id: 4,
is_muted: false,
invite_only: false,
history_public_to_subscribers: true,
is_web_public: true,
};
stream_data.add_sub(denmark);
stream_data.add_sub(social);
stream_data.add_sub(web_public_stream);
stream_data.add_sub(test);
assert.deepEqual(stream_data.get_options_for_dropdown_widget(), [
{
name: "Denmark",
stream: {
subscribed: true,
color: "blue",
name: "Denmark",
stream_id: 1,
is_muted: true,
invite_only: true,
history_public_to_subscribers: true,
},
unique_id: 1,
},
{
name: "social",
stream: {
color: "red",
history_public_to_subscribers: false,
invite_only: true,
is_muted: false,
name: "social",
stream_id: 2,
stream_post_policy: 2,
subscribed: true,
},
unique_id: 2,
},
{
name: "test",
stream: {
color: "yellow",
invite_only: false,
is_muted: true,
name: "test",
stream_id: 3,
subscribed: true,
},
unique_id: 3,
},
{
name: "web_public_stream",
stream: {
subscribed: true,
color: "yellow",
name: "web_public_stream",
stream_id: 4,
is_muted: false,
invite_only: false,
history_public_to_subscribers: true,
is_web_public: true,
},
unique_id: 4,
},
]);
});