Remove taken book and notify if what you are looking at is taken

This commit is contained in:
Robbie Antenesse 2019-01-03 16:04:38 -07:00
parent 3c8449f6d1
commit 1078e6f6e5
4 changed files with 20 additions and 5 deletions

View File

@ -4,10 +4,19 @@ $(document).ready(function() {
var downloadButton; var downloadButton;
socket.on('get book', function(url) { socket.on('get book', function(url) {
console.log(url);
$(downloadButton).replaceWith('<a download href="' + url + '" class="button is-success is-large">Download</a>'); $(downloadButton).replaceWith('<a download href="' + url + '" class="button is-success is-large">Download</a>');
}); });
socket.on('remove book', function(bookId) {
var book = $('#book_' + bookId);
var modal = $('<div class="modal is-active"><div class="modal-background"></div><div class="modal-content"><button class="modal-close"></button><div class="notification is-danger"><h2 class="title">Sorry!</h2><p>Someone took the book you were looking at.</h2></div></div></div>');
modal.find('.modal-background, .modal-close').click(function() {
modal.remove();
});
book.after(modal);
book.remove();
});
$('.navbar-burger').click(function() { $('.navbar-burger').click(function() {
if ($(this).hasClass('is-active')) { if ($(this).hasClass('is-active')) {
$(this).removeClass('is-active'); $(this).removeClass('is-active');
@ -32,6 +41,9 @@ $(document).ready(function() {
$('.take-book').click(function() { $('.take-book').click(function() {
var id = $(this).data('book'); var id = $(this).data('book');
$('#book_' + id).find('.box')
.removeClass('box').addClass(['notification', 'is-success'])
.attr('title', 'This can be downloaded until you leave this page');
socket.emit('take book', id); socket.emit('take book', id);
downloadButton = this; downloadButton = this;
$(this).addClass('is-loading'); $(this).addClass('is-loading');

View File

@ -144,10 +144,12 @@ function Server () {
console.log(socket.id + ' removed ' + bookId); console.log(socket.id + ' removed ' + bookId);
const downloadLocation = fileLocation.substr(fileLocation.lastIndexOf('/')); const downloadLocation = fileLocation.substr(fileLocation.lastIndexOf('/'));
socket.emit('get book', encodeURI('./files' + downloadLocation)); socket.emit('get book', encodeURI('./files' + downloadLocation));
socket.broadcast.emit('remove book', bookId);
} }
}); });
socket.on('disconnect', () => { socket.on('disconnect', () => {
// console.log(socket.id + ' disconnected');
this.broadcastVisitors(); this.broadcastVisitors();
this.deleteBooks(socket.id); this.deleteBooks(socket.id);
}); });
@ -240,7 +242,7 @@ Server.prototype.generateHistoryPage = function (req) {
const id = fileName.replace('.json', ''); const id = fileName.replace('.json', '');
const added = fecha.format(new Date(bookData.added), 'hh:mm:ssA on dddd MMMM Do, YYYY'); const added = fecha.format(new Date(bookData.added), 'hh:mm:ssA on dddd MMMM Do, YYYY');
const removed = fecha.format(new Date(parseInt(id)), 'hh:mm:ssA on dddd MMMM Do, YYYY'); const removed = fecha.format(new Date(parseInt(id)), 'hh:mm:ssA on dddd MMMM Do, YYYY');
const removedTag = '<div class="control"><div class="tags has-addons"><span class="tag">Taken</span><span class="tag is-primary">' + removed + '</span></div></div>'; const removedTag = '<div class="control"><div class="tags has-addons"><span class="tag">Taken</span><span class="tag is-warning">' + removed + '</span></div></div>';
const modal = this.fillTemplate('./templates/elements/modalCard.html', { const modal = this.fillTemplate('./templates/elements/modalCard.html', {
id, id,
header: '<h2 class="title">' + bookData.title + '</h2><h4 class="subtitle">' + bookData.author + '</h4>', header: '<h2 class="title">' + bookData.title + '</h2><h4 class="subtitle">' + bookData.author + '</h4>',
@ -353,6 +355,7 @@ Server.prototype.deleteBooks = function (socketId) {
if (data.socketId === socketId) { if (data.socketId === socketId) {
const check = this.checkId(data.bookId, (bookPath, bookDataPath) => { const check = this.checkId(data.bookId, (bookPath, bookDataPath) => {
fs.unlinkSync(bookPath); fs.unlinkSync(bookPath);
// console.log('removed ' + bookPath);
fs.renameSync(bookDataPath, unusedFilename.sync(path.resolve(this.historyLocation, Date.now() + '.json'))); fs.renameSync(bookDataPath, unusedFilename.sync(path.resolve(this.historyLocation, Date.now() + '.json')));
}); });
if (check === false) { if (check === false) {

View File

@ -1,10 +1,10 @@
<div class="column is-one-quarter"> <div class="column is-one-quarter" id="book_{{id}}">
<div class="box modal-button has-text-centered" data-modal="{{id}}"> <div class="box modal-button has-text-centered" data-modal="{{id}}">
<h2 class="title">{{title}}</h2> <h2 class="title">{{title}}</h2>
<h4 class="subtitle">{{author}}</h4> <h4 class="subtitle">{{author}}</h4>
<div class="tags has-addons"> <div class="tags has-addons">
<span class="tag">File Format</span> <span class="tag">File Format</span>
<span class="tag is-primary">{{fileType}}</span> <span class="tag is-info">{{fileType}}</span>
</div> </div>
</div> </div>

View File

@ -4,7 +4,7 @@
<div class="control"> <div class="control">
<div class="tags has-addons"> <div class="tags has-addons">
<span class="tag">File Format</span> <span class="tag">File Format</span>
<span class="tag is-primary">{{fileFormat}}</span> <span class="tag is-info">{{fileFormat}}</span>
</div> </div>
</div> </div>
<div class="control"> <div class="control">