Skip to content
Snippets Groups Projects
Commit 7bebc3aa authored by gltron's avatar gltron
Browse files

Added pagination & improved search logic

parent 25c6bca3
Branches
No related tags found
No related merge requests found
<template> <template>
<div class="comments"> <div class="comments" v-if="comments">
<h1 class="title is-5">{{comments.length}} commentaires</h1> <h1 class="title is-5">{{comments.length}} commentaires</h1>
<article class="media" v-for="comment in comments" v-bind:key="comment.id"> <article class="media" v-for="comment in comments" v-bind:key="comment.id">
<div class="media-content"> <div class="media-content">
...@@ -12,35 +12,13 @@ ...@@ -12,35 +12,13 @@
</div> </div>
</div> </div>
</article> </article>
<b-loading :is-full-page="false" :active.sync="isLoading"/>
</div> </div>
</template> </template>
<script> <script>
export default { export default {
name: 'Comments', name: 'Comments',
props: ['id'], props: ['comments']
data () {
return {
comments: '',
isLoading: true
}
},
async mounted () {
this.comments = await this.getComments(this.id)
this.isLoading = false
},
methods: {
async getComments (id) {
const url = this.$serverurl + 'comment' + '?id=' + id
const response = await fetch(url)
try {
return await response.json()
} catch (error) {
return null
}
}
}
} }
</script> </script>
......
...@@ -27,7 +27,7 @@ export default { ...@@ -27,7 +27,7 @@ export default {
props: ['model'], props: ['model'],
methods: { methods: {
onClick () { onClick () {
this.$router.push({ name: 'Model', params: { model: this.model } }) this.$router.push({ name: 'Model', query: { id: this.model.id } })
} }
} }
} }
......
...@@ -55,7 +55,7 @@ export default { ...@@ -55,7 +55,7 @@ export default {
}, },
methods: { methods: {
onSearch () { onSearch () {
this.$router.push({ name: 'Search', query: { n: this.search } }) this.$router.push({ name: 'Search', query: { n: this.search }, force: true })
} }
} }
} }
......
...@@ -28,8 +28,7 @@ const routes = [ ...@@ -28,8 +28,7 @@ const routes = [
{ {
path: '/model', path: '/model',
name: 'Model', name: 'Model',
component: () => import('../views/Model.vue'), component: () => import('../views/Model.vue')
props: true
} }
] ]
......
<template> <template>
<div class="model container"> <div class="model container">
<div v-if="isError" class="error">
<h1 class="title is-1">Erreur</h1>
<h2 class="subtitle">Modèle introuvable</h2>
</div>
<div v-else>
<div class="columns box"> <div class="columns box">
<div class="column is-two-thirds"> <div class="column is-two-thirds">
<h1 class="title is-1">{{model.name}}</h1> <h1 class="title is-1">{{model.name}}</h1>
...@@ -27,8 +32,10 @@ ...@@ -27,8 +32,10 @@
{{model.longDescription}} {{model.longDescription}}
</div> </div>
<div class="box"> <div class="box">
<Comments v-bind:id="model.id"/> <Comments v-bind:comments="model.comments"/>
</div>
</div> </div>
<b-loading :is-full-page="false" :active.sync="isLoading"/>
</div> </div>
</template> </template>
...@@ -40,7 +47,30 @@ export default { ...@@ -40,7 +47,30 @@ export default {
components: { components: {
Comments Comments
}, },
props: ['model'] data () {
return {
model: '',
isError: false,
isLoading: true
}
},
async mounted () {
const id = this.$route.query.id
this.model = await this.getModel(id)
if (this.model == null) this.isError = true
this.isLoading = false
},
methods: {
async getModel (id) {
const url = this.$serverurl + 'models' + '?id=' + id
const response = await fetch(url)
try {
return await response.json()
} catch (error) {
return null
}
}
}
} }
</script> </script>
...@@ -48,4 +78,8 @@ export default { ...@@ -48,4 +78,8 @@ export default {
.model { .model {
padding: 25px; padding: 25px;
} }
.error {
margin-top: 50px;
text-align: center;
}
</style> </style>
...@@ -2,12 +2,15 @@ ...@@ -2,12 +2,15 @@
<div class="search container"> <div class="search container">
<div class="columns"> <div class="columns">
<div class="column"> <div class="column">
<Filters v-bind:tags="tags" v-on:setTags="getModels"/> <h1 class="title is-6 filterTitle">Filtres</h1>
<Filters v-bind:tags="tags" v-on:setTags="refreshResults"/>
</div> </div>
<div v-if="models" class="column is-four-fifths"> <div v-if="result" class="column is-four-fifths">
<div v-for="model in models" v-bind:key="model.name"> <h1 class="title is-6">{{result.total}} résultats - page {{result.page}}</h1>
<div v-for="model in result.models" v-bind:key="model.name">
<ModelCard v-bind:model="model"/> <ModelCard v-bind:model="model"/>
</div> </div>
<b-pagination :total="result.total" :current.sync="result.page" :per-page="resultSize" @change="changePage"/>
</div> </div>
<div v-else class="column is-four-fifths"> <div v-else class="column is-four-fifths">
<h1 class="title">no modelo</h1> <h1 class="title">no modelo</h1>
...@@ -30,9 +33,10 @@ export default { ...@@ -30,9 +33,10 @@ export default {
data () { data () {
return { return {
tags: '', tags: '',
models: true, result: true,
paramTags: '', paramTags: '',
paramSearch: '', paramSearch: '',
paramPerf: '',
resultSize: 10, resultSize: 10,
page: 1, page: 1,
isLoading: true isLoading: true
...@@ -40,21 +44,18 @@ export default { ...@@ -40,21 +44,18 @@ export default {
}, },
async mounted () { async mounted () {
this.tags = await this.getTags() this.tags = await this.getTags()
this.models = await this.getModels() this.result = await this.getResult()
this.isLoading = false this.isLoading = false
}, },
methods: { methods: {
setParams (tags, perfValue) { async changePage (page) {
const searchParam = this.$route.query.n this.page = page
var tagsParam this.result = await this.getResult()
if (tags) tagsParam = tags },
else tagsParam = this.$route.query.t async setTags (tags, perfValue) {
this.paramTags = tags
var perfParam this.paramPerf = perfValue
if (perfValue) perfParam = perfValue this.result = await this.getResult()
else perfParam = this.$route.query.p
this.$router.push({ name: 'Search', query: { n: searchParam, t: tagsParam, p: perfParam } })
}, },
async getTags () { async getTags () {
const url = this.$serverurl + 'tags' const url = this.$serverurl + 'tags'
...@@ -65,22 +66,19 @@ export default { ...@@ -65,22 +66,19 @@ export default {
return null return null
} }
}, },
async getModels (tags, perfValue) { async getResult () {
this.setParams(tags, perfValue)
const url = new URL(this.$serverurl + 'search') const url = new URL(this.$serverurl + 'search')
const params = new URLSearchParams() const params = new URLSearchParams()
const nameParam = this.$route.query.n const nameParam = this.$route.query.n
const tagsParam = this.$route.query.t
const perfParam = this.$route.query.p
if (nameParam) params.append('n', nameParam) if (nameParam != null) params.append('q', nameParam)
if (tagsParam) params.append('t', tagsParam) if (this.paramTags != null) params.append('tag', this.paramTags)
if (perfParam) params.append('p', perfParam) if (this.paramPerf != null) params.append('perf', this.paramPerf)
params.append('size', this.resultSize) params.append('size', this.resultSize)
params.append('page', this.page) params.append('page', this.page)
console.log('get models ' + nameParam + ' + ' + tags) console.log('get models ' + nameParam + ' + ' + this.paramPerf + ' + ' + this.paramTags)
url.search = params url.search = params
const response = await fetch(url) const response = await fetch(url)
...@@ -98,4 +96,8 @@ export default { ...@@ -98,4 +96,8 @@ export default {
.search { .search {
padding-top: 50px; padding-top: 50px;
} }
.filterTitle {
margin-left: 15px;
}
</style> </style>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment