compose_state: Use stream_name() and topic() universally.

This change ensures that every call to find the stream name or topic
in the composebox is calling compose_state functions instead of directly
taking a value from an HTML element specified by a classname.

This has better code readability and abstracts away the classname,
and also makes it easier to change which classname we use (which
will be happening in an upcoming change as part of switching the
stream name field to a dropdown).

Note that this change means that the stream name will always be
trimmed, whereas before it was whatever the user had written even
if it had trailing whitespace. This shouldn't be an issue as far
as we (me + Tim) can tell, and also it will become irrelevant as
soon as the dropdown changes land.
This commit is contained in:
evykassirer 2022-10-26 15:30:06 -07:00 committed by Tim Abbott
parent 7f9989003a
commit 53d8abe79b
5 changed files with 19 additions and 19 deletions

View File

@ -731,7 +731,7 @@ test_ui("on_events", ({override}) => {
});
test_ui("create_message_object", ({override, override_rewire}) => {
$("#stream_message_recipient_stream").val("social");
compose_state.set_stream_name("social");
$("#stream_message_recipient_topic").val("lunch");
$("#compose-textarea").val("burrito");
@ -744,7 +744,7 @@ test_ui("create_message_object", ({override, override_rewire}) => {
blueslip.expect("error", "Trying to send message with bad stream name: BOGUS STREAM");
$("#stream_message_recipient_stream").val("BOGUS STREAM");
compose_state.set_stream_name("BOGUS STREAM");
message = compose.create_message_object();
assert.equal(message.to, "BOGUS STREAM");
assert.equal(message.topic, "lunch");

View File

@ -131,8 +131,8 @@ test("start", ({override, override_rewire}) => {
assert_visible("#stream-message");
assert_hidden("#private-message");
assert.equal($("#stream_message_recipient_stream").val(), "stream1");
assert.equal($("#stream_message_recipient_topic").val(), "topic1");
assert.equal(compose_state.stream_name(), "stream1");
assert.equal(compose_state.topic(), "topic1");
assert.equal(compose_state.get_message_type(), "stream");
assert.ok(compose_state.composing());
@ -151,8 +151,8 @@ test("start", ({override, override_rewire}) => {
opts = {};
start("stream", opts);
assert.equal($("#stream_message_recipient_stream").val(), "Denmark");
assert.equal($("#stream_message_recipient_topic").val(), "");
assert.equal(compose_state.stream_name(), "Denmark");
assert.equal(compose_state.topic(), "");
compose_defaults = {
trigger: "compose_hotkey",
@ -160,8 +160,8 @@ test("start", ({override, override_rewire}) => {
opts = {};
start("stream", opts);
assert.equal($("#stream_message_recipient_stream").val(), "Denmark");
assert.equal($("#stream_message_recipient_topic").val(), "");
assert.equal(compose_state.stream_name(), "Denmark");
assert.equal(compose_state.topic(), "");
const social = {
subscribed: true,
@ -174,8 +174,8 @@ test("start", ({override, override_rewire}) => {
// More than 1 subscription, do not autofill
opts = {};
start("stream", opts);
assert.equal($("#stream_message_recipient_stream").val(), "");
assert.equal($("#stream_message_recipient_topic").val(), "");
assert.equal(compose_state.stream_name(), "");
assert.equal(compose_state.topic(), "");
stream_data.clear_subscriptions();
// Start PM
@ -268,7 +268,7 @@ test("respond_to_message", ({override, override_rewire}) => {
opts = {};
respond_to_message(opts);
assert.equal($("#stream_message_recipient_stream").val(), "devel");
assert.equal(compose_state.stream_name(), "devel");
});
test("reply_with_mention", ({override, override_rewire}) => {
@ -296,7 +296,7 @@ test("reply_with_mention", ({override, override_rewire}) => {
const opts = {};
reply_with_mention(opts);
assert.equal($("#stream_message_recipient_stream").val(), "devel");
assert.equal(compose_state.stream_name(), "devel");
assert.equal(syntax_to_insert, "@**Bob Roberts**");
// Test for extended mention syntax
@ -314,7 +314,7 @@ test("reply_with_mention", ({override, override_rewire}) => {
people.add_active_user(bob_2);
reply_with_mention(opts);
assert.equal($("#stream_message_recipient_stream").val(), "devel");
assert.equal(compose_state.stream_name(), "devel");
assert.equal(syntax_to_insert, "@**Bob Roberts|40**");
});

View File

@ -735,7 +735,7 @@ test("initialize", ({override, mock_template}) => {
return topics;
};
$("#stream_message_recipient_stream").val("Sweden");
compose_state.set_stream_name("Sweden");
let actual_value = options.source();
// Topics should be sorted alphabetically, not by addition order.
let expected_value = topics;
@ -792,7 +792,7 @@ test("initialize", ({override, mock_template}) => {
topic_typeahead_called = true;
// Unset the stream name.
$("#stream_message_recipient_stream").val("");
compose_state.set_stream_name("");
};
let pm_recipient_typeahead_called = false;

View File

@ -268,8 +268,8 @@ export function update_placeholder_text() {
const opts = {
message_type: compose_state.get_message_type(),
stream: $("#stream_message_recipient_stream").val(),
topic: $("#stream_message_recipient_topic").val(),
stream: compose_state.stream_name(),
topic: compose_state.topic(),
private_message_recipient: compose_pm_pill.get_emails(),
};

View File

@ -27,8 +27,8 @@ export function set_focused_recipient(msg_type) {
};
if (focused_recipient.type === "stream") {
const stream_name = $("#stream_message_recipient_stream").val();
focused_recipient.topic = $("#stream_message_recipient_topic").val();
const stream_name = compose_state.stream_name();
focused_recipient.topic = compose_state.topic();
focused_recipient.stream = stream_name;
const sub = stream_data.get_sub(stream_name);
if (sub) {