Skip to content

Commit a060b9b

Browse files
authored
Merge pull request #1720 from NatLibFi/issue1692-remove-search-language-cookie
Remove the search language cookie
2 parents bf41dee + 10e78c5 commit a060b9b

File tree

2 files changed

+32
-50
lines changed

2 files changed

+32
-50
lines changed

resource/js/vocab-search.js

+14-27
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ const vocabSearch = Vue.createApp({
6969
return false
7070
},
7171
parseSearchLang () {
72-
// if content language can be found from uri params, use that and update it to SKOSMOS object and to search lang cookie
72+
// if content language can be found from uri params, use that and update it to SKOSMOS object
7373
const urlParams = new URLSearchParams(window.location.search)
7474
const paramLang = urlParams.get('clang')
7575
const anyLang = urlParams.get('anylang')
@@ -81,19 +81,11 @@ const vocabSearch = Vue.createApp({
8181
this.changeLang(paramLang)
8282
return paramLang
8383
}
84-
// use searchLangCookie if it can be found, otherwise pick content lang from SKOSMOS object
85-
const cookies = document.cookie.split('; ')
86-
const searchLangCookie = cookies.find(cookie =>
87-
cookie.startsWith('SKOSMOS_SEARCH_LANG='))
88-
if (searchLangCookie) {
89-
const selectedLanguage = searchLangCookie.split('=')[1]
90-
if (selectedLanguage !== 'all') {
91-
window.SKOSMOS.content_lang = selectedLanguage
92-
}
93-
return selectedLanguage
94-
} else {
84+
// otherwise pick content lang from SKOSMOS object (it should always exist)
85+
if (window.SKOSMOS.content_lang) {
9586
return window.SKOSMOS.content_lang
9687
}
88+
return null
9789
},
9890
renderMatchingPart (searchTerm, label, lang = null) {
9991
if (label) {
@@ -151,6 +143,9 @@ const vocabSearch = Vue.createApp({
151143
if ('uri' in result) { // create relative Skosmos page URL from the search result URI
152144
result.pageUrl = window.SKOSMOS.vocab + '/' + window.SKOSMOS.lang + '/page?'
153145
const urlParams = new URLSearchParams({ uri: result.uri })
146+
if (this.selectedLanguage !== window.SKOSMOS.lang) { // add content language parameter
147+
urlParams.append('clang', this.selectedLanguage)
148+
}
154149
result.pageUrl += urlParams.toString()
155150
}
156151
// render search result renderedTypes
@@ -184,19 +179,19 @@ const vocabSearch = Vue.createApp({
184179
const searchUrl = vocabHref + 'search?' + searchUrlParams.toString()
185180
window.location.href = searchUrl
186181
},
187-
changeLang (lang) {
188-
this.selectedLanguage = lang
189-
this.setSearchLangCookie(lang)
182+
changeLang (clang) {
183+
this.selectedLanguage = clang
184+
window.SKOSMOS.content_lang = clang
190185
this.resetSearchTermAndHideDropdown()
191186
},
192-
changeContentLangAndReload (lang) {
193-
this.changeLang(lang)
187+
changeContentLangAndReload (clang) {
188+
this.changeLang(clang)
194189
const params = new URLSearchParams(window.location.search)
195-
if (lang === 'all') {
190+
if (clang === 'all') {
196191
params.set('anylang', 'true')
197192
} else {
198193
params.delete('anylang')
199-
params.set('clang', lang)
194+
params.set('clang', clang)
200195
}
201196
this.$forceUpdate()
202197
window.location.search = params.toString()
@@ -212,14 +207,6 @@ const vocabSearch = Vue.createApp({
212207
showAutoComplete () {
213208
this.showDropdown = true
214209
this.$forceUpdate()
215-
},
216-
setSearchLangCookie (lang) {
217-
// The cookie path should be relative if the baseHref is known
218-
let cookiePath = '/'
219-
if (window.SKOSMOS.baseHref && window.SKOSMOS.baseHref.replace(window.origin, '')) {
220-
cookiePath = window.SKOSMOS.baseHref.replace(window.origin, '')
221-
}
222-
document.cookie = `SKOSMOS_SEARCH_LANG=${this.selectedLanguage};path=${cookiePath}`
223210
}
224211
},
225212
template: `

tests/cypress/template/vocab-search-bar.cy.js

+18-23
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,24 @@ describe('Vocab search bar', () => {
156156
cy.get('#main-container').click({ force: true }); // using force true to click on elements not considered actionable
157157
cy.get('#search-autocomplete-results').should('not.be.visible'); // the autocomplete should disappear
158158
})
159+
it('Search language parameter is passed to the autocomplete result links', () => {
160+
cy.visit('/yso/sv/')
161+
162+
// Choose 'fi' for search & content language
163+
cy.get('#language-selector .dropdown-toggle').click();
164+
cy.get('#language-list .dropdown-item').contains('finska').click();
165+
166+
// Searchg for 'kissa'
167+
cy.get('#search-field').type('aarre');
168+
cy.get('#search-autocomplete-results', { timeout: 20000 }).should('be.visible'); // the autocomplete should appear
169+
170+
// Click the first search result
171+
cy.get('#search-autocomplete-results li:first-child a').click();
172+
173+
// The language parameters should persist on the concept page
174+
cy.url().should('include', '/sv/');
175+
cy.url().should('include', 'clang=fi');
176+
})
159177
});
160178

161179
describe('Search Result Rendering', () => {
@@ -195,27 +213,4 @@ describe('Vocab search bar', () => {
195213
})
196214
})
197215
});
198-
199-
describe('Cookie Management', () => {
200-
it('The search language cookie is set', () => {
201-
cy.visit('/yso/fi/');
202-
203-
// Select a language option from the dropdown
204-
cy.get('#language-selector .dropdown-toggle').click();
205-
cy.get('#language-list .dropdown-item').contains('ruotsi').click();
206-
207-
// Test that the cookie has been set correctly
208-
cy.getCookie('SKOSMOS_SEARCH_LANG').should('have.property', 'value', 'sv');
209-
});
210-
211-
it('The search language cookie is read', () => {
212-
cy.visit('/'); // setting the cookie at the front page to make sure the cookies are set globally
213-
cy.setCookie('SKOSMOS_SEARCH_LANG', 'en');
214-
cy.visit('/yso/fi/');
215-
216-
// Test that the cookie value has been read and used to initialize the language selector
217-
cy.get('.dropdown .dropdown-toggle').should('contain', 'englanti');
218-
219-
});
220-
});
221216
})

0 commit comments

Comments
 (0)