Skip to content

Commit

Permalink
feat: props and references for SearchBar
Browse files Browse the repository at this point in the history
  • Loading branch information
gromdimon committed Aug 30, 2023
1 parent ec3d82d commit e021214
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 70 deletions.
2 changes: 1 addition & 1 deletion frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<link rel="icon" href="/favicon.ico">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vite App</title>
<title>REEV</title>
</head>
<body>
<div id="app"></div>
Expand Down
Binary file modified frontend/public/favicon.ico
Binary file not shown.
5 changes: 3 additions & 2 deletions frontend/src/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const roundIt = (value: number, digits: number = 2, label?: string): stri
*
* @param searchTerm The search term to use.
*/
export const search = (searchTerm: string) => {
export const search = (searchTerm: string, genomeRelease: string) => {
interface RouteLocationFragment {
name: string
params?: any
Expand All @@ -38,7 +38,8 @@ export const search = (searchTerm: string) => {
(): RouteLocationFragment => ({
name: 'gene',
params: {
searchTerm: searchTerm
searchTerm: searchTerm,
genomeRelease: genomeRelease
}
})
]
Expand Down
38 changes: 17 additions & 21 deletions frontend/src/components/HeaderDetailPage.vue
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useGeneInfoStore } from '@/stores/geneInfo'
import SearchBar from '@/components/SearchBar.vue'
import { search } from '@/api/utils'
const router = useRouter()
export interface Props {
searchTerm?: string
genomeRelease?: string
}
const geneInfoStore = useGeneInfoStore()
const props = withDefaults(defineProps<Props>(), {
searchTerm: '',
genomeRelease: 'grch37'
})
const router = useRouter()
const searchTerm = ref('')
const genomeRelease = ref('grch37')
const searchTermRef = ref(props.searchTerm)
const genomeReleaseRef = ref(props.genomeRelease)
const performSearch = async () => {
const routeLocation: any = search(searchTerm.value)
console.log(routeLocation)
const routeLocation: any = search(searchTermRef.value, genomeReleaseRef.value)
if (routeLocation) {
router.push(routeLocation)
} else {
console.log(`no route found for ${searchTerm.value}`)
console.error(`no route found for ${searchTermRef.value}`)
}
}
// Load props search term and genome release from local storage on mount
onMounted(() => {
searchTerm.value = localStorage.getItem('searchTerm') || ''
genomeRelease.value = localStorage.getItem('genomeRelease') || 'grch37'
if (geneInfoStore.geneInfo === null) {
router.push({ name: 'home' })
}
})
</script>

<template>
Expand All @@ -43,8 +39,8 @@ onMounted(() => {
</v-toolbar-title>
<SearchBar
class="top-search-bar"
v-model:search-term="searchTerm"
v-model:genome-release="genomeRelease"
v-model:search-term="searchTermRef"
v-model:genome-release="genomeReleaseRef"
@click-search="performSearch"
/>
<v-spacer></v-spacer>
Expand Down
11 changes: 0 additions & 11 deletions frontend/src/components/__tests__/HeaderDetailPage.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,4 @@ describe('HeaderDetailPage', async () => {
const search = wrapper.find('#search')
expect(search.exists()).toBe(true)
})

it('redirects if gene data is null', async () => {
const store = useGeneInfoStore()
store.storeState = StoreState.Initial
store.geneSymbol = null
store.geneInfo = null

makeWrapper()

expect(router.push).toHaveBeenCalled()
})
})
5 changes: 2 additions & 3 deletions frontend/src/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ const routes = [
component: ContactView
},
{
path: '/gene/:searchTerm',
path: '/gene/:searchTerm/:genomeRelease',
name: 'gene',
component: GeneDetailsView,
props: (route: any) => {
// remark: the following line is not covered because we stub out the router
return { searchTerm: route.params.searchTerm }
return { searchTerm: route.params.searchTerm, genomeRelease: route.params.genomeRelease }
}
}
]
Expand Down
12 changes: 9 additions & 3 deletions frontend/src/views/GeneDetailView.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { watch, onMounted } from 'vue'
import { watch, onMounted, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { StoreState, useGeneInfoStore } from '@/stores/geneInfo'
Expand All @@ -9,10 +9,12 @@ import { roundIt } from '@/api/utils'
export interface Props {
searchTerm?: string
genomeRelease?: string
}
const props = withDefaults(defineProps<Props>(), {
searchTerm: ''
searchTerm: '',
genomeRelease: 'grch37'
})
const router = useRouter()
Expand Down Expand Up @@ -62,10 +64,14 @@ const SECTIONS = [
{ id: 'gene-rifs', title: 'Gene RIFs' },
{ id: 'locus-specific-databases', title: 'Locus-Specific Databases' }
]
// We need to use refs here because of props mutations in the parent
const searchTermRef = ref(props.searchTerm)
const genomeReleaseRef = ref(props.genomeRelease)
</script>

<template>
<HeaderDetailPage />
<HeaderDetailPage v-model:search-term="searchTermRef" v-model:genome-release="genomeReleaseRef" />
<v-navigation-drawer location="right" class="overflow-auto">
<div v-if="geneInfoStore.storeState == StoreState.Active" class="gene-info">
<v-list density="compact" nav>
Expand Down
34 changes: 6 additions & 28 deletions frontend/src/views/HomeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { useRouter } from 'vue-router'
import HeaderDefault from '@/components/HeaderDefault.vue'
import SearchBar from '@/components/SearchBar.vue'
import { search } from '@/api/utils'
const router = useRouter()
Expand All @@ -29,35 +30,12 @@ const useExample = (example: string) => {
searchTerm.value = example
}
interface RouteLocationFragment {
name: string
params?: any
}
type RouteLoctionBuilder = () => RouteLocationFragment
// We iterate the regexps in the `Map` and will use the route from the
// first match.
const SEARCH_REGEXPS: [RegExp, RouteLoctionBuilder][] = [
[
/^.*$/,
(): RouteLocationFragment => ({
name: 'gene',
params: {
searchTerm: searchTerm.value
}
})
]
]
const performSearch = async () => {
for (const [regexp, getRoute] of SEARCH_REGEXPS) {
if (regexp.test(searchTerm.value)) {
const routeLocation = getRoute()
console.log(`term ${searchTerm.value} matched ${regexp}, route is`, routeLocation)
router.push(routeLocation)
return
}
const routeLocation: any = search(searchTerm.value, genomeRelease.value)
if (routeLocation) {
router.push(routeLocation)
} else {
console.error(`no route found for ${searchTerm.value}`)
}
}
</script>
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/views/__tests__/HomeView.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ describe('HomeView with mocked router', async () => {
await nextTick()

expect(router.push).toHaveBeenCalledOnce()
expect(router.push).toHaveBeenCalledWith({ name: 'gene', params: { searchTerm: 'HGNC:1100' } })
expect(router.push).toHaveBeenCalledWith({
name: 'gene',
params: { searchTerm: 'HGNC:1100', genomeRelease: 'grch37' }
})
})
})

0 comments on commit e021214

Please sign in to comment.