diff --git a/lib/librarix/application.rb b/lib/librarix/application.rb index c9e55eb..519ec9c 100644 --- a/lib/librarix/application.rb +++ b/lib/librarix/application.rb @@ -53,5 +53,15 @@ module Librarix redirect to('/') end end + + post '/view' do + Librarix::Redis::Movie.new(params[:id]).view + + if request.xhr? + "" + else + redirect to('/') + end + end end end diff --git a/lib/librarix/public/application.js b/lib/librarix/public/application.js index 1c88a66..322d72d 100644 --- a/lib/librarix/public/application.js +++ b/lib/librarix/public/application.js @@ -6,6 +6,14 @@ function initButtons() { 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() { @@ -56,6 +64,21 @@ function removeMovie(id) { 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) { initButtons(); diff --git a/lib/librarix/redis/movie.rb b/lib/librarix/redis/movie.rb index 42305b7..4913462 100644 --- a/lib/librarix/redis/movie.rb +++ b/lib/librarix/redis/movie.rb @@ -38,6 +38,11 @@ module Librarix end def view + Librarix.redis.sadd('viewed_movies_id', id) + end + + def viewed? + Librarix.redis.sismember('viewed_movies_id', id) end end end diff --git a/lib/librarix/the_movie_db.rb b/lib/librarix/the_movie_db.rb index 8928cc1..2612492 100644 --- a/lib/librarix/the_movie_db.rb +++ b/lib/librarix/the_movie_db.rb @@ -26,6 +26,10 @@ module Librarix def added? Librarix::Redis::Movie.new(id).added? end + + def viewed? + Librarix::Redis::Movie.new(id).viewed? + end end end end diff --git a/lib/librarix/views/movie.slim b/lib/librarix/views/movie.slim index b30decd..3422b15 100644 --- a/lib/librarix/views/movie.slim +++ b/lib/librarix/views/movie.slim @@ -4,10 +4,15 @@ img src="#{poster_url(movie.poster_path, 'w154')}" .informations 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 .actions - 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" input type="hidden" name="id" value="#{movie.id}" button type="submit" data-action="remove-movie" Remove