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:
Lalit 2023-04-06 21:56:31 +05:30 committed by Tim Abbott
parent cfa92aa60b
commit ccbba1acb0
3 changed files with 18 additions and 25 deletions

View File

@ -112,9 +112,6 @@ function call(args) {
return $.ajax(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) { export function get(options) {
const args = {type: "GET", dataType: "json", ...options}; const args = {type: "GET", dataType: "json", ...options};
return call(args); return call(args);

View File

@ -5,6 +5,7 @@ import render_dialog_widget from "../templates/dialog_widget.hbs";
import {$t_html} from "./i18n"; import {$t_html} from "./i18n";
import * as loading from "./loading"; import * as loading from "./loading";
import * as overlays from "./overlays"; import * as overlays from "./overlays";
import type {AjaxRequestHandler} from "./types";
import * as ui_report from "./ui_report"; import * as ui_report from "./ui_report";
/* /*
@ -61,24 +62,10 @@ type WidgetConfig = {
loading_spinner?: boolean; 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 = { type RequestOpts = {
failure_msg_html?: string; failure_msg_html?: string;
success_continuation?: (response_data?: string) => void; success_continuation?: Parameters<AjaxRequestHandler>[0]["success"];
error_continuation?: (xhr?: JQuery.jqXHR) => void; error_continuation?: Parameters<AjaxRequestHandler>[0]["error"];
}; };
export function hide_dialog_spinner(): void { export function hide_dialog_spinner(): void {
@ -190,9 +177,9 @@ export function launch(conf: WidgetConfig): void {
} }
export function submit_api_request( export function submit_api_request(
request_method: AjaxRequest, request_method: AjaxRequestHandler,
url: string, url: string,
data = {}, data: Parameters<AjaxRequestHandler>[0]["data"] = {},
{ {
failure_msg_html = $t_html({defaultMessage: "Failed"}), failure_msg_html = $t_html({defaultMessage: "Failed"}),
success_continuation, success_continuation,
@ -203,17 +190,17 @@ export function submit_api_request(
request_method({ request_method({
url, url,
data, data,
success(response_data?: string) { success(response_data, textStatus, jqXHR) {
close_modal(); close_modal();
if (success_continuation !== undefined) { 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")); ui_report.error(failure_msg_html, xhr, $("#dialog_error"));
hide_dialog_spinner(); hide_dialog_spinner();
if (error_continuation !== undefined) { if (error_continuation !== undefined) {
error_continuation(xhr); error_continuation(xhr, error_type, xhn);
} }
}, },
}); });

View File

@ -69,3 +69,12 @@ export type UpdateMessageEvent = {
// TODO/typescript: Move the User and Stream placeholder // TODO/typescript: Move the User and Stream placeholder
// types to their appropriate modules. // types to their appropriate modules.
export type User = Record<string, never>; export type User = Record<string, never>;
// TODO/typescript: Move this to channel
export type AjaxRequestHandler = (args: {
url: string;
data?: Record<string, unknown> | 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;