Skip to content

Commit dab49be

Browse files
committed
refactor: code cleanup
1 parent 9278536 commit dab49be

File tree

4 files changed

+42
-15
lines changed

4 files changed

+42
-15
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Fetch data for configuration
3+
*
4+
* This Source Code Form is subject to the terms of the Mozilla Public License,
5+
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
6+
* obtain one at https://mozilla.org/MPL/2.0/.
7+
*
8+
* @package phpMyFAQ
9+
* @author Thorsten Rinne <[email protected]>
10+
* @copyright 2025 phpMyFAQ Team
11+
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
12+
* @link https://www.phpmyfaq.de
13+
* @since 2025-01-21
14+
*/
15+
16+
import { Response } from '../interfaces';
17+
18+
export const saveConfiguration = async (data: FormData): Promise<void> => {
19+
try {
20+
const response = (await fetch('api/configuration', {
21+
method: 'POST',
22+
body: data,
23+
})) as unknown as Response;
24+
25+
if (response.success) {
26+
return await response.json();
27+
} else {
28+
throw new Error('Network response was not ok.');
29+
}
30+
} catch (error) {
31+
console.error('Error updating configuration: ', error);
32+
throw error;
33+
}
34+
};

phpmyfaq/admin/assets/src/api/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from './attachment';
22
export * from './category';
3+
export * from './configuration';
34
export * from './faqs';
45
export * from './forms';
56
export * from './glossary';

phpmyfaq/admin/assets/src/configuration/configuration.ts

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@
1515

1616
import { Tab } from 'bootstrap';
1717
import { pushErrorNotification, pushNotification } from '../../../../assets/src/utils';
18+
import { saveConfiguration } from '../api';
19+
import { Response } from '../interfaces';
1820

1921
export const handleConfiguration = async (): Promise<void> => {
2022
const configTabList: HTMLElement[] = [].slice.call(document.querySelectorAll('#configuration-list a'));
2123
const result = document.getElementById('pmf-configuration-result') as HTMLElement;
22-
const language = document.getElementById('pmf-language') as HTMLInputElement;
2324
if (configTabList.length) {
2425
let tabLoaded = false;
2526
configTabList.forEach((element) => {
@@ -80,22 +81,12 @@ export const handleSaveConfiguration = async (): Promise<void> => {
8081
const form = document.getElementById('configuration-list') as HTMLFormElement;
8182
const formData = new FormData(form);
8283

83-
const response = await fetch('./api/configuration', {
84-
method: 'POST',
85-
body: formData,
86-
});
87-
88-
if (!response.ok) {
89-
console.error('Request failed!');
90-
return;
91-
}
92-
93-
const json = await response.json();
84+
const response = (await saveConfiguration(formData)) as unknown as Response;
9485

95-
if (json.success) {
96-
pushNotification(json.success);
86+
if (typeof response.success === 'string') {
87+
pushNotification(response.success);
9788
} else {
98-
pushErrorNotification(json.error);
89+
pushErrorNotification(response.error as string);
9990
}
10091
});
10192
}

phpmyfaq/admin/assets/src/interfaces/response.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export interface Response {
2+
json(): void | PromiseLike<void>;
23
success: boolean;
34
message?: string;
45
error?: string;

0 commit comments

Comments
 (0)