From dab49bec4a8584b7dea429fc5dd1da12ccf97027 Mon Sep 17 00:00:00 2001 From: Thorsten Rinne Date: Wed, 22 Jan 2025 07:10:46 +0100 Subject: [PATCH] refactor: code cleanup --- .../admin/assets/src/api/configuration.ts | 34 +++++++++++++++++++ phpmyfaq/admin/assets/src/api/index.ts | 1 + .../assets/src/configuration/configuration.ts | 21 ++++-------- .../admin/assets/src/interfaces/response.ts | 1 + 4 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 phpmyfaq/admin/assets/src/api/configuration.ts diff --git a/phpmyfaq/admin/assets/src/api/configuration.ts b/phpmyfaq/admin/assets/src/api/configuration.ts new file mode 100644 index 0000000000..b6a82e6d7a --- /dev/null +++ b/phpmyfaq/admin/assets/src/api/configuration.ts @@ -0,0 +1,34 @@ +/** + * Fetch data for configuration + * + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at https://mozilla.org/MPL/2.0/. + * + * @package phpMyFAQ + * @author Thorsten Rinne + * @copyright 2025 phpMyFAQ Team + * @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 + * @link https://www.phpmyfaq.de + * @since 2025-01-21 + */ + +import { Response } from '../interfaces'; + +export const saveConfiguration = async (data: FormData): Promise => { + try { + const response = (await fetch('api/configuration', { + method: 'POST', + body: data, + })) as unknown as Response; + + if (response.success) { + return await response.json(); + } else { + throw new Error('Network response was not ok.'); + } + } catch (error) { + console.error('Error updating configuration: ', error); + throw error; + } +}; diff --git a/phpmyfaq/admin/assets/src/api/index.ts b/phpmyfaq/admin/assets/src/api/index.ts index 2c653a6b57..61bf04acd2 100644 --- a/phpmyfaq/admin/assets/src/api/index.ts +++ b/phpmyfaq/admin/assets/src/api/index.ts @@ -1,5 +1,6 @@ export * from './attachment'; export * from './category'; +export * from './configuration'; export * from './faqs'; export * from './forms'; export * from './glossary'; diff --git a/phpmyfaq/admin/assets/src/configuration/configuration.ts b/phpmyfaq/admin/assets/src/configuration/configuration.ts index 3add4f7dee..6490b5b49d 100644 --- a/phpmyfaq/admin/assets/src/configuration/configuration.ts +++ b/phpmyfaq/admin/assets/src/configuration/configuration.ts @@ -15,11 +15,12 @@ import { Tab } from 'bootstrap'; import { pushErrorNotification, pushNotification } from '../../../../assets/src/utils'; +import { saveConfiguration } from '../api'; +import { Response } from '../interfaces'; export const handleConfiguration = async (): Promise => { const configTabList: HTMLElement[] = [].slice.call(document.querySelectorAll('#configuration-list a')); const result = document.getElementById('pmf-configuration-result') as HTMLElement; - const language = document.getElementById('pmf-language') as HTMLInputElement; if (configTabList.length) { let tabLoaded = false; configTabList.forEach((element) => { @@ -80,22 +81,12 @@ export const handleSaveConfiguration = async (): Promise => { const form = document.getElementById('configuration-list') as HTMLFormElement; const formData = new FormData(form); - const response = await fetch('./api/configuration', { - method: 'POST', - body: formData, - }); - - if (!response.ok) { - console.error('Request failed!'); - return; - } - - const json = await response.json(); + const response = (await saveConfiguration(formData)) as unknown as Response; - if (json.success) { - pushNotification(json.success); + if (typeof response.success === 'string') { + pushNotification(response.success); } else { - pushErrorNotification(json.error); + pushErrorNotification(response.error as string); } }); } diff --git a/phpmyfaq/admin/assets/src/interfaces/response.ts b/phpmyfaq/admin/assets/src/interfaces/response.ts index e1bc121ea3..4f005ccc61 100644 --- a/phpmyfaq/admin/assets/src/interfaces/response.ts +++ b/phpmyfaq/admin/assets/src/interfaces/response.ts @@ -1,4 +1,5 @@ export interface Response { + json(): void | PromiseLike; success: boolean; message?: string; error?: string;