From 17be7d5a548ce703ab8e5f51ec8ac4d33cb223a1 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tim=20D=C3=BCsterhus?= Date: Thu, 11 Mar 2021 14:38:17 +0100 Subject: [PATCH] Improve typing in Form/Builder/Dialog.ts see 155f9dd41571c72b79815783efc0924ff5279ad8 --- ts/WoltLabSuite/Core/Form/Builder/Data.ts | 5 ---- ts/WoltLabSuite/Core/Form/Builder/Dialog.ts | 29 ++++++++++++++----- .../WoltLabSuite/Core/Form/Builder/Dialog.js | 20 ++++++++----- 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/ts/WoltLabSuite/Core/Form/Builder/Data.ts b/ts/WoltLabSuite/Core/Form/Builder/Data.ts index 20a39a7bea..b87b1c5ba0 100644 --- a/ts/WoltLabSuite/Core/Form/Builder/Data.ts +++ b/ts/WoltLabSuite/Core/Form/Builder/Data.ts @@ -5,11 +5,6 @@ interface InternalFormBuilderData { [key: string]: any; } -export interface AjaxResponseReturnValues { - dialog: string; - formId: string; -} - export type FormBuilderData = InternalFormBuilderData | Promise; export interface FormBuilderDialogOptions { diff --git a/ts/WoltLabSuite/Core/Form/Builder/Dialog.ts b/ts/WoltLabSuite/Core/Form/Builder/Dialog.ts index 17b88324b4..0b607c9182 100644 --- a/ts/WoltLabSuite/Core/Form/Builder/Dialog.ts +++ b/ts/WoltLabSuite/Core/Form/Builder/Dialog.ts @@ -14,10 +14,27 @@ import { DialogCallbackObject, DialogCallbackSetup, DialogData } from "../../Ui/ import * as Ajax from "../../Ajax"; import { AjaxCallbackObject, AjaxCallbackSetup, DatabaseObjectActionResponse, RequestOptions } from "../../Ajax/Data"; import * as FormBuilderManager from "./Manager"; -import { AjaxResponseReturnValues, FormBuilderData, FormBuilderDialogOptions } from "./Data"; +import { FormBuilderData, FormBuilderDialogOptions } from "./Data"; + +interface DialogResponse { + dialog: string; + formId: string; +} + +function isDialogResponse(val: any): val is DialogResponse { + return val.dialog !== undefined && val.formId !== undefined; +} + +function assertDialogResponse(val: any): asserts val is DialogResponse { + if (val.dialog === undefined) { + throw new Error("Missing dialog template in return data."); + } else if (val.formId === undefined) { + throw new Error("Missing form id in return data."); + } +} interface AjaxResponse extends DatabaseObjectActionResponse { - returnValues: AjaxResponseReturnValues; + returnValues: DialogResponse | DatabaseObjectActionResponse["returnValues"]; } class FormBuilderDialog implements AjaxCallbackObject, DialogCallbackObject { @@ -82,19 +99,17 @@ class FormBuilderDialog implements AjaxCallbackObject, DialogCallbackObject { case this._actionName: if (data.returnValues === undefined) { throw new Error("Missing return data."); - } else if (data.returnValues.dialog === undefined) { - throw new Error("Missing dialog template in return data."); - } else if (data.returnValues.formId === undefined) { - throw new Error("Missing form id in return data."); } + assertDialogResponse(data.returnValues); + this._openDialogContent(data.returnValues.formId, data.returnValues.dialog); break; case this._options.submitActionName: // If the validation failed, the dialog is shown again. - if (data.returnValues && data.returnValues.formId && data.returnValues.dialog) { + if (data.returnValues && isDialogResponse(data.returnValues)) { if (data.returnValues.formId !== this._formId) { throw new Error( "Mismatch between form ids: expected '" + this._formId + "' but got '" + data.returnValues.formId + "'.", diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Dialog.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Dialog.js index c91d29e30e..92d372dea3 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Dialog.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Form/Builder/Dialog.js @@ -13,6 +13,17 @@ define(["require", "exports", "tslib", "../../Core", "../../Ui/Dialog", "../../A Dialog_1 = tslib_1.__importDefault(Dialog_1); Ajax = tslib_1.__importStar(Ajax); FormBuilderManager = tslib_1.__importStar(FormBuilderManager); + function isDialogResponse(val) { + return val.dialog !== undefined && val.formId !== undefined; + } + function assertDialogResponse(val) { + if (val.dialog === undefined) { + throw new Error("Missing dialog template in return data."); + } + else if (val.formId === undefined) { + throw new Error("Missing form id in return data."); + } + } class FormBuilderDialog { constructor(dialogId, className, actionName, options) { this.init(dialogId, className, actionName, options); @@ -54,17 +65,12 @@ define(["require", "exports", "tslib", "../../Core", "../../Ui/Dialog", "../../A if (data.returnValues === undefined) { throw new Error("Missing return data."); } - else if (data.returnValues.dialog === undefined) { - throw new Error("Missing dialog template in return data."); - } - else if (data.returnValues.formId === undefined) { - throw new Error("Missing form id in return data."); - } + assertDialogResponse(data.returnValues); this._openDialogContent(data.returnValues.formId, data.returnValues.dialog); break; case this._options.submitActionName: // If the validation failed, the dialog is shown again. - if (data.returnValues && data.returnValues.formId && data.returnValues.dialog) { + if (data.returnValues && isDialogResponse(data.returnValues)) { if (data.returnValues.formId !== this._formId) { throw new Error("Mismatch between form ids: expected '" + this._formId + "' but got '" + data.returnValues.formId + "'."); } -- 2.20.1