mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	static/js/invite.js: Extract func submit_invitation_form.
				
					
				
			Extract function `submit_invitation_form` and relocate some functions to make it easy to review diff.
This commit is contained in:
		
				
					committed by
					
						
						Tim Abbott
					
				
			
			
				
	
			
			
			
						parent
						
							37acb3e2cb
						
					
				
				
					commit
					f4067bb38b
				
			@@ -2,6 +2,95 @@ var invite = (function () {
 | 
			
		||||
 | 
			
		||||
var exports = {};
 | 
			
		||||
 | 
			
		||||
function reset_error_messages() {
 | 
			
		||||
    var invite_status = $('#invite_status');
 | 
			
		||||
    var invitee_emails = $("#invitee_emails");
 | 
			
		||||
    var invitee_emails_group = invitee_emails.closest('.control-group');
 | 
			
		||||
 | 
			
		||||
    invite_status.hide().text('').removeClass('alert-error alert-warning alert-success');
 | 
			
		||||
    invitee_emails_group.removeClass('warning error');
 | 
			
		||||
    if (page_params.development_environment) {
 | 
			
		||||
        $('#dev_env_msg').hide().text('').removeClass('alert-error alert-warning alert-success');
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function submit_invitation_form() {
 | 
			
		||||
    var invite_status = $('#invite_status');
 | 
			
		||||
    var invitee_emails = $("#invitee_emails");
 | 
			
		||||
    var invitee_emails_group = invitee_emails.closest('.control-group');
 | 
			
		||||
    var invite_as = $('#invite_as').val();
 | 
			
		||||
    var data = {
 | 
			
		||||
        invitee_emails: $("#invitee_emails").val(),
 | 
			
		||||
        invite_as_admin: invite_as === 'admin',
 | 
			
		||||
        csrfmiddlewaretoken: $('input[name="csrfmiddlewaretoken"]').attr('value'),
 | 
			
		||||
    };
 | 
			
		||||
    var streams = [];
 | 
			
		||||
    $.each($("#invite-stream-checkboxes input:checked"), function () {
 | 
			
		||||
        streams.push($(this).val());
 | 
			
		||||
    });
 | 
			
		||||
    data.stream = streams;
 | 
			
		||||
 | 
			
		||||
    channel.post({
 | 
			
		||||
        url: "/json/invites",
 | 
			
		||||
        data: data,
 | 
			
		||||
        traditional: true,
 | 
			
		||||
        beforeSubmit: function () {
 | 
			
		||||
            reset_error_messages();
 | 
			
		||||
            // TODO: You could alternatively parse the textarea here, and return errors to
 | 
			
		||||
            // the user if they don't match certain constraints (i.e. not real email addresses,
 | 
			
		||||
            // aren't in the right domain, etc.)
 | 
			
		||||
            //
 | 
			
		||||
            // OR, you could just let the server do it. Probably my temptation.
 | 
			
		||||
            $('#submit-invitation').button('loading');
 | 
			
		||||
            return true;
 | 
			
		||||
        },
 | 
			
		||||
        success: function () {
 | 
			
		||||
            $('#submit-invitation').button('reset');
 | 
			
		||||
            invite_status.text(i18n.t('User(s) invited successfully.'))
 | 
			
		||||
                .addClass('alert-success')
 | 
			
		||||
                .show();
 | 
			
		||||
            invitee_emails.val('');
 | 
			
		||||
 | 
			
		||||
            if (page_params.development_environment) {
 | 
			
		||||
                var rendered_email_msg = templates.render('dev_env_email_access');
 | 
			
		||||
                $('#dev_env_msg').html(rendered_email_msg).addClass('alert-info').show();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        },
 | 
			
		||||
        error: function (xhr) {
 | 
			
		||||
            $('#submit-invitation').button('reset');
 | 
			
		||||
            var arr = JSON.parse(xhr.responseText);
 | 
			
		||||
            if (arr.errors === undefined) {
 | 
			
		||||
                // There was a fatal error, no partial processing occurred.
 | 
			
		||||
                invite_status.text(arr.msg)
 | 
			
		||||
                    .addClass('alert-error')
 | 
			
		||||
                    .show();
 | 
			
		||||
            } else {
 | 
			
		||||
                // Some users were not invited.
 | 
			
		||||
                var invitee_emails_errored = [];
 | 
			
		||||
                var error_list = $('<ul>');
 | 
			
		||||
                _.each(arr.errors, function (value) {
 | 
			
		||||
                    error_list.append($('<li>').text(value.join(': ')));
 | 
			
		||||
                    invitee_emails_errored.push(value[0]);
 | 
			
		||||
                });
 | 
			
		||||
 | 
			
		||||
                invite_status.addClass('alert-warning')
 | 
			
		||||
                    .empty()
 | 
			
		||||
                    .append($('<p>').text(arr.msg))
 | 
			
		||||
                    .append(error_list)
 | 
			
		||||
                    .show();
 | 
			
		||||
                invitee_emails_group.addClass('warning');
 | 
			
		||||
 | 
			
		||||
                if (arr.sent_invitations) {
 | 
			
		||||
                    invitee_emails.val(invitee_emails_errored.join('\n'));
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        },
 | 
			
		||||
    });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// `get_invite_streams` is further modification of stream_data.invite_streams(), it is
 | 
			
		||||
// defined here to keep stream_data.invite_stream() generic.
 | 
			
		||||
exports.get_invite_streams = function () {
 | 
			
		||||
@@ -40,18 +129,6 @@ function update_subscription_checkboxes() {
 | 
			
		||||
    $('#streams_to_add').html(html);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function reset_error_messages() {
 | 
			
		||||
    var invite_status = $('#invite_status');
 | 
			
		||||
    var invitee_emails = $("#invitee_emails");
 | 
			
		||||
    var invitee_emails_group = invitee_emails.closest('.control-group');
 | 
			
		||||
 | 
			
		||||
    invite_status.hide().text('').removeClass('alert-error alert-warning alert-success');
 | 
			
		||||
    invitee_emails_group.removeClass('warning error');
 | 
			
		||||
    if (page_params.development_environment) {
 | 
			
		||||
        $('#dev_env_msg').hide().text('').removeClass('alert-error alert-warning alert-success');
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
function prepare_form_to_be_shown() {
 | 
			
		||||
    update_subscription_checkboxes();
 | 
			
		||||
    reset_error_messages();
 | 
			
		||||
@@ -59,87 +136,10 @@ function prepare_form_to_be_shown() {
 | 
			
		||||
 | 
			
		||||
exports.launch = function () {
 | 
			
		||||
    ui.set_up_scrollbar($("#invite_user_form .modal-body"));
 | 
			
		||||
    var invite_status = $('#invite_status');
 | 
			
		||||
    var invitee_emails = $("#invitee_emails");
 | 
			
		||||
    var invitee_emails_group = invitee_emails.closest('.control-group');
 | 
			
		||||
 | 
			
		||||
    $('#submit-invitation').button();
 | 
			
		||||
    prepare_form_to_be_shown();
 | 
			
		||||
    invitee_emails.focus().autosize();
 | 
			
		||||
 | 
			
		||||
    $("#submit-invitation").on("click", function () {
 | 
			
		||||
        var invite_as = $('#invite_as').val();
 | 
			
		||||
        var data = {
 | 
			
		||||
            invitee_emails: $("#invitee_emails").val(),
 | 
			
		||||
            invite_as_admin: invite_as === 'admin',
 | 
			
		||||
            csrfmiddlewaretoken: $('input[name="csrfmiddlewaretoken"]').attr('value'),
 | 
			
		||||
        };
 | 
			
		||||
        var streams = [];
 | 
			
		||||
        $.each($("#invite-stream-checkboxes input:checked"), function () {
 | 
			
		||||
            streams.push($(this).val());
 | 
			
		||||
        });
 | 
			
		||||
        data.stream = streams;
 | 
			
		||||
 | 
			
		||||
        channel.post({
 | 
			
		||||
            url: "/json/invites",
 | 
			
		||||
            data: data,
 | 
			
		||||
            traditional: true,
 | 
			
		||||
            beforeSubmit: function () {
 | 
			
		||||
                reset_error_messages();
 | 
			
		||||
                // TODO: You could alternatively parse the textarea here, and return errors to
 | 
			
		||||
                // the user if they don't match certain constraints (i.e. not real email addresses,
 | 
			
		||||
                // aren't in the right domain, etc.)
 | 
			
		||||
                //
 | 
			
		||||
                // OR, you could just let the server do it. Probably my temptation.
 | 
			
		||||
                $('#submit-invitation').button('loading');
 | 
			
		||||
                return true;
 | 
			
		||||
            },
 | 
			
		||||
            success: function () {
 | 
			
		||||
                $('#submit-invitation').button('reset');
 | 
			
		||||
                invite_status.text(i18n.t('User(s) invited successfully.'))
 | 
			
		||||
                    .addClass('alert-success')
 | 
			
		||||
                    .show();
 | 
			
		||||
                invitee_emails.val('');
 | 
			
		||||
 | 
			
		||||
                if (page_params.development_environment) {
 | 
			
		||||
                    var rendered_email_msg = templates.render('dev_env_email_access');
 | 
			
		||||
                    $('#dev_env_msg').html(rendered_email_msg).addClass('alert-info').show();
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
            },
 | 
			
		||||
            error: function (xhr) {
 | 
			
		||||
                $('#submit-invitation').button('reset');
 | 
			
		||||
                var arr = JSON.parse(xhr.responseText);
 | 
			
		||||
                if (arr.errors === undefined) {
 | 
			
		||||
                    // There was a fatal error, no partial processing occurred.
 | 
			
		||||
                    invite_status.text(arr.msg)
 | 
			
		||||
                        .addClass('alert-error')
 | 
			
		||||
                        .show();
 | 
			
		||||
                } else {
 | 
			
		||||
                    // Some users were not invited.
 | 
			
		||||
                    var invitee_emails_errored = [];
 | 
			
		||||
                    var error_list = $('<ul>');
 | 
			
		||||
                    _.each(arr.errors, function (value) {
 | 
			
		||||
                        error_list.append($('<li>').text(value.join(': ')));
 | 
			
		||||
                        invitee_emails_errored.push(value[0]);
 | 
			
		||||
                    });
 | 
			
		||||
 | 
			
		||||
                    invite_status.addClass('alert-warning')
 | 
			
		||||
                        .empty()
 | 
			
		||||
                        .append($('<p>').text(arr.msg))
 | 
			
		||||
                        .append(error_list)
 | 
			
		||||
                        .show();
 | 
			
		||||
                    invitee_emails_group.addClass('warning');
 | 
			
		||||
 | 
			
		||||
                    if (arr.sent_invitations) {
 | 
			
		||||
                        invitee_emails.val(invitee_emails_errored.join('\n'));
 | 
			
		||||
                    }
 | 
			
		||||
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
            },
 | 
			
		||||
        });
 | 
			
		||||
    });
 | 
			
		||||
    $("#invitee_emails").focus().autosize();
 | 
			
		||||
 | 
			
		||||
    overlays.open_overlay({
 | 
			
		||||
        name: 'invite',
 | 
			
		||||
@@ -148,6 +148,7 @@ exports.launch = function () {
 | 
			
		||||
            hashchange.exit_overlay();
 | 
			
		||||
        },
 | 
			
		||||
    });
 | 
			
		||||
    $("#submit-invitation").on("click", submit_invitation_form);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
exports.initialize = function () {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user