Add view action

master
Guillaume Dott 2015-10-30 12:48:47 +01:00
parent 75e122b00d
commit 5abef4cef7
5 changed files with 48 additions and 1 deletions

View File

@ -53,5 +53,15 @@ module Librarix
redirect to('/') redirect to('/')
end end
end end
post '/view' do
Librarix::Redis::Movie.new(params[:id]).view
if request.xhr?
""
else
redirect to('/')
end
end
end end
end end

View File

@ -6,6 +6,14 @@ function initButtons() {
removeMovie(this.previousElementSibling.value) removeMovie(this.previousElementSibling.value)
}); });
} }
var view_buttons = document.querySelectorAll('button[data-action=view-movie]');
for (var i = 0; i < view_buttons.length; i++) {
view_buttons[i].addEventListener('click', function(event) {
event.preventDefault();
viewMovie(this.previousElementSibling.value)
});
}
} }
function initSearch() { function initSearch() {
@ -56,6 +64,21 @@ function removeMovie(id) {
req.send(data) req.send(data)
} }
function viewMovie(id) {
var data = new FormData();
data.append('id', id);
var req = new XMLHttpRequest();
req.open('POST', '/view');
req.setRequestHeader("X-Requested-With", "XMLHttpRequest");
req.addEventListener('load', function(e) {
document.querySelector('.movie[data-id="' + id + '"] button[data-action="view-movie"]').parentNode.remove();
});
req.send(data)
}
document.addEventListener('DOMContentLoaded', function(event) { document.addEventListener('DOMContentLoaded', function(event) {
initButtons(); initButtons();

View File

@ -38,6 +38,11 @@ module Librarix
end end
def view def view
Librarix.redis.sadd('viewed_movies_id', id)
end
def viewed?
Librarix.redis.sismember('viewed_movies_id', id)
end end
end end
end end

View File

@ -26,6 +26,10 @@ module Librarix
def added? def added?
Librarix::Redis::Movie.new(id).added? Librarix::Redis::Movie.new(id).added?
end end
def viewed?
Librarix::Redis::Movie.new(id).viewed?
end
end end
end end
end end

View File

@ -4,10 +4,15 @@
img src="#{poster_url(movie.poster_path, 'w154')}" img src="#{poster_url(movie.poster_path, 'w154')}"
.informations .informations
h2 h2
a href="https://www.themoviedb.org/movie/#{movie.id}" #{movie.original_title} (#{movie.release_year}) a href="https://www.themoviedb.org/movie/#{movie.id}" #{movie.original_title}
p = movie.release_date
p = movie.overview p = movie.overview
.actions .actions
- if movie.added? - if movie.added?
- unless movie.viewed?
form method="post" action="/view"
input type="hidden" name="id" value="#{movie.id}"
button type="submit" data-action="view-movie" View
form method="post" action="/remove" form method="post" action="/remove"
input type="hidden" name="id" value="#{movie.id}" input type="hidden" name="id" value="#{movie.id}"
button type="submit" data-action="remove-movie" Remove button type="submit" data-action="remove-movie" Remove