-
Notifications
You must be signed in to change notification settings - Fork 1
/
blog.js
163 lines (132 loc) · 4.04 KB
/
blog.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
const postList = document.querySelector('#post-list');
const addButtonEl = document.querySelector('#add-button');
let posts = [];
function renderPosts() {
postList.innerHTML = '';
posts.forEach((post, index) => {
const row = document.createElement('tr');
const title = document.createElement('td');
const date = document.createElement('td');
const summary = document.createElement('td');
const actions = document.createElement('td');
const editButton = document.createElement('button');
const deleteButton = document.createElement('button');
title.textContent = post.title;
date.textContent = post.date;
summary.textContent = post.summary;
editButton.textContent = 'Edit';
deleteButton.textContent = 'Delete';
editButton.addEventListener('click', () => {
editPost(index);
});
deleteButton.addEventListener('click', () => {
deletePost(index);
});
actions.appendChild(editButton);
actions.appendChild(deleteButton);
row.appendChild(title);
row.appendChild(date);
row.appendChild(summary);
row.appendChild(actions);
postList.appendChild(row);
});
}
function addPost() {
const title = prompt('Enter post title:');
const date = prompt('Enter post date:');
const summary = prompt('Enter post summary:');
if (title && date && summary) {
const post = { title, date, summary };
posts.push(post);
renderPosts();
savePosts();
}
}
function editPost(index) {
const post = posts[index];
const title = prompt('Enter new post title:', post.title);
const date = prompt('Enter new post date:', post.date);
const summary = prompt('Enter new post summary:', post.summary);
if (title && date && summary) {
posts[index] = { title, date, summary };
renderPosts();
savePosts();
}
}
function deletePost(index) {
if (confirm('Are you sure you want to delete this post?')) {
posts.splice(index, 1);
renderPosts();
savePosts();
}
}
function savePosts() {
localStorage.setItem('posts', JSON.stringify(posts));
}
function loadPosts() {
const savedPosts = localStorage.getItem('posts');
if (savedPosts) {
posts = JSON.parse(savedPosts);
}
renderPosts();
}
addButtonEl.addEventListener('click', addPost);
loadPosts();
const postDialogEl = document.querySelector('#post-dialog');
const postFormEl = postDialogEl.querySelector('form');
const titleInputEl = postFormEl.querySelector('#title-input');
const dateInputEl = postFormEl.querySelector('#date-input');
const summaryInputEl = postFormEl.querySelector('#summary-input');
const cancelButtonEl = postFormEl.querySelector('#cancel-button');
function addPost() {
// Open the dialog
postDialogEl.showModal();
// Clear the input fields
titleInputEl.value = '';
dateInputEl.value = '';
summaryInputEl.value = '';
// Handle form submission
postFormEl.addEventListener('submit', (event) => {
event.preventDefault();
const title = titleInputEl.value;
const date = dateInputEl.value;
const summary = summaryInputEl.value;
if (title && date && summary) {
const post = { title, date, summary };
posts.push(post);
renderPosts();
savePosts();
postDialogEl.close();
}
});
// Handle cancel button click
cancelButtonEl.addEventListener('click', () => {
postDialogEl.close();
});
}
function editPost(index) {
const post = posts[index];
// Open the dialog
postDialogEl.showModal();
// Set the input field values
titleInputEl.value = post.title;
dateInputEl.value = post.date;
summaryInputEl.value = post.summary;
// Handle form submission
postFormEl.addEventListener('submit', (event) => {
event.preventDefault();
const title = titleInputEl.value;
const date = dateInputEl.value;
const summary = summaryInputEl.value;
if (title && date && summary) {
posts[index] = { title, date, summary };
renderPosts();
savePosts();
postDialogEl.close();
}
});
// Handle cancel button click
cancelButtonEl.addEventListener('click', () => {
postDialogEl.close();
});
}