Skip to content

Commit 6d2c117

Browse files
authored
Merge pull request #11350 from taearls/11327/feature/make-follow-buttons-asynchronous
11327/feature/make follow buttons asynchronous
2 parents 9c7aea9 + 3917167 commit 6d2c117

File tree

3 files changed

+49
-40
lines changed

3 files changed

+49
-40
lines changed

openlibrary/plugins/openlibrary/js/book-page-lists.js

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { buildPartialsUrl } from './utils'
2-
import { PersistentToast } from './Toast'
2+
import { initAsyncFollowing } from './following'
3+
34
/**
45
* Initializes lazy-loading the "Lists" section of Open Library book pages.
56
*
@@ -84,42 +85,3 @@ async function fetchPartials(workId, editionId) {
8485

8586
return fetch(buildPartialsUrl('BPListsSection', params));
8687
}
87-
88-
async function initAsyncFollowing(followForms) {
89-
followForms.forEach(form => {
90-
form.addEventListener('submit', async e => {
91-
e.preventDefault();
92-
const url = form.action;
93-
const formData = new FormData(form);
94-
const submitButton = form.querySelector('button[type=submit]')
95-
const stateInput = form.querySelector('input[name=state]')
96-
97-
const isFollowRequest = stateInput.value === '0'
98-
const i18nStrings = JSON.parse(submitButton.dataset.i18n)
99-
submitButton.disabled = true
100-
101-
await fetch(url, {
102-
method: 'POST',
103-
headers: {
104-
'Content-Type': 'application/x-www-form-urlencoded',
105-
},
106-
body: new URLSearchParams(formData)
107-
})
108-
.then(resp => {
109-
if (!resp.ok) {
110-
throw new Error('Network response was not ok');
111-
}
112-
submitButton.classList.toggle('cta-btn--primary')
113-
submitButton.classList.toggle('cta-btn--delete')
114-
submitButton.textContent = isFollowRequest ? i18nStrings.unfollow : i18nStrings.follow
115-
stateInput.value = isFollowRequest ? '1' : '0'
116-
})
117-
.catch(() => {
118-
new PersistentToast(i18nStrings.errorMsg).show();
119-
})
120-
.finally(() => {
121-
submitButton.disabled = false
122-
});
123-
});
124-
});
125-
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { PersistentToast } from './Toast';
2+
3+
export async function initAsyncFollowing(followForms) {
4+
followForms.forEach(form => {
5+
form.addEventListener('submit', async (e) => {
6+
e.preventDefault();
7+
const url = form.action;
8+
const formData = new FormData(form);
9+
const submitButton = form.querySelector('button[type=submit]');
10+
const stateInput = form.querySelector('input[name=state]');
11+
12+
const isFollowRequest = stateInput.value === '0';
13+
const i18nStrings = JSON.parse(submitButton.dataset.i18n);
14+
submitButton.disabled = true;
15+
16+
await fetch(url, {
17+
method: 'POST',
18+
headers: {
19+
'Content-Type': 'application/x-www-form-urlencoded',
20+
},
21+
body: new URLSearchParams(formData)
22+
})
23+
.then(resp => {
24+
if (!resp.ok) {
25+
throw new Error('Network response was not ok');
26+
}
27+
submitButton.classList.toggle('cta-btn--primary');
28+
submitButton.classList.toggle('cta-btn--delete');
29+
submitButton.textContent = isFollowRequest ? i18nStrings.unfollow : i18nStrings.follow;
30+
stateInput.value = isFollowRequest ? '1' : '0';
31+
})
32+
.catch(() => {
33+
new PersistentToast(i18nStrings.errorMsg).show();
34+
})
35+
.finally(() => {
36+
submitButton.disabled = false;
37+
});
38+
});
39+
});
40+
}

openlibrary/plugins/openlibrary/js/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,13 @@ jQuery(function () {
549549
.then(module => module.initListsSection(listSection))
550550
}
551551

552+
// Initialize follow forms lazily
553+
const followForms = document.querySelectorAll('.follow-form');
554+
if (followForms.length) {
555+
import(/* webpackChunkName: "following" */ './following')
556+
.then(module => module.initAsyncFollowing(followForms))
557+
}
558+
552559
// Generalized carousel lazy-loading
553560
const lazyCarousels = document.querySelectorAll('.lazy-carousel')
554561
if (lazyCarousels.length) {

0 commit comments

Comments
 (0)