Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Persist search term across pages (#474)

Olga Bulata3bd73b3978e
diff --git a/src/components/NavSection.vue b/src/components/NavSection.vue
index 66f171a6c..968de916c 100644
--- a/src/components/NavSection.vue
+++ b/src/components/NavSection.vue
@@ -139,13 +139,13 @@
</template>
<script>
-import { mapActions } from 'vuex'
+import { mapActions, mapState } from 'vuex'
import Dropdown from '~/components/Dropdown'
import VIcon from '~/components/VIcon/VIcon.vue'
-import { UPDATE_QUERY } from '~/constants/action-types'
-import { SEARCH } from '~/constants/store-modules'
+import { FETCH_MEDIA, UPDATE_QUERY } from '~/constants/action-types'
+import { MEDIA, SEARCH } from '~/constants/store-modules'
import OpenverseLogo from '~/assets/logo.svg?inline'
import externalLinkIcon from '~/assets/icons/external-link.svg'
@@ -168,17 +168,24 @@ export default {
externalLinkIcon,
}),
computed: {
+ ...mapState(SEARCH, ['query']),
navSearchPlaceholder() {
return this.$t('header.placeholder')
},
},
+ mounted() {
+ this.form.searchTerm = this.query.q
+ },
methods: {
...mapActions(SEARCH, { setSearchTerm: UPDATE_QUERY }),
+ ...mapActions(MEDIA, { fetchMedia: FETCH_MEDIA }),
onSubmit() {
const q = this.form.searchTerm
+ const mediaType = this.query.mediaType
this.setSearchTerm({ q })
+ this.fetchMedia({ q, mediaType })
const newPath = this.localePath({
- path: '/search',
+ path: `/search/${mediaType}`,
query: { q },
})
this.$router.push(newPath)
diff --git a/test/unit/specs/components/nav-section.spec.js b/test/unit/specs/components/nav-section.spec.js
index 8dff9037f..cc00a5cbb 100644
--- a/test/unit/specs/components/nav-section.spec.js
+++ b/test/unit/specs/components/nav-section.spec.js
@@ -3,13 +3,9 @@ import render from '../../test-utils/render'
import { createLocalVue } from '@vue/test-utils'
import NavSection from '~/components/NavSection'
import { UPDATE_QUERY } from '~/constants/action-types'
+import { IMAGE } from '~/constants/media'
describe('NavSection', () => {
- it('should render correct contents', () => {
- const wrapper = render(NavSection, { stubs: { NuxtLink: true } })
- expect(wrapper.find('nav').vm).toBeDefined()
- })
-
it('dispatches an action when the form is submitted', async () => {
const localVue = createLocalVue()
localVue.use(Vuex)
@@ -18,6 +14,9 @@ describe('NavSection', () => {
modules: {
search: {
namespaced: true,
+ state: {
+ query: { q: 'foo', mediaType: IMAGE },
+ },
actions: {
[UPDATE_QUERY]: dispatchMock,
},
@@ -37,10 +36,9 @@ describe('NavSection', () => {
}
const wrapper = render(NavSection, options)
- await wrapper.setData({ form: { searchTerm: 'foo' } })
wrapper.find('.search-form').trigger('submit')
expect(routerMock.push).toHaveBeenCalledWith({
- path: '/search',
+ path: '/search/image',
query: { q: 'foo' },
})
const dispatchArgs = dispatchMock.mock.calls[0][1]