mirror of
https://github.com/zulip/zulip.git
synced 2025-11-12 18:06:44 +00:00
dialog_widget: Refactor AjaxRequest type and moved it to types.ts.
Changes `AjaxRequest` name to more clear one `AjaxRequestHandler`, also moved this type to `types.ts` with a comment to move it into `channel.js` once it migrates to typescript.
This commit is contained in:
@@ -5,6 +5,7 @@ import render_dialog_widget from "../templates/dialog_widget.hbs";
|
||||
import {$t_html} from "./i18n";
|
||||
import * as loading from "./loading";
|
||||
import * as overlays from "./overlays";
|
||||
import type {AjaxRequestHandler} from "./types";
|
||||
import * as ui_report from "./ui_report";
|
||||
|
||||
/*
|
||||
@@ -61,24 +62,10 @@ type WidgetConfig = {
|
||||
loading_spinner?: boolean;
|
||||
};
|
||||
|
||||
// TODO: This type should probably be exported from channel.ts once
|
||||
// that's converted to TypeScript.
|
||||
type AjaxRequest = ({
|
||||
url,
|
||||
data = {},
|
||||
success,
|
||||
error,
|
||||
}: {
|
||||
url: string;
|
||||
data?: Record<string, never>;
|
||||
success(response_data?: string): void;
|
||||
error(xhr?: JQuery.jqXHR): void;
|
||||
}) => void;
|
||||
|
||||
type RequestOpts = {
|
||||
failure_msg_html?: string;
|
||||
success_continuation?: (response_data?: string) => void;
|
||||
error_continuation?: (xhr?: JQuery.jqXHR) => void;
|
||||
success_continuation?: Parameters<AjaxRequestHandler>[0]["success"];
|
||||
error_continuation?: Parameters<AjaxRequestHandler>[0]["error"];
|
||||
};
|
||||
|
||||
export function hide_dialog_spinner(): void {
|
||||
@@ -190,9 +177,9 @@ export function launch(conf: WidgetConfig): void {
|
||||
}
|
||||
|
||||
export function submit_api_request(
|
||||
request_method: AjaxRequest,
|
||||
request_method: AjaxRequestHandler,
|
||||
url: string,
|
||||
data = {},
|
||||
data: Parameters<AjaxRequestHandler>[0]["data"] = {},
|
||||
{
|
||||
failure_msg_html = $t_html({defaultMessage: "Failed"}),
|
||||
success_continuation,
|
||||
@@ -203,17 +190,17 @@ export function submit_api_request(
|
||||
request_method({
|
||||
url,
|
||||
data,
|
||||
success(response_data?: string) {
|
||||
success(response_data, textStatus, jqXHR) {
|
||||
close_modal();
|
||||
if (success_continuation !== undefined) {
|
||||
success_continuation(response_data);
|
||||
success_continuation(response_data, textStatus, jqXHR);
|
||||
}
|
||||
},
|
||||
error(xhr?: JQuery.jqXHR) {
|
||||
error(xhr, error_type, xhn) {
|
||||
ui_report.error(failure_msg_html, xhr, $("#dialog_error"));
|
||||
hide_dialog_spinner();
|
||||
if (error_continuation !== undefined) {
|
||||
error_continuation(xhr);
|
||||
error_continuation(xhr, error_type, xhn);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user