Skip to content

Commit f4e814d

Browse files
committed
feature/Add ability to follow users from connect view
1 parent d8619b5 commit f4e814d

File tree

9 files changed

+84
-41
lines changed

9 files changed

+84
-41
lines changed

app/controllers/home_controller.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
class HomeController < ApplicationController
2-
before_action :authenticate_user!, only: :connect
3-
42
def index
53
@display_popover = true
64

@@ -31,7 +29,12 @@ def index
3129

3230
def connect
3331
@page_title = 'Connect'
34-
@users = current_user.not_following
32+
33+
@users = if user_signed_in?
34+
current_user.not_following
35+
else
36+
User.order(updated_at: :desc)
37+
end
3538

3639
# @users = current_user.not_following
3740

app/controllers/users_controller.rb

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,25 +61,17 @@ def modify
6161
def follow
6262
@user = User.find_by(id: params[:id])
6363

64-
if current_user.follow(@user)
65-
flash[:notice] = "You are now following #{@user.name}."
66-
else
67-
flash[:alert] = "You are already following #{@user.name} you nutter!"
68-
end
69-
70-
redirect_to user_path(@user)
64+
current_user.follow(@user)
65+
66+
render partial: 'users/follow_button', locals: { user: @user }
7167
end
7268

7369
def unfollow
7470
@user = User.find_by(id: params[:id])
7571

76-
if current_user.unfollow(@user)
77-
flash[:notice] = "You are no longer following #{@user.name}."
78-
else
79-
flash[:alert] = "You can't unfollow someone you aren't following you nutter!"
80-
end
72+
current_user.unfollow(@user)
8173

82-
redirect_to user_path(@user)
74+
render partial: 'users/follow_button', locals: { user: @user }
8375
end
8476

8577
private
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Controller } from 'stimulus';
2+
import axios from 'axios';
3+
4+
export default class extends Controller {
5+
static targets = ['container'];
6+
static values = {
7+
id: String
8+
};
9+
10+
follow() {
11+
axios.post(this.followUrl)
12+
.then(res => {
13+
this.containerTarget.innerHTML = res.data
14+
})
15+
.catch(console.error)
16+
}
17+
18+
unfollow() {
19+
axios.post(this.unfollowUrl)
20+
.then(res => {
21+
this.containerTarget.innerHTML = res.data
22+
})
23+
.catch(console.error)
24+
}
25+
26+
get followUrl() {
27+
return `/users/${this.idValue}/follow`
28+
}
29+
30+
get unfollowUrl() {
31+
return `/users/${this.idValue}/unfollow`
32+
}
33+
}

app/views/shared/_connect.html.erb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<% if users_for_connect.any? %>
2+
<div class="card--container card--container-padding-horizontal" data-controller="list" data-list-url-value="<%= users_path(limit: 5) %>">
3+
<h3 class="text-center no-margin-top margin-bottom">Connect</h3>
4+
<% users_for_connect.each do |user| %>
5+
<%= render partial: 'users/connect', locals: { user: user } %>
6+
<% end %>
7+
<div class="flex flex-end">
8+
<%= link_to 'More...', connect_path, class: 'padding-right light' %>
9+
</div>
10+
</div>
11+
<% end %>

app/views/shared/_sidebar.html.erb

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,11 @@
2727
</div>
2828
<% end %>
2929

30-
<div class="card--container card--container-padding" style="text-align: center;">
31-
<%= link_to(@modal_url, data: { controller: 'modal', action: 'modal#present', modal_url_value: @modal_url }, class: 'sidebar--new-snippet') do %>
32-
<span>NEW SNIPPET</span>
33-
<% end %>
34-
</div>
30+
<div class="card--container card--container-padding" style="text-align: center;">
31+
<%= link_to(@modal_url, data: { controller: 'modal', action: 'modal#present', modal_url_value: @modal_url }, class: 'sidebar--new-snippet') do %>
32+
<span>NEW SNIPPET</span>
33+
<% end %>
34+
</div>
3535

36-
<div class="card--container card--container-padding-horizontal" data-controller="list" data-list-url-value="<%= users_path(limit: 5) %>">
37-
<h3 class="text-center no-margin-top margin-bottom">Connect</h3>
38-
<% @users_for_connect.each do |user| %>
39-
<%= render partial: 'users/connect', locals: { user: user } %>
40-
<% end %>
41-
<div class="flex flex-end">
42-
<%= link_to 'More...', connect_path, class: 'padding-right light' %>
43-
</div>
44-
</div>
36+
<%= render partial: 'shared/connect', locals: { users_for_connect: @users_for_connect }%>
4537
</div>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<div data-controller="follow" data-follow-target="container" data-follow-id-value="<%= user.id %>">
2+
<% if user_signed_in? && current_user == user %>
3+
<%# Don't display anything if the current user is the same as the user being viewed %>
4+
<% elsif user_signed_in? %>
5+
<% if current_user.following?(user) %>
6+
<%= button_tag 'FOLLOWING', data: { action: 'click->follow#unfollow' }, class: 'button--cta-unfollow' %>
7+
<% else %>
8+
<%= button_tag 'FOLLOW', data: { action: 'click->follow#follow' }, class: 'button--cta-follow' %>
9+
<% end %>
10+
<% else %>
11+
<%= button_tag 'FOLLOW', type: :button, class: 'button--cta-follow', data: { controller: 'modal', modal_url_value: sign_in_sign_up_modals_users_path, action: "click->modal#present" } %>
12+
<% end %>
13+
</div>
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
<div class="card--container card--container-padding">
2-
<%= render partial: 'users/preview', locals: { user: user } %>
2+
<div class="flex space-between">
3+
<%= render partial: 'users/preview', locals: { user: user } %>
4+
<div>
5+
<%= render partial: 'users/follow_button', locals: { user: user } %>
6+
</div>
7+
</div>
38
<span class="follow--description"><%= user.bio %></span>
49
</div>

app/views/users/_preview.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<span class="user-preview--summary-name"><%= user.name %></span>
2323
<% end %>
2424
</div>
25-
<% if defined?(resource) %>
25+
<% if defined?(resource) && resource.present? %>
2626
<span class="user-preview--summary-time-ago"><%= time_ago_in_words(resource.created_at) %> ago</span>
2727
<% else %>
2828
<span class="user-preview--summary-time-ago"><%= user.name %></span>

app/views/users/show.html.erb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,10 @@
1616
</div>
1717

1818
<div class="margin-top flex space-between">
19-
<% if user_signed_in? %>
20-
<% if @user == current_user %>
21-
<%= button_to 'EDIT PROFILE', edit_user_registration_path, method: :get, class: 'button--cta-follow' %>
22-
<% elsif current_user.following?(@user) %>
23-
<%= button_to 'FOLLOWING', unfollow_user_path(@user), class: 'button--cta-unfollow' %>
24-
<% else %>
25-
<%= button_to 'FOLLOW', follow_user_path(@user), class: 'button--cta-follow' %>
26-
<% end %>
19+
<% if @user == current_user %>
20+
<%= button_to 'EDIT PROFILE', edit_user_registration_path, method: :get, class: 'button--cta-follow' %>
2721
<% else %>
28-
<%= button_tag('FOLLOW', type: :button, class: 'button--cta-follow', data: { controller: 'modal', modal_url_value: sign_in_sign_up_modals_users_path, action: "click->modal#present" }) %>
22+
<%= render partial: 'users/follow_button', locals: { user: @user } %>
2923
<% end %>
3024
</div>
3125
</div>

0 commit comments

Comments
 (0)