Skip to content

Commit

Permalink
search posts feature (only in the general context for now)
Browse files Browse the repository at this point in the history
  • Loading branch information
tempe-techie committed Mar 5, 2024
1 parent 1007afb commit 8f29214
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 2 deletions.
112 changes: 112 additions & 0 deletions components/search/SearchPosts.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<template>
<div>
<div class="input-group mb-3">
<input
type="text"
class="form-control text-center"
placeholder="enter a search term"
v-model="queryTerm"
v-on:keyup.enter="search"
>
</div>

<div class="text-center">
<button class="btn btn-primary mt-2 mb-4" :disabled="!queryTerm || waitingLoadPosts" @click="search">
Search
</button>
</div>

<div v-if="orbisPosts">
<ChatPost
v-for="post in orbisPosts"
:key="post.stream_id"
:post="post"
:orbisContext="selectedContext" />
</div>

<div class="d-flex justify-content-center mt-5 mb-4" v-if="searched && queryTerm && orbisPosts.length === 0 && !waitingLoadPosts">
<p>No posts. Try a different query.</p>
</div>

<div class="d-flex justify-content-center mb-3" v-if="waitingLoadPosts">
<span class="spinner-border spinner-border-lg" role="status" aria-hidden="true"></span>
</div>

<div class="d-grid gap-2 col-6 mx-auto mb-5" v-if="showLoadMore">
<button class="btn btn-primary" type="button" @click="search">Load more posts</button>
</div>
</div>
</template>

<script>
import ChatPost from "~/components/chat/ChatPost.vue";
import { useToast } from "vue-toastification/dist/index.mjs";
export default {
name: "SearchPosts",
data() {
return {
orbisPosts: [],
pageCounter: 0,
queryTerm: null,
searched: false,
selectedContext: this.$config.chatChannels.general,
showLoadMore: false,
waitingLoadPosts: false
}
},
components: {
ChatPost
},
methods: {
async search() {
this.waitingLoadPosts = true;
let {data, error} = await this.$orbis.getPosts(
{context: this.selectedContext, term: this.queryTerm},
this.pageCounter,
this.$config.getPostsLimit, // posts limit
false // ascending=true, descending=false
);
if (error) {
this.toast("Error fetching posts from the Orbis/Ceramic node.", {type: "error"});
console.log(error);
}
if (data.length < this.$config.getPostsLimit) {
this.showLoadMore = false; // hide Load More Posts button if there's less than getPostsLimit number of posts received
} else if (data.length === this.$config.getPostsLimit) {
this.showLoadMore = true; // show Load More Posts button if data length was full (getPostsLimit number of posts)
}
this.orbisPosts.push(...data);
this.pageCounter++;
this.waitingLoadPosts = false;
this.searched = true;
}
},
setup() {
const toast = useToast();
return { toast }
},
watch: {
// if queryTerm changes, clear the orbisPosts array
queryTerm(newVal, oldVal) {
if (newVal !== oldVal) {
this.orbisPosts = [];
this.pageCounter = 0;
this.searched = false;
}
}
}
}
</script>
11 changes: 9 additions & 2 deletions components/sidebars/SidebarLeft.vue
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,14 @@
<!-- Send tokens -->
<li class="nav-item p-1" @click="closeLeftSidebar" v-if="$config.showFeatures.sendTokens">
<NuxtLink class="nav-link" :class="$route.path.startsWith('/send-tokens') ? 'active' : ''" aria-current="page" to="/send-tokens">
<i class="bi bi-send"></i> Send tokens
<i class="bi bi-send"></i> Send Tokens
</NuxtLink>
</li>

<!-- Search Posts -->
<li class="nav-item p-1" @click="closeLeftSidebar" v-if="$config.showFeatures.searchPosts">
<NuxtLink class="nav-link" :class="$route.path.startsWith('/search-posts') ? 'active' : ''" aria-current="page" to="/search-posts">
<i class="bi bi-search"></i> Search Posts
</NuxtLink>
</li>

Expand Down Expand Up @@ -172,7 +179,7 @@
<!-- Find User -->
<li class="nav-item p-1" @click="closeLeftSidebar">
<NuxtLink class="nav-link" :class="$route.path.startsWith('/find-user') ? 'active' : ''" aria-current="page" to="/find-user">
<i class="bi bi-search"></i> Find User
<i class="bi bi-binoculars"></i> Find User
</NuxtLink>
</li>

Expand Down
1 change: 1 addition & 0 deletions nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export default defineNuxtConfig({
"newsletter": false,
"nftLaunchpad": true,
"randomMintedPosts": true,
"searchPosts": true,
"swap": true,
"stake": false,
"sendTokens": true,
Expand Down
37 changes: 37 additions & 0 deletions pages/search-posts.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<template>
<Head>
<Title>Search posts | {{ $config.projectMetadataTitle }}</Title>
<Meta property="og:title" :content="'Search posts | ' + $config.projectMetadataTitle" />
</Head>

<div class="scroll-500">
<div class="card border">
<div class="card-body mb-3">
<p class="fs-3">
<i class="bi bi-arrow-left-circle cursor-pointer" @click="$router.back()"></i>
</p>

<h3 class="mt-3">Search posts</h3>

<div class="d-flex justify-content-center mt-5">
<div class="col-12 col-lg-8">
<SearchPosts />
</div>
</div>

</div>
</div>
</div>
</template>

<script>
import SearchPosts from '~/components/search/SearchPosts.vue';
export default {
name: 'SearchPostsPage',
components: {
SearchPosts,
}
}
</script>

0 comments on commit 8f29214

Please sign in to comment.