Skip to content

Commit

Permalink
refactor: moved code from PHP files into controller (#2791)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Apr 30, 2024
1 parent 11774fb commit ab6f271
Show file tree
Hide file tree
Showing 21 changed files with 508 additions and 557 deletions.
38 changes: 38 additions & 0 deletions phpmyfaq/admin/assets/src/api/faqs.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,41 @@ export const deleteFaq = async (faqId, faqLanguage, token) => {
throw error;
}
};

export const create = async (formData) => {
try {
const response = await fetch('./api/faq/create', {
method: 'POST',
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
},
body: JSON.stringify({
data: formData,
}),
});

return await response.json();
} catch (error) {
console.error(error);
}
};

export const update = async (formData) => {
try {
const response = await fetch('./api/faq/update', {
method: 'PUT',
headers: {
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/json',
},
body: JSON.stringify({
data: formData,
}),
});

return await response.json();
} catch (error) {
console.error(error);
}
};
5 changes: 5 additions & 0 deletions phpmyfaq/admin/assets/src/content/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ export const renderEditor = () => {
image_dimensions: true,
images_upload_url: '/admin/api/content/images',
automatic_uploads: true,
setup: (editor) => {
editor.on('change', () => {
tinymce.triggerSave();
});
},
});
}
};
37 changes: 37 additions & 0 deletions phpmyfaq/admin/assets/src/content/faqs.editor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { create, update } from '../api';
import { serialize } from '../../../../assets/src/utils';
import { pushErrorNotification, pushNotification } from '../utils';

export const handleSaveFaqData = () => {
const submitButton = document.getElementById('faqEditorSubmit');

if (submitButton) {
submitButton.addEventListener('click', async (event) => {
event.preventDefault();
const form = document.getElementById('faqEditor');
const formData = new FormData(form);

const serializedData = serialize(formData);

let response;
if (serializedData.faqId === '0') {
response = await create(serializedData);
} else {
response = await update(serializedData);
}

if (response.success) {
const data = JSON.parse(response.data);
const faqId = document.getElementById('faqId');
const revisionId = document.getElementById('revisionId');

faqId.value = data.id;
revisionId.value = data.revisionId;

pushNotification(response.success);
} else {
pushErrorNotification(response.error);
}
});
}
};
1 change: 1 addition & 0 deletions phpmyfaq/admin/assets/src/content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export * from './csvimport';
export * from './editor';
export * from './faqs';
export * from './faqs.autocomplete';
export * from './faqs.editor';
export * from './glossary';
export * from './markdown';
export * from './media.browser';
Expand Down
7 changes: 6 additions & 1 deletion phpmyfaq/admin/assets/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ import {
handleDeleteGlossary,
handleAddGlossary,
onOpenUpdateGlossaryModal,
handleUpdateGlossary, handleAddNews, handleNews, handleEditNews,
handleUpdateGlossary,
handleAddNews,
handleNews,
handleEditNews,
handleSaveFaqData,
} from './content';
import { handleUserList, handleUsers } from './user';
import { handleGroups } from './group';
Expand Down Expand Up @@ -82,6 +86,7 @@ document.addEventListener('DOMContentLoaded', async () => {
handleMarkdownForm();
handleAttachmentUploads();
handleFileFilter();
handleSaveFaqData();
await handleFaqOverview();

// Content -> Comments
Expand Down
1 change: 0 additions & 1 deletion phpmyfaq/admin/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@
case 'showcategory':
case 'faqs-overview':
case 'editentry':
case 'insertentry':
case 'saveentry':
case 'glossary':
case 'saveglossary':
Expand Down
3 changes: 0 additions & 3 deletions phpmyfaq/admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,6 @@
case 'editpreview':
require 'record.edit.php';
break;
case 'insertentry':
require 'record.add.php';
break;
case 'saveentry':
require 'record.save.php';
break;
Expand Down
Loading

0 comments on commit ab6f271

Please sign in to comment.