This repository has been archived by the owner on Sep 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
78 lines (78 loc) · 2.99 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const dropdown = document.querySelector(
'.navbar-item.has-dropdown.is-hoverable'
);
const save = document.getElementById('save');
const edit = document.getElementById('edit');
const notification = document.getElementById('notification');
const editors = [];
save.addEventListener('click', async (event) => {
const data = editors.map((editor) => editor.getData());
const response = await fetch('create.php', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
});
notification.textContent = (await response.json()).message || 'Oops!';
notification.classList.remove('is-hidden');
setTimeout(() => notification.classList.add('is-hidden'), 4444);
});
edit.addEventListener('click', async (event) => {
const { id } = event.target.dataset;
const data = editors.map((editor) => editor.getData());
const response = await fetch(`update.php?id=${id}`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
});
const { message } = await response.json();
notification.textContent = message || 'Oops!';
notification.classList.remove('is-hidden');
setTimeout(() => notification.classList.add('is-hidden'), 4444);
editors.forEach((editor) => editor.setData(''));
edit.dataset.id = undefined;
edit.classList.add('is-hidden');
save.classList.remove('is-hidden');
});
(async () => {
try {
editors.push(
await ClassicEditor.create(document.getElementById('editor1')),
await ClassicEditor.create(document.getElementById('editor2')),
await ClassicEditor.create(document.getElementById('editor3')),
await ClassicEditor.create(document.getElementById('editor4')),
await ClassicEditor.create(document.getElementById('editor5')),
await ClassicEditor.create(document.getElementById('editor6'))
);
} catch (error) {
console.error(error);
}
})();
(async () => {
const response = await fetch('fetch.php');
const { data } = await response.json();
const wrapper = document.createElement('div');
wrapper.classList.add('navbar-dropdown');
data.forEach((id) => {
const link = document.createElement('a');
link.classList.add('navbar-item');
link.textContent = `ID #${id}`;
link.addEventListener('click', async (event) => {
const response = await fetch(`show.php?id=${id}`);
const { message, data } = await response.json();
notification.textContent = message || 'Oops!';
notification.classList.remove('is-hidden');
setTimeout(() => notification.classList.add('is-hidden'), 4444);
editors[0].setData(data.ParagraphOne);
editors[1].setData(data.SectionOne);
editors[2].setData(data.ParagraphTwo);
editors[3].setData(data.SectionTwo);
editors[4].setData(data.ParagraphThree);
editors[5].setData(data.SectionThree);
save.classList.add('is-hidden');
edit.classList.remove('is-hidden');
edit.dataset.id = id;
});
wrapper.appendChild(link);
});
dropdown.appendChild(wrapper);
})();