Skip to content

Commit

Permalink
refactor: moved news to twig (#2791)
Browse files Browse the repository at this point in the history
  • Loading branch information
modelrailroader committed Apr 26, 2024
1 parent e097a37 commit 8154fdf
Show file tree
Hide file tree
Showing 12 changed files with 983 additions and 550 deletions.
1 change: 1 addition & 0 deletions phpmyfaq/admin/assets/src/api/index.js
Expand Up @@ -5,3 +5,4 @@ export * from './group';
export * from './statistics';
export * from './tags';
export * from './user';
export * from './news';
132 changes: 132 additions & 0 deletions phpmyfaq/admin/assets/src/api/news.js
@@ -0,0 +1,132 @@
/**
* Fetch data for news
*
* 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>
* @author Jan Harms <modelrailroader@gmx-topmail.de>
* @copyright 2024 phpMyFAQ Team
* @license http://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2024-04-21
*/
import { pushErrorNotification, pushNotification } from '../utils';

export const addNews = async (data = {}) => {
try {
const response = await fetch('api/news/add', {
method: 'POST',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: JSON.stringify(data),
});

const result = await response.json();
if (result.success) {
pushNotification(result.success);
setTimeout(function () {
window.location.href = '?action=news';
}, 3000);
} else {
pushErrorNotification(result.error);
}
} catch (error) {
console.error('Error posting news data:', error);
throw error;
}
};

export const deleteNews = async (csrfToken, id) => {
try {
const response = await fetch('api/news/delete', {
method: 'DELETE',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: JSON.stringify({
csrfToken: csrfToken,
id: id
}),
});

const result = await response.json();
if (result.success) {
pushNotification(result.success);
setTimeout(function () {
window.location.reload();
}, 3000);
} else {
pushErrorNotification(result.error);
}
} catch (error) {
console.error('Error deleting news data:', error);
throw error;
}
};

export const updateNews = async (data = {}) => {
try {
const response = await fetch('api/news/update', {
method: 'PUT',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: JSON.stringify(data),
});

const result = await response.json();
if (result.success) {
pushNotification(result.success);
setTimeout(function () {
window.location.href = '?action=news';
}, 3000);
} else {
pushErrorNotification(result.error);
}
} catch (error) {
console.error('Error posting news data:', error);
throw error;
}
};

export const activateNews = async (id, status, csrfToken) => {
try {
const response = await fetch('api/news/activate', {
method: 'POST',
cache: 'no-cache',
headers: {
'Content-Type': 'application/json',
},
redirect: 'follow',
referrerPolicy: 'no-referrer',
body: JSON.stringify({
id: id,
status: status,
csrfToken: csrfToken
}),
});

const result = await response.json();
if (result.success) {
pushNotification(result.success);
} else {
pushErrorNotification(result.error);
}
} catch (error) {
console.error('Error posting news data:', error);
throw error;
}
};
1 change: 1 addition & 0 deletions phpmyfaq/admin/assets/src/content/index.js
Expand Up @@ -9,6 +9,7 @@ export * from './faqs.autocomplete';
export * from './glossary';
export * from './markdown';
export * from './media.browser';
export * from './news';
export * from './faqs.overview';
export * from './question';
export * from './tags';
Expand Down
111 changes: 111 additions & 0 deletions phpmyfaq/admin/assets/src/content/news.js
@@ -0,0 +1,111 @@
/**
* News administration stuff
*
* 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>
* @author Jan Harms <modelrailroader@gmx-topmail.de>
* @copyright 2024 phpMyFAQ Team
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
* @link https://www.phpmyfaq.de
* @since 2024-04-20
*/

import { activateNews, addNews, deleteNews, updateNews } from '../api';
import { Modal } from 'bootstrap';
import { TinyMCE } from 'tinymce';
import tinymce from 'tinymce/tinymce';

export const handleAddNews = () => {
const submit = document.getElementById('submitAddNews');
if (submit) {
submit.addEventListener('click', async (event) => {
event.preventDefault();
let target = '';
document.querySelectorAll('#target').forEach(item) => {
if (item.checked) {
target = item.value;
}
});

const data = {
dateStart: document.getElementById('dateStart').value,
dateEnd: document.getElementById('dateEnd').value,
news: tinymce.get('editor').getContent(),
newsHeader: document.getElementById('newsheader').value,
authorName: document.getElementById('authorName').value,
authorEmail: document.getElementById('authorEmail').value,
active: document.getElementById('active').checked,
comment: document.getElementById('comment').checked,
link: document.getElementById('link').value,
linkTitle: document.getElementById('linkTitle').value,
langTo: document.getElementById('langTo').value,
target: target,
csrfToken: document.getElementById('pmf-csrf-token').value
}
await addNews(data);
});
}
}

export const handleNews = () => {
const deleteNewsButton = document.getElementById('deleteNews');
if (deleteNewsButton) {
document.querySelectorAll('#deleteNews').forEach(item) => {
item.addEventListener('click', (event) => {
event.preventDefault();
const modal = new Modal(document.getElementById('confirmDeleteNewsModal'));
document.getElementById('newsId').value = item.getAttribute('data-pmf-newsid');
modal.show();
});
});
document.getElementById('pmf-delete-news-action').addEventListener('click', async (event) => {
event.preventDefault();
const csrfToken = document.getElementById('pmf-csrf-token-delete').value;
const id = document.getElementById('newsId').value;
await deleteNews(csrfToken, id);
});
document.querySelectorAll('#activate').forEach(item) => {
item.addEventListener('click', async () => {
await activateNews(item.getAttribute('data-pmf-id'), item.checked, item.getAttribute('data-pmf-csrf-token'));
});
});
}
}

export const handleEditNews = () => {
const submit = document.getElementById('submitEditNews');
if (submit) {
submit.addEventListener('click', async (event) => {
event.preventDefault();
let target = '';
document.querySelectorAll('#target').forEach(item) => {
if (item.checked) {
target = item.value;
}
});

const data = {
id: document.getElementById('id').value,
csrfToken: document.getElementById('pmf-csrf-token').value,
dateStart: document.getElementById('dateStart').value,
dateEnd: document.getElementById('dateEnd').value,
news: tinymce.get('editor').getContent(),
newsHeader: document.getElementById('newsheader').value,
authorName: document.getElementById('authorName').value,
authorEmail: document.getElementById('authorEmail').value,
active: document.getElementById('active').checked,
comment: document.getElementById('comment').checked,
link: document.getElementById('link').value,
linkTitle: document.getElementById('linkTitle').value,
langTo: document.getElementById('langTo').value,
target: target
}

await updateNews(data);
});
}
}
7 changes: 6 additions & 1 deletion phpmyfaq/admin/assets/src/index.js
Expand Up @@ -43,7 +43,7 @@ import {
handleDeleteGlossary,
handleAddGlossary,
onOpenUpdateGlossaryModal,
handleUpdateGlossary,
handleUpdateGlossary, handleAddNews, handleNews, handleEditNews,
} from './content';
import { handleUserList, handleUsers } from './user';
import { handleGroups } from './group';
Expand Down Expand Up @@ -133,4 +133,9 @@ document.addEventListener('DOMContentLoaded', async () => {

await handleFormEdit();
await handleFormTranslations();

// News
await handleAddNews();
await handleNews();
await handleEditNews();
});

0 comments on commit 8154fdf

Please sign in to comment.