1 | // AUTHOOOOOOOOOOOOOOOOOR
|
---|
2 | function editAuthor(authorId) {
|
---|
3 | const modal = document.getElementById('editAuthorModal');
|
---|
4 | modal.style.display = 'block';
|
---|
5 |
|
---|
6 | // Fetch author details and populate form
|
---|
7 | fetch(`../Admin Actions/GetAuthor.php?id=${authorId}`)
|
---|
8 | .then(response => response.json())
|
---|
9 | .then(author => {
|
---|
10 | document.getElementById('authorId').value = author.authorid;
|
---|
11 | document.getElementById('firstName').value = author.firstname;
|
---|
12 | document.getElementById('lastName').value = author.lastname;
|
---|
13 | document.getElementById('nationality').value = author.nationality;
|
---|
14 | document.getElementById('dateOfBirth').value = author.dateofbirth;
|
---|
15 | document.getElementById('authorDescription').value = author.author_description;
|
---|
16 |
|
---|
17 | // Show current image if exists
|
---|
18 | const currentImageDiv = document.getElementById('currentImage');
|
---|
19 | if (author.author_image) {
|
---|
20 | currentImageDiv.innerHTML = `
|
---|
21 | <img src=./AuthorImages/${author.author_image} alt="Current author image" style="max-width: 100px; margin-top: 10px;">
|
---|
22 | <p>Current image</p>
|
---|
23 | `;
|
---|
24 | } else {
|
---|
25 | currentImageDiv.innerHTML = '<p>No image uploaded</p>';
|
---|
26 | }
|
---|
27 | });
|
---|
28 | }
|
---|
29 |
|
---|
30 | function closeAuthorModal() {
|
---|
31 | document.getElementById('editAuthorModal').style.display = 'none';
|
---|
32 | }
|
---|
33 |
|
---|
34 | function searchAuthors() {
|
---|
35 | const searchInput = document.querySelector('.search-input').value.toLowerCase();
|
---|
36 | const tableRows = document.querySelectorAll('#authorsTableBody tr');
|
---|
37 |
|
---|
38 | tableRows.forEach(row => {
|
---|
39 | const firstName = row.cells[1].textContent.toLowerCase();
|
---|
40 | const lastName = row.cells[2].textContent.toLowerCase();
|
---|
41 | const nationality = row.cells[3].textContent.toLowerCase();
|
---|
42 |
|
---|
43 | if (firstName.includes(searchInput) ||
|
---|
44 | lastName.includes(searchInput) ||
|
---|
45 | nationality.includes(searchInput)) {
|
---|
46 | row.style.display = '';
|
---|
47 | } else {
|
---|
48 | row.style.display = 'none';
|
---|
49 | }
|
---|
50 | });
|
---|
51 | document.querySelector('.search-input').addEventListener('keyup', searchAuthors);
|
---|
52 | }
|
---|
53 |
|
---|
54 | function updateAuthor(event) {
|
---|
55 | event.preventDefault();
|
---|
56 | const formData = new FormData(event.target);
|
---|
57 |
|
---|
58 | // Debug: Check what data is being sent
|
---|
59 | for (let pair of formData.entries()) {
|
---|
60 | console.log(pair[0] + ': ' + pair[1]);
|
---|
61 | }
|
---|
62 |
|
---|
63 | fetch('../Admin Actions/UpdateAuthor.php', {
|
---|
64 | method: 'POST',
|
---|
65 | body: formData
|
---|
66 | })
|
---|
67 | .then(response => {
|
---|
68 | console.log('Response status:', response.status);
|
---|
69 | return response.json();
|
---|
70 | })
|
---|
71 | .then(data => {
|
---|
72 | console.log('Response data:', data);
|
---|
73 | if(data.success) {
|
---|
74 | closeAuthorModal();
|
---|
75 | location.reload();
|
---|
76 | } else {
|
---|
77 | alert(data.message || 'Failed to update author');
|
---|
78 | }
|
---|
79 | })
|
---|
80 | .catch(error => {
|
---|
81 | console.error('Error:', error);
|
---|
82 | alert('An error occurred while updating the author');
|
---|
83 | });
|
---|
84 | }
|
---|
85 |
|
---|
86 | function deleteAuthor(authorId) {
|
---|
87 | if(confirm('Are you sure you want to delete this author?')) {
|
---|
88 | fetch('../Admin Actions/DeleteAuthor.php', {
|
---|
89 | method: 'POST',
|
---|
90 | headers: {
|
---|
91 | 'Content-Type': 'application/x-www-form-urlencoded',
|
---|
92 | },
|
---|
93 | body: `authorId=${authorId}`
|
---|
94 | })
|
---|
95 | .then(response => response.json())
|
---|
96 | .then(data => {
|
---|
97 | if(data.success) {
|
---|
98 | location.reload();
|
---|
99 | }
|
---|
100 | });
|
---|
101 | }
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | // ADD AUTHOR
|
---|
106 | function handleAuthorSubmit(event) {
|
---|
107 | event.preventDefault();
|
---|
108 |
|
---|
109 | // Log the form submission event
|
---|
110 | console.debug('Form submission started.');
|
---|
111 |
|
---|
112 | // Create FormData object from the form
|
---|
113 | const formData = new FormData(event.target);
|
---|
114 |
|
---|
115 | // Log the form data for debugging
|
---|
116 | console.debug('FormData created:', Array.from(formData.entries()));
|
---|
117 |
|
---|
118 | // Send the form data to the server
|
---|
119 | fetch('../Admin Actions/AddAuthor.php', {
|
---|
120 | method: 'POST',
|
---|
121 | body: formData
|
---|
122 | })
|
---|
123 | .then(response => response.text()) // Read as plain text
|
---|
124 | .then(text => {
|
---|
125 | console.debug('Raw server response:', text);
|
---|
126 |
|
---|
127 | try {
|
---|
128 | // Try to parse JSON
|
---|
129 | const data = JSON.parse(text);
|
---|
130 | if (data.success) {
|
---|
131 | alert('Author added successfully!');
|
---|
132 | event.target.reset();
|
---|
133 | } else {
|
---|
134 | alert(data.message || 'Error adding author');
|
---|
135 | }
|
---|
136 | } catch (error) {
|
---|
137 | console.error('Invalid JSON:', text);
|
---|
138 | alert('Server returned invalid JSON. Check console for details.');
|
---|
139 | }
|
---|
140 | })
|
---|
141 | .catch(error => {
|
---|
142 | console.error('Fetch error:', error);
|
---|
143 | alert('An error occurred while adding the author');
|
---|
144 | });
|
---|
145 | }
|
---|
146 |
|
---|
147 |
|
---|
148 | function resetAuthorForm() {
|
---|
149 | document.getElementById('addAuthorForm').reset();
|
---|
150 | // Clear any error messages that might be displayed
|
---|
151 | document.querySelectorAll('.error-message').forEach(elem => {
|
---|
152 | elem.style.display = 'none';
|
---|
153 | });
|
---|
154 | } |
---|