|
44 | 44 | credentials: 'same-origin' |
45 | 45 | }).then(function (response) { |
46 | 46 | if (!response.ok) { |
47 | | - throw new Error('unexpected status ' + response.status); |
| 47 | + showArchiveError(form, button); |
| 48 | + return; |
48 | 49 | } |
49 | 50 | button.disabled = false; |
50 | 51 | // Every control for this article, not just the one clicked: the reader |
|
56 | 57 | toggle(other, other.querySelector('button')); |
57 | 58 | } |
58 | 59 | }); |
59 | | - }).catch(function () { |
60 | | - // Fall back to the ordinary post rather than leaving the reader unsure |
61 | | - // whether the click registered. Worst case they get the old behaviour. |
62 | | - form.submit(); |
| 60 | + }, function () { |
| 61 | + showArchiveError(form, button); |
63 | 62 | }); |
64 | 63 | }); |
65 | 64 |
|
| 65 | + // Once the script has intercepted a submission it must never submit the same |
| 66 | + // form again: the request may have reached the server even when its response |
| 67 | + // was lost. Retrying automatically could therefore perform the action twice |
| 68 | + // and, more visibly, bring back the full-page reload this enhancement avoids. |
| 69 | + function showArchiveError(form, button) { |
| 70 | + button.disabled = false; |
| 71 | + var error = form.querySelector('.action-error'); |
| 72 | + if (!error) { |
| 73 | + error = document.createElement('span'); |
| 74 | + error.className = 'action-error'; |
| 75 | + error.setAttribute('role', 'alert'); |
| 76 | + form.appendChild(error); |
| 77 | + } |
| 78 | + error.textContent = 'Could not update. Try again.'; |
| 79 | + } |
| 80 | + |
66 | 81 | // articleOf reads the article's id out of a form's address, which is either |
67 | 82 | // /article/{id}/archive or /article/{id}/unarchive. |
68 | 83 | function articleOf(form) { |
|
0 commit comments