Skip to content

Commit 4e6544b

Browse files
authored
Merge pull request #10767 from cdrini/feature/partials-lang-param
Forward ?lang param when rendering partials
2 parents 907031a + c880b3f commit 4e6544b

File tree

9 files changed

+40
-19
lines changed

9 files changed

+40
-19
lines changed

openlibrary/plugins/openlibrary/js/affiliate-links.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { buildPartialsUrl } from './utils'
2+
13
/**
24
* Adds functionality to fetch affialite links asyncronously.
35
*
@@ -49,9 +51,8 @@ function showLoadingIndicators(linkSections) {
4951
*/
5052
async function getPartials(data, affiliateLinksSections) {
5153
const dataString = JSON.stringify(data)
52-
const dataQueryParam = encodeURIComponent(dataString)
5354

54-
return fetch(`/partials.json?_component=AffiliateLinks&data=${dataQueryParam}`)
55+
return fetch(buildPartialsUrl('/partials.json', {_component: 'AffiliateLinks', data: dataString}))
5556
.then((resp) => {
5657
if (resp.status !== 200) {
5758
throw new Error(`Failed to fetch partials. Status code: ${resp.status}`)

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { buildPartialsUrl } from './utils'
2+
13
/**
24
* Initializes lazy-loading the "Lists" section of Open Library book pages.
35
*
@@ -61,6 +63,6 @@ async function fetchPartials(workId, editionId) {
6163
if (editionId) {
6264
params.editionId = editionId
6365
}
64-
const searchParams = new URLSearchParams(params)
65-
return fetch(`/partials.json?${searchParams.toString()}`)
66+
67+
return fetch(buildPartialsUrl('/partials.json', params));
6668
}

openlibrary/plugins/openlibrary/js/carousel/Carousel.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Slick#1.6.0 is not on npm
22
import 'slick-carousel';
33
import '../../../../../static/css/components/carousel--js.less';
4+
import { buildPartialsUrl } from '../utils.js';
45

56
/**
67
* @typedef {Object} CarouselConfig
@@ -142,9 +143,8 @@ export class Carousel {
142143
}
143144

144145
fetchPartials() {
145-
const url = new URL(`${location.origin}/partials.json`)
146146
const loadMore = this.loadMore
147-
const params = {
147+
const url = buildPartialsUrl('/partials.json', {
148148
_component: 'CarouselLoadMore',
149149
queryType: loadMore.queryType,
150150
q: loadMore.q,
@@ -156,8 +156,7 @@ export class Carousel {
156156
hasFulltextOnly: loadMore.hasFulltextOnly,
157157
secondaryAction: loadMore.secondaryAction,
158158
key: loadMore.key
159-
}
160-
url.search = new URLSearchParams(params).toString()
159+
});
161160
this.appendLoadingSlide();
162161
$.ajax({url: url, type: 'GET'})
163162
.then((results) => {

openlibrary/plugins/openlibrary/js/check-ins/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
import { PersistentToast } from '../Toast'
66
import { initDialogs } from '../native-dialog'
7+
import { buildPartialsUrl } from '../utils'
78

89
/**
910
* Enum for check-in event types.
@@ -587,7 +588,7 @@ function updateProgressComponent(elem, goal) {
587588
* @param {string} goalYear Year that the goal is set for.
588589
*/
589590
function fetchProgressAndUpdateViews(yearlyGoalElems, goalYear) {
590-
fetch(`/reading-goal/partials.json?year=${goalYear}`)
591+
fetch(buildPartialsUrl('/reading-goal/partials.json', {year: goalYear}))
591592
.then((response) => {
592593
if (!response.ok) {
593594
throw new Error('Failed to fetch progress element')

openlibrary/plugins/openlibrary/js/fulltext-search-suggestion.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { buildPartialsUrl } from './utils'
2+
13
export function initFulltextSearchSuggestion(fulltextSearchSuggestion) {
24
const isLoading = showLoadingIndicators(fulltextSearchSuggestion)
35
if (isLoading) {
@@ -16,8 +18,7 @@ function showLoadingIndicators(fulltextSearchSuggestion) {
1618
return isLoading
1719
}
1820
async function getPartials(fulltextSearchSuggestion, query) {
19-
const queryParam = encodeURIComponent(query)
20-
return fetch(`/partials.json?_component=FulltextSearchSuggestion&data=${queryParam}`)
21+
return fetch(buildPartialsUrl('/partials.json', {_component: 'FulltextSearchSuggestion', data: query}))
2122
.then((resp) => {
2223
if (resp.status !== 200) {
2324
throw new Error(`Failed to fetch partials. Status code: ${resp.status}`)

openlibrary/plugins/openlibrary/js/lazy-carousel.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {initialzeCarousels} from './carousel';
2+
import { buildPartialsUrl } from './utils';
23

34
/**
45
* Adds functionality that allows carousels to lazy-load when a patron
@@ -34,8 +35,7 @@ export function initLazyCarousel(elems) {
3435
* @returns {Promise<Response>}
3536
*/
3637
async function fetchPartials(data) {
37-
const searchParams = new URLSearchParams({...data, _component: 'LazyCarousel'})
38-
return fetch(`/partials.json?${searchParams.toString()}`)
38+
return fetch(buildPartialsUrl('/partials.json', {...data, _component: 'LazyCarousel'}))
3939
}
4040

4141
/**

openlibrary/plugins/openlibrary/js/lists/ListService.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* @module lists/ListService
44
*/
55

6+
import { buildPartialsUrl } from '../utils';
7+
68
/**
79
* Makes a POST to a `.json` endpoint.
810
* @param {object} data Configurations and payload for POST request.
@@ -158,14 +160,14 @@ export function updateReadingLog(formElem, success) {
158160
export function fetchPartials(key, success) {
159161
$.ajax({
160162
type: 'GET',
161-
url: `/lists/partials.json?key=${key}`,
163+
url: buildPartialsUrl('/lists/partials.json', {key}),
162164
success: success
163165
})
164166
}
165167

166168
// XXX : jsdoc
167169
export async function getListPartials() {
168-
return await fetch('/lists/partials.json', {
170+
return await fetch(buildPartialsUrl('/lists/partials.json'), {
169171
method: 'GET',
170172
headers: {
171173
'Content-Type': 'application/json',

openlibrary/plugins/openlibrary/js/search.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
* Functionalities for templates/work_search and related templates.
33
*/
44

5+
import { buildPartialsUrl } from './utils';
6+
57
/**
68
* Displays more facets by removing the ui-helper-hidden class.
79
*
@@ -129,12 +131,11 @@ function fetchPartials(param) {
129131
path: location.pathname,
130132
query: location.search
131133
}
132-
const dataString = JSON.stringify(data)
133134

134-
return fetch(`/partials.json?${new URLSearchParams({
135+
return fetch(buildPartialsUrl('/partials.json', {
135136
_component: 'SearchFacets',
136-
data: dataString
137-
})}`)
137+
data: JSON.stringify(data),
138+
}))
138139
.then((resp) => {
139140
if (!resp.ok) {
140141
throw new Error(`Failed to fetch partials. Status code: ${resp.status}`)

openlibrary/plugins/openlibrary/js/utils.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,17 @@ export function trimInputValues(param) {
7272
});
7373
});
7474
}
75+
76+
export function buildPartialsUrl(path, params) {
77+
const curUrl = new URL(window.location.href);
78+
const url = new URL(location.origin + path);
79+
if (curUrl.searchParams.has('lang')) {
80+
url.searchParams.set('lang', curUrl.searchParams.get('lang'));
81+
}
82+
83+
for (const key in params) {
84+
url.searchParams.set(key, params[key]);
85+
}
86+
87+
return url;
88+
}

0 commit comments

Comments
 (0)