diff --git a/web/src/channel.js b/web/src/channel.js index f39e6923e7..1abd8310ca 100644 --- a/web/src/channel.js +++ b/web/src/channel.js @@ -112,9 +112,6 @@ function call(args) { return $.ajax(args); } -// TODO: When this file is converted to TypeScript, deduplicate the -// AjaxRequest type defined in dialog_widget.js. - export function get(options) { const args = {type: "GET", dataType: "json", ...options}; return call(args); diff --git a/web/src/dialog_widget.ts b/web/src/dialog_widget.ts index ed64168561..ec708b30bc 100644 --- a/web/src/dialog_widget.ts +++ b/web/src/dialog_widget.ts @@ -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; - 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[0]["success"]; + error_continuation?: Parameters[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[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); } }, }); diff --git a/web/src/types.ts b/web/src/types.ts index dc14653c3a..78122d1db2 100644 --- a/web/src/types.ts +++ b/web/src/types.ts @@ -69,3 +69,12 @@ export type UpdateMessageEvent = { // TODO/typescript: Move the User and Stream placeholder // types to their appropriate modules. export type User = Record; + +// TODO/typescript: Move this to channel +export type AjaxRequestHandler = (args: { + url: string; + data?: Record | string | unknown[]; + ignoreReload?: boolean; + success?(response_data: unknown, textStatus: string, jqXHR: JQuery.jqXHR): void; + error?(xhr: JQuery.jqXHR, error_type: string, xhn: string): void; +}) => void;