mirror of
https://github.com/neocities/neocities.git
synced 2025-04-25 01:32:36 +02:00
following button on site
This commit is contained in:
parent
51a85a0b03
commit
0a29a496d7
5 changed files with 62 additions and 18 deletions
7
app.rb
7
app.rb
|
@ -205,6 +205,13 @@ get '/site/:username/tip' do |username|
|
||||||
erb :'tip'
|
erb :'tip'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
post '/site/:site_id/toggle_follow' do |site_id|
|
||||||
|
require_login
|
||||||
|
content_type :json
|
||||||
|
site = Site[id: site_id]
|
||||||
|
{result: (current_site.toggle_follow(site) ? 'followed' : 'unfollowed')}.to_json
|
||||||
|
end
|
||||||
|
|
||||||
get '/browse' do
|
get '/browse' do
|
||||||
@current_page = params[:current_page]
|
@current_page = params[:current_page]
|
||||||
@current_page = @current_page.to_i
|
@current_page = @current_page.to_i
|
||||||
|
|
|
@ -65,7 +65,10 @@ class Site < Sequel::Model
|
||||||
one_to_many :profile_comments
|
one_to_many :profile_comments
|
||||||
one_to_many :profile_commentings, key: :actioning_site_id, class: :ProfileComment
|
one_to_many :profile_commentings, key: :actioning_site_id, class: :ProfileComment
|
||||||
|
|
||||||
|
# Who is following this site
|
||||||
one_to_many :follows
|
one_to_many :follows
|
||||||
|
|
||||||
|
# Who this site is following
|
||||||
one_to_many :followings, key: :actioning_site_id, class: :Follow
|
one_to_many :followings, key: :actioning_site_id, class: :Follow
|
||||||
|
|
||||||
one_to_many :tips
|
one_to_many :tips
|
||||||
|
@ -96,6 +99,20 @@ class Site < Sequel::Model
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def is_following?(site)
|
||||||
|
followings_dataset.select(:id).filter(site_id: site.id).first ? true : false
|
||||||
|
end
|
||||||
|
|
||||||
|
def toggle_follow(site)
|
||||||
|
if is_following? site
|
||||||
|
followings_dataset.filter(site_id: site.id).delete
|
||||||
|
false
|
||||||
|
else
|
||||||
|
add_following site_id: site.id
|
||||||
|
true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def tip_amount
|
def tip_amount
|
||||||
return '0.00' if tips_dataset.count == 0
|
return '0.00' if tips_dataset.count == 0
|
||||||
'31.337'
|
'31.337'
|
||||||
|
|
16
public/assets/scripts/news/site.js
Normal file
16
public/assets/scripts/news/site.js
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
var Site = {
|
||||||
|
toggleFollow: function(siteId, csrfToken) {
|
||||||
|
var link = $('a#followLink')
|
||||||
|
var span = $('a#followLink span')
|
||||||
|
$.post('/site/'+siteId+'/toggle_follow', {csrf_token: csrfToken}, function(res) {
|
||||||
|
console.log(res)
|
||||||
|
if(res.result == "followed") {
|
||||||
|
span.text('Unfollow')
|
||||||
|
link.removeClass('follow')
|
||||||
|
} else if(res.result == 'unfollowed') {
|
||||||
|
span.text('Follow')
|
||||||
|
link.addClass('follow')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,6 +3,8 @@
|
||||||
<script src="/assets/scripts/news/comment.js"></script>
|
<script src="/assets/scripts/news/comment.js"></script>
|
||||||
<script src="/assets/scripts/news/profile_comment.js"></script>
|
<script src="/assets/scripts/news/profile_comment.js"></script>
|
||||||
<script src="/assets/scripts/news/event.js"></script>
|
<script src="/assets/scripts/news/event.js"></script>
|
||||||
|
<script src="/assets/scripts/news/site.js"></script>
|
||||||
|
|
||||||
<% events.each do |event| %>
|
<% events.each do |event| %>
|
||||||
<% if event.profile_comment_id %>
|
<% if event.profile_comment_id %>
|
||||||
<div class="news-item comment for-me" id="event_<%= event.id %>">
|
<div class="news-item comment for-me" id="event_<%= event.id %>">
|
||||||
|
|
|
@ -16,9 +16,11 @@
|
||||||
<div class="stat tips"><strong><%= site.tips_dataset.count %></strong> <span>tips</span></div>
|
<div class="stat tips"><strong><%= site.tips_dataset.count %></strong> <span>tips</span></div>
|
||||||
</div>
|
</div>
|
||||||
<div class="actions">
|
<div class="actions">
|
||||||
<% if site != current_site %>
|
<% if current_site && current_site != site %>
|
||||||
<a href="" class="btn-Action follow"><span>Follow</span></a>
|
<a id="followLink" href="#" onclick="Site.toggleFollow(<%= site.id %>, '<%= csrf_token %>')" class="btn-Action <%= current_site.is_following?(site) ? '' : 'follow' %>">
|
||||||
<a href="" class="btn-Action tip"><span>Tip</span></a>
|
<span><%= current_site.is_following?(site) ? 'Unfollow' : 'Follow' %></span>
|
||||||
|
</a>
|
||||||
|
<a href="#" class="btn-Action tip"><span>Tip</span></a>
|
||||||
<% end %>
|
<% end %>
|
||||||
<a href="" class="btn-Action share"><span>Share</span></a>
|
<a href="" class="btn-Action share"><span>Share</span></a>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Add table
Reference in a new issue