Skip to content

Commit b8661a6

Browse files
committed
feature/Make snippets on user profile searchable
1 parent 92455ea commit b8661a6

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

app/controllers/snippets_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ class SnippetsController < ApplicationController
66
UNFILE_CONFIRM_TEXT = "Are you sure you want to unfile this snippet? It will be removed from your collection.".freeze
77

88
def index
9+
@user = User.find_by(id: params[:user_id]) || current_user
910
@display_popover = true
10-
@snippets = current_user.filed_snippets.includes(:user)
11+
@snippets = @user.filed_snippets.includes(:user)
1112

1213
# TODO: Extract this logic to model/service
1314
@snippets = @snippets.where('description ILIKE ?', "%#{params[:search]}%") if params[:search]

app/controllers/users_controller.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ def show
55
@is_following = current_user ? current_user.following?(@user) : false
66
@followers = @user.followers
77
@following = @user.following
8+
@display_popover = true
9+
@snippets = @user.filed_snippets
10+
.order(created_at: :desc)
11+
.paginate(page: 1, per_page: 6)
812
end
913

1014
def hovercard

app/views/users/show.html.erb

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<div></div>
1111
<div class="tabs--headers-wrapper">
1212
<div data-target="tabs.tab" data-action="click->tabs#change" class="tabs--header tabs--header-active">
13-
<span><%= @user.snippets.size %> Snippets</span>
13+
<span><%= @user.filed_snippets.size %> Snippets</span>
1414
</div>
1515
<div data-target="tabs.tab" data-action="click->tabs#change" class="tabs--header">
1616
<span><%= @followers.size %> Followers</span>
@@ -23,12 +23,32 @@
2323

2424
<div class="tabs-details">
2525
<div data-target="tabs.panel">
26-
<% @user.snippets.order(created_at: :desc).each do |snippet| %>
27-
<snippet-preview
28-
:key="<%= snippet.id %>"
29-
:snippet="<%= snippet.serialize(current_user).to_json %>">
30-
</snippet-preview>
31-
<% end %>
26+
<div data-controller="infinite-scroll" data-infinite-scroll-base-url="<%= user_snippets_path(@user) %>">
27+
<div style="display: flex; align-items: center; justify-content: space-between;" class="margin-top">
28+
29+
<div class="searchbar">
30+
<input autofocus
31+
type="text"
32+
placeholder="Search"
33+
data-action="input->infinite-scroll#search"
34+
data-target="infinite-scroll.input"
35+
/>
36+
<img src="/icons/search.svg">
37+
</div>
38+
39+
</div>
40+
41+
<div class="margin-top"
42+
data-target="infinite-scroll.entries"
43+
data-action="scroll@window->infinite-scroll#scroll"
44+
>
45+
<%= render 'snippets/snippets' %>
46+
</div>
47+
48+
<div data-target="infinite-scroll.pagination" class="hidden">
49+
<%= will_paginate @snippets, container: false, params: { controller: "snippets", action: "index" } %>
50+
</div>
51+
</div>
3252
</div>
3353

3454
<div data-target="tabs.panel" class="hidden">

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
post :follow, on: :member
4444
post :unfollow, on: :member
4545
get :hovercard, on: :member
46+
47+
resources :snippets, only: :index
4648
end
4749

4850
resources :likes, only: :create

0 commit comments

Comments
 (0)