Skip to content

Commit

Permalink
refactor: code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Jan 22, 2025
1 parent 9278536 commit dab49be
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 15 deletions.
34 changes: 34 additions & 0 deletions phpmyfaq/admin/assets/src/api/configuration.ts
Original file line number Diff line number Diff line change
@@ -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 <thorsten@phpmyfaq.de>
* @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<void> => {
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;
}
};
1 change: 1 addition & 0 deletions phpmyfaq/admin/assets/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './attachment';
export * from './category';
export * from './configuration';
export * from './faqs';
export * from './forms';
export * from './glossary';
Expand Down
21 changes: 6 additions & 15 deletions phpmyfaq/admin/assets/src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> => {
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) => {
Expand Down Expand Up @@ -80,22 +81,12 @@ export const handleSaveConfiguration = async (): Promise<void> => {
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);
}
});
}
Expand Down
1 change: 1 addition & 0 deletions phpmyfaq/admin/assets/src/interfaces/response.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export interface Response {
json(): void | PromiseLike<void>;
success: boolean;
message?: string;
error?: string;
Expand Down

0 comments on commit dab49be

Please sign in to comment.