Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance search for sorting and filters #134

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const SORT_OPTIONS = [
]

export const FILTER_OPTIONS = [
{ key: "title", label: "Title" },
{ key: "author", label: "Author" },
{ key: "tag", label: "Tags" }
]
32 changes: 25 additions & 7 deletions src/views/HomeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
id="search"
class="home-view--search--input"
v-model="search"
placeholder="Search..."
:placeholder="getSearchPlaceHolder"
@keyup.enter="makeSearch"
>
<div class="home-view--search--actions">
Expand Down Expand Up @@ -107,7 +107,7 @@
</div>
</template>
<script setup>
import { ref, watch, onMounted } from "vue"
import { ref, watch, onMounted, computed } from "vue"
import { useRouter } from "vue-router"
import BlogPeek from "../components/BlogPeek"
import DropMenu from "../components/DropMenu"
Expand All @@ -126,12 +126,20 @@ const loading = ref(false)
const filterBy = ref(null)
const sortBy = ref(null)
const homeViewMode = ref(null)
const searchPlaceHolder = ref("")

onMounted(async () => {
init()
tracker.trackPageView()
})

const getSearchPlaceHolder = computed(() => {
if (currentRoute.value.params.filterBy) {
return "search by " + searchPlaceHolder.value
}
return "search"
})

const init = () => {
loading.value = true
filterBy.value = currentRoute.value.params.filterBy
Expand Down Expand Up @@ -171,14 +179,22 @@ const makeSearch = () => {
searchResultBlog.value = []
if (filterBy.value) {
peekData.value.forEach(item => {
if (filterBy.value === "tag") {
switch (filterBy.value) {
case "tag" :
if (item.tags && item.tags.join(" ").toLowerCase().includes(searchVal)) {
searchResultBlog.value.push(item)
}
} else {
if (item.authorName.toLowerCase().includes(searchVal)) {
break
case "author" :
if (item.tags && item.tags.join(" ").toLowerCase().includes(searchVal)) {
searchResultBlog.value.push(item)
}
break
case "title":
if (item.title.toLowerCase().includes(searchVal)) {
searchResultBlog.value.push(item)
}
break
}
SagarGi marked this conversation as resolved.
Show resolved Hide resolved
})
} else if (sortBy.value) {
Expand All @@ -188,8 +204,9 @@ const makeSearch = () => {
}
})
} else {
// by default user can search with any of the options (author, title, tags)
peekData.value.forEach(item => {
if (item.title.toLowerCase().includes(searchVal)) {
if ((item.tags && item.tags.join(" ").toLowerCase().includes(searchVal)) || (item.authorName.toLowerCase().includes(searchVal)) || (item.title.toLowerCase().includes(searchVal))) {
koebel marked this conversation as resolved.
Show resolved Hide resolved
searchResultBlog.value.push(item)
}
})
Expand All @@ -206,7 +223,7 @@ const toHome = () => {
.then(() => {
peekData.value = getPeekData()
sortPeekData(sortBy.value)
search.value = ""
clearSearch()
})
}

Expand All @@ -231,6 +248,7 @@ const setFilterKey = (key) => {
if (currentRoute.value.params.filterBy === key) {
toHome()
} else {
searchPlaceHolder.value = key
push({
name: "Filter",
params: {
Expand Down
Loading