Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: moved news to twig (#2791)
- Loading branch information
1 parent
e097a37
commit 8154fdf
Showing
12 changed files
with
983 additions
and
550 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.