|
13 | 13 | * @since 2022-03-21 |
14 | 14 | */ |
15 | 15 |
|
16 | | -import { addElement } from '../../../assets/src/utils'; |
17 | | - |
18 | 16 | export const handleStatistics = () => { |
19 | 17 | const buttonsDeleteSearchTerm = document.querySelectorAll('.pmf-delete-search-term'); |
20 | 18 |
|
21 | 19 | if (buttonsDeleteSearchTerm) { |
22 | 20 | buttonsDeleteSearchTerm.forEach((element) => { |
23 | | - element.addEventListener('click', (event) => { |
| 21 | + element.addEventListener('click', async (event) => { |
24 | 22 | event.preventDefault(); |
25 | 23 |
|
26 | 24 | const searchTermId = event.target.getAttribute('data-delete-search-term-id'); |
27 | 25 | const csrf = event.target.getAttribute('data-csrf-token'); |
28 | 26 |
|
29 | 27 | if (confirm('Are you sure?')) { |
30 | | - fetch('./api/search/term', { |
31 | | - method: 'DELETE', |
32 | | - headers: { |
33 | | - Accept: 'application/json, text/plain, */*', |
34 | | - 'Content-Type': 'application/json', |
35 | | - }, |
36 | | - body: JSON.stringify({ |
37 | | - csrf: csrf, |
38 | | - searchTermId: searchTermId, |
39 | | - }), |
40 | | - }) |
41 | | - .then(async (response) => { |
42 | | - if (response.ok) { |
43 | | - return response.json(); |
44 | | - } |
45 | | - throw new Error('Network response was not ok: ', { cause: { response } }); |
46 | | - }) |
47 | | - .then((response) => { |
48 | | - const row = document.getElementById(`row-search-id-${response.deleted}`); |
| 28 | + try { |
| 29 | + const response = await fetch('./api/search/term', { |
| 30 | + method: 'DELETE', |
| 31 | + headers: { |
| 32 | + Accept: 'application/json, text/plain, */*', |
| 33 | + 'Content-Type': 'application/json', |
| 34 | + }, |
| 35 | + body: JSON.stringify({ |
| 36 | + csrf: csrf, |
| 37 | + searchTermId: searchTermId, |
| 38 | + }), |
| 39 | + }); |
| 40 | + |
| 41 | + if (response.ok) { |
| 42 | + const jsonResponse = await response.json(); |
| 43 | + const row = document.getElementById(`row-search-id-${jsonResponse.deleted}`); |
49 | 44 | row.addEventListener('click', () => (row.style.opacity = '0')); |
50 | 45 | row.addEventListener('transitionend', () => row.remove()); |
51 | | - }) |
52 | | - .catch(async (error) => { |
53 | | - const errorMessage = await error.cause.response.json(); |
54 | | - console.error(errorMessage.error); |
55 | | - }); |
| 46 | + } else { |
| 47 | + const errorMessage = await response.json(); |
| 48 | + throw new Error(`Network response was not ok: ${errorMessage.error}`); |
| 49 | + } |
| 50 | + } catch (error) { |
| 51 | + console.error(error.message); |
| 52 | + } |
56 | 53 | } |
57 | 54 | }); |
58 | 55 | }); |
|
0 commit comments