From 203e8a7484fab76e4f3736eded5bc454ce1999a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Dahlgren?= Date: Mon, 17 Jan 2022 03:39:43 +0100 Subject: [PATCH] Delete all log files button --- lib/logs.js | 12 ++++++++++++ public/js/app/views/logs/list.js | 30 +++++++++++++++++++++++++++++- public/js/tpl/logs/list.html | 8 ++++++-- routes/logs.js | 10 ++++++++++ 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/lib/logs.js b/lib/logs.js index 3e30109..0e152e6 100644 --- a/lib/logs.js +++ b/lib/logs.js @@ -46,6 +46,18 @@ Logs.prototype.delete = function (filename, callback) { }) } +Logs.prototype.deleteAll = function (callback) { + this.logFiles(function (err, files) { + if (err) { + return callback(err) + } + + async.map(files, function (logFile, cb) { + fs.unlink(logFile.path, cb) + }, callback) + }) +} + Logs.prototype.generateLogFilePath = function (prefix, suffix) { return path.join(this.logsPath(), Logs.generateLogFileName(prefix, suffix)) } diff --git a/public/js/app/views/logs/list.js b/public/js/app/views/logs/list.js index a8654e0..293bae3 100644 --- a/public/js/app/views/logs/list.js +++ b/public/js/app/views/logs/list.js @@ -1,5 +1,7 @@ +var $ = require('jquery') var _ = require('underscore') var Marionette = require('marionette') +var sweetAlert = require('sweet-alert') var ListItemView = require('app/views/logs/list_item') var tpl = require('tpl/logs/list.html') @@ -9,5 +11,31 @@ var template = _.template(tpl) module.exports = Marionette.CompositeView.extend({ childView: ListItemView, childViewContainer: 'tbody', - template: template + template: template, + + events: { + 'click .delete-all': 'deleteAll' + }, + + deleteAll: function (event) { + var self = this + + sweetAlert({ + title: 'Are you sure?', + text: 'All logs will be deleted from the server!', + type: 'warning', + showCancelButton: true, + confirmButtonClass: 'btn-danger', + confirmButtonText: 'Yes, delete all!' + }, + function () { + $.ajax('/api/logs/all', { + type: 'DELETE', + success: function (data) { + self.collection.fetch() + }, + error: function () {} + }) + }) + } }) diff --git a/public/js/tpl/logs/list.html b/public/js/tpl/logs/list.html index d97609a..b706f21 100644 --- a/public/js/tpl/logs/list.html +++ b/public/js/tpl/logs/list.html @@ -5,8 +5,12 @@ Created Modified Size - - + + + + Delete all + + diff --git a/routes/logs.js b/routes/logs.js index d86a5ff..9f165d3 100644 --- a/routes/logs.js +++ b/routes/logs.js @@ -13,6 +13,16 @@ module.exports = function (logsManager) { }) }) + router.delete('/all', function (req, res) { + logsManager.deleteAll(function (err) { + if (err) { + res.status(500).send(err) + } else { + res.status(204).send() + } + }) + }) + router.delete('/:log', function (req, res) { var filename = req.params.log logsManager.delete(filename, function (err) {