Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
settings: Extract stream edit modal functions for reusability.
Rakshit Chauhan1b0b6b55e326
diff --git a/web/src/stream_create.ts b/web/src/stream_create.tsindex 01ad6d314ec..1d3013b85ba 100644--- a/web/src/stream_create.ts+++ b/web/src/stream_create.ts@@ -3,11 +3,9 @@ import assert from "minimalistic-assert";import * as z from "zod/mini";import render_subscription_invites_warning_modal from "../templates/confirm_dialog/confirm_subscription_invites_warning.hbs";-import render_change_stream_info_modal from "../templates/stream_settings/change_stream_info_modal.hbs";import * as channel from "./channel.ts";import * as confirm_dialog from "./confirm_dialog.ts";-import * as dialog_widget from "./dialog_widget.ts";import type {DropdownWidget} from "./dropdown_widget.ts";import {$t, $t_html} from "./i18n.ts";import * as keydown_util from "./keydown_util.ts";@@ -19,6 +17,7 @@ import * as settings_data from "./settings_data.ts";import {current_user, realm} from "./state_data.ts";import * as stream_create_subscribers from "./stream_create_subscribers.ts";import * as stream_data from "./stream_data.ts";+import * as stream_edit from "./stream_edit.ts";import * as stream_settings_components from "./stream_settings_components.ts";import * as stream_settings_data from "./stream_settings_data.ts";import {stream_permission_group_settings_schema} from "./stream_types.ts";@@ -667,53 +666,10 @@ export function initialize(): void {$("#channels_overlay_container").on("click", "#archived_stream_rename", (e) => {e.preventDefault();e.stopPropagation();- const stream_id = Number.parseInt($("#archived_stream_rename").attr("data-stream-id")!, 10);- const stream = stream_data.get_sub_by_id(stream_id);+ const stream_id = Number.parseInt($(e.currentTarget).attr("data-stream-id")!, 10);- assert(stream !== undefined);-- const template_data = {- stream_name: stream.name,- stream_description: stream.description,- max_stream_name_length: realm.max_stream_name_length,- max_stream_description_length: realm.max_stream_description_length,- };- const change_stream_info_modal = render_change_stream_info_modal(template_data);- dialog_widget.launch({- html_heading: $t_html(- {defaultMessage: "Edit #{stream_name} (<i>archived</i>)"},- {stream_name: stream.name},- ),- html_body: change_stream_info_modal,- id: "change_stream_info_modal",- loading_spinner: true,- on_click: save_stream_info,- post_render() {- $("#change_stream_info_modal .dialog_submit_button")- .addClass("save-button")- .attr("data-stream-id", stream_id);- },- update_submit_disabled_state_on_change: true,- });+ stream_edit.open_stream_edit_modal(stream_id);});-- function save_stream_info(): void {- const stream_id = Number.parseInt($("#archived_stream_rename").attr("data-stream-id")!, 10);- const sub = stream_data.get_sub_by_id(stream_id);- const url = `/json/streams/${sub?.stream_id}`;- const data: {new_name?: string; description?: string} = {};- const new_name = $<HTMLInputElement>("#change_stream_name").val()!.trim();- const new_description = $<HTMLInputElement>("#change_stream_description").val()!.trim();-- if (new_name !== sub?.name) {- data.new_name = new_name;- }- if (new_description !== sub?.description) {- data.description = new_description;- }-- dialog_widget.submit_api_request(channel.patch, url, data);- }}export function set_channel_folder_dropdown_value(folder_id: number): void {diff --git a/web/src/stream_edit.ts b/web/src/stream_edit.tsindex eb0a5a1d3d9..6b16f85e7c4 100644--- a/web/src/stream_edit.ts+++ b/web/src/stream_edit.ts@@ -99,6 +99,60 @@ export function setup_subscriptions_tab_hash(tab_key_value: string): void {}}+export function open_stream_edit_modal(stream_id: number): void {+ const stream = sub_store.get(stream_id);+ assert(stream !== undefined);++ const is_archived = stream.is_archived;++ const template_data = {+ stream_name: stream.name,+ stream_description: stream.description,+ max_stream_name_length: realm.max_stream_name_length,+ max_stream_description_length: realm.max_stream_description_length,+ };+ const change_stream_info_modal = render_change_stream_info_modal(template_data);++ const heading = is_archived+ ? $t_html(+ {defaultMessage: "Edit #{channel_name} (<i>archived</i>)"},+ {channel_name: stream.name},+ )+ : $t_html({defaultMessage: "Edit #{channel_name}"}, {channel_name: stream.name});+ dialog_widget.launch({+ html_heading: heading,+ html_body: change_stream_info_modal,+ id: "change_stream_info_modal",+ loading_spinner: true,+ on_click: save_stream_info,+ post_render() {+ $("#change_stream_info_modal .dialog_submit_button")+ .addClass("save-button")+ .attr("data-stream-id", stream_id);+ },+ update_submit_disabled_state_on_change: true,+ });+}++export function save_stream_info(): void {+ const sub = get_sub_for_target(util.the($("#change_stream_info_modal .dialog_submit_button")));+ const url = `/json/streams/${sub.stream_id}`;+ const data: {new_name?: string; description?: string} = {};+ const new_name = $<HTMLInputElement>("input#change_stream_name").val()!.trim();+ const new_description = $<HTMLTextAreaElement>("textarea#change_stream_description")+ .val()!+ .trim();++ if (new_name !== sub.name) {+ data.new_name = new_name;+ }+ if (new_description !== sub.description) {+ data.description = new_description;+ }++ dialog_widget.submit_api_request(channel.patch, url, data);+}+export function get_display_text_for_realm_message_retention_setting(): string {const realm_message_retention_days = realm.realm_message_retention_days;if (realm_message_retention_days === settings_config.retain_message_forever) {@@ -532,36 +586,7 @@ export function initialize(): void {e.preventDefault();e.stopPropagation();const stream_id = get_stream_id(this);- const stream = sub_store.get(stream_id);- assert(stream !== undefined);- const template_data = {- stream_name: stream.name,- stream_description: stream.description,- max_stream_name_length: realm.max_stream_name_length,- max_stream_description_length: realm.max_stream_description_length,- };- const change_stream_info_modal = render_change_stream_info_modal(template_data);-- const heading = stream.is_archived- ? $t_html(- {defaultMessage: "Edit #{channel_name} (<i>archived</i>)"},- {channel_name: stream.name},- )- : $t_html({defaultMessage: "Edit #{channel_name}"}, {channel_name: stream.name});-- dialog_widget.launch({- html_heading: heading,- html_body: change_stream_info_modal,- id: "change_stream_info_modal",- loading_spinner: true,- on_click: save_stream_info,- post_render() {- $("#change_stream_info_modal .dialog_submit_button")- .addClass("save-button")- .attr("data-stream-id", stream_id);- },- update_submit_disabled_state_on_change: true,- });+ open_stream_edit_modal(stream_id);},);@@ -605,27 +630,6 @@ export function initialize(): void {},);- function save_stream_info(): void {- const sub = get_sub_for_target(- util.the($("#change_stream_info_modal .dialog_submit_button")),- );- const url = `/json/streams/${sub.stream_id}`;- const data: {new_name?: string; description?: string} = {};- const new_name = $<HTMLInputElement>("input#change_stream_name").val()!.trim();- const new_description = $<HTMLTextAreaElement>("textarea#change_stream_description")- .val()!- .trim();-- if (new_name !== sub.name) {- data.new_name = new_name;- }- if (new_description !== sub.description) {- data.description = new_description;- }-- dialog_widget.submit_api_request(channel.patch, url, data);- }-$("#channels_overlay_container").on("click",".copy_email_button",