2019-01-10 23:20:48 +01:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const snarkdown = require('snarkdown');
|
|
|
|
const fecha = require('fecha');
|
|
|
|
|
|
|
|
module.exports = function (app) {
|
|
|
|
app.server.get('/history', (req, res) => {
|
|
|
|
const files = fs.readdirSync(app.historyLocation).filter(fileName => fileName.includes('.json'))
|
|
|
|
.map(fileName => { // Cache the file data so sorting doesn't need to re-check each file
|
|
|
|
return { name: fileName, time: fs.statSync(path.resolve(app.historyLocation, fileName)).mtime.getTime() };
|
|
|
|
}).sort((a, b) => b.time - a.time).map(v => v.name); // Sort from newest to oldest.
|
|
|
|
|
|
|
|
let history = files.map(fileName => {
|
|
|
|
const bookData = JSON.parse(fs.readFileSync(path.resolve(app.historyLocation, fileName), 'utf8'));
|
|
|
|
bookData.author = bookData.author ? bookData.author : '<em>author not provided</em>';
|
|
|
|
bookData.contributor = bookData.contributor ? bookData.contributor : 'Anonymous';
|
|
|
|
const id = fileName.replace('.json', '');
|
|
|
|
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 removedTag = '<div class="control"><div class="tags has-addons"><span class="tag">Taken</span><span class="tag is-warning">' + removed + '</span></div></div>';
|
2019-01-10 23:31:08 +01:00
|
|
|
const modal = app.templater.fill('./templates/elements/modalCard.html', {
|
2019-01-10 23:20:48 +01:00
|
|
|
id,
|
|
|
|
header: '<h2 class="title">' + bookData.title + '</h2><h4 class="subtitle">' + bookData.author + '</h4>',
|
2019-01-10 23:31:08 +01:00
|
|
|
content: app.templater.fill('./templates/elements/bookInfo.html', {
|
2019-01-10 23:20:48 +01:00
|
|
|
contributor: bookData.contributor,
|
|
|
|
fileFormat: bookData.fileType,
|
|
|
|
added,
|
|
|
|
removedTag,
|
|
|
|
summary: snarkdown(bookData.summary),
|
|
|
|
}),
|
|
|
|
footer: '<a class="button close">Close</a>',
|
|
|
|
});
|
2019-01-10 23:31:08 +01:00
|
|
|
return app.templater.fill('./templates/elements/book.html', {
|
2019-01-10 23:20:48 +01:00
|
|
|
id,
|
|
|
|
title: bookData.title,
|
|
|
|
author: bookData.author,
|
|
|
|
fileType: bookData.fileType,
|
|
|
|
modal,
|
|
|
|
});
|
|
|
|
}).join('');
|
|
|
|
|
|
|
|
if (history == '') {
|
|
|
|
history = '<div class="column"><div class="content">No books have been taken yet. Would you like to <a href="/">take a book</a>?</div></div>';
|
|
|
|
}
|
|
|
|
|
|
|
|
const body = '<h2 class="title">History</h2><div class="columns is-multiline">' + history + '</div>';
|
2019-01-10 23:31:08 +01:00
|
|
|
const html = app.templater.fill('./templates/htmlContainer.html', {
|
2019-01-10 23:20:48 +01:00
|
|
|
title: 'History',
|
|
|
|
resourcePath: (req.url.substr(-1) === '/' ? '../' : './'),
|
|
|
|
body
|
|
|
|
});
|
|
|
|
|
|
|
|
if (html) {
|
|
|
|
res.send(html);
|
|
|
|
} else {
|
|
|
|
res.send('Something went wrong!');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|