diff options
Diffstat (limited to 'public/assets/js/panel/admin_users.js')
| -rw-r--r-- | public/assets/js/panel/admin_users.js | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/public/assets/js/panel/admin_users.js b/public/assets/js/panel/admin_users.js new file mode 100644 index 0000000..62a2aff --- /dev/null +++ b/public/assets/js/panel/admin_users.js @@ -0,0 +1,177 @@ +// Globals +// ----------------------------------------------------------------------------- +var petitions = []; +var table; + + + +// Supplamentary functions +// ----------------------------------------------------------------------------- + +// array.indexOf polyfill +// --- -- -- - - - +if (!Array.prototype.indexOf) +{ + Array.prototype.indexOf = function(elt /*, from*/) + { + var len = this.length >>> 0; + + var from = Number(arguments[1]) || 0; + from = (from < 0) + ? Math.ceil(from) + : Math.floor(from); + if (from < 0) + from += len; + + for (; from < len; from++) + { + if (from in this && + this[from] === elt) + return from; + } + return -1; + }; +} + + +// return petitions user by user-id +// --- -- -- - - - +function user_record(id) { + var record = false; + petitions.forEach(el => { if (el.id == id) record = el; }); + + return record; +} + + +// create actions html (edit and delete buttons) +// --- -- -- - - - +function create_actions_html(el) { + if (el.deprecated == null) { + icon = '<i class="fa-solid fa-minus"></i>'; + action = 'deprecate'; + title = 'κατάργηση'; + type = 'warning'; + + } else { + icon = '<i class="fa-solid fa-plus"></i>'; + action = 'activate'; + title = 'προσθήκη'; + type = 'primary'; + } + + return [ + '<button type="button" class="btn btn-xs btn-'+ type +'" title="'+ title +'"', + 'data-id="'+ el.id +'" data-action="'+ action +'" >', + icon, + '</button>' + ].join('\n'); + +} + + + +$(document).ready(function() { + + // When document is ready + // ------------------------------------------------------------------------- + + + /** init_table + * --- -- -- - - - + * get categories + * constuct categories array + * render dataTable + */ + function init_table() { + + // get all categories from server (ajax) + $.getJSON( "/admin/get/users") + .done(function( json ) { + // then ... + console.log(json); + + // reset categories + petitions.length = 0; + + // construct petitions data + json.forEach( el => { + rec = { + id: parseInt(el.id), + first_name: el.first_name, + last_name: el.last_name, + email: el.email, + sector: el.sector, + status: (el.deprecated == 1) + ? ('<i class="fa-regular fa-eye-slash"></i> / deprecated') + : ('<i class="fa-regular fa-eye"></i> / ' + ((el.active == 1) + ? 'active' + : 'pending') + ), + deprecated: el.deprecated + } + rec.actions = create_actions_html(rec); + petitions.push(rec); + }); + + + // destroy previous datatable table instances + $('#dt-petitions').dataTable().fnClearTable(); + $('#dt-petitions').dataTable().fnDestroy(); + + // (re-)create categories datatable + table = $('#dt-petitions').DataTable({ + language: { url: '/assets/js/DataTables/localization/Greek.json' }, + data: petitions, + // ordering: false, + columns: [ + { data: 'last_name' }, + { data: 'first_name'}, + { data: 'email' }, + { data: 'sector' }, + { data: 'status' }, + { data: 'actions', class: 'dt-actions' } + ] + }); + + }) + .fail(function( jqxhr, textStatus, error ) { + console.log( "Request Failed (" + error +")" ); + }); + + } + init_table(); // init table on first run + + + + /** When click to action button + * ------------------------------------------------------------------------- + */ + $('body').on('click', '.dt-actions button', event => { + event.preventDefault(); + var button; + + if ($(event.target).data('id')) { // click on button + button = $(event.target) + + } else { // click on button's content + button = $(event.target).parent('button'); + } + + var id = button.data('id'); // user-id from data-id + var action = button.data('action'); // action to take + + console.log(action, id); + + var request = '/admin/user/'+ action +'/'+ id; + + $.get(request) + .done(function(data) { + init_table(); + }); + + }) + + + +}); |
