added User management
This commit is contained in:
125
public/assets/js/traffic-mgt.js
Normal file
125
public/assets/js/traffic-mgt.js
Normal file
@@ -0,0 +1,125 @@
|
||||
const token = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
|
||||
const startDateElement = document.getElementById('startDate');
|
||||
const endDateElement = document.getElementById('endDate');
|
||||
|
||||
const startDatepicker = new Datepicker(startDateElement, {
|
||||
autohide: true,
|
||||
format: 'yyyy-mm-dd'
|
||||
});
|
||||
|
||||
|
||||
const endDatepicker = new Datepicker(endDateElement, {
|
||||
autohide: true,
|
||||
format: 'yyyy-mm-dd'
|
||||
});
|
||||
|
||||
function sendDailySmsUnits() {
|
||||
document.getElementById('loadingOverlay').style.display = 'flex';
|
||||
const endpoint = "{{ route('client.dailysmsunits') }}";
|
||||
const startDate = startDateElement.value;
|
||||
const endDate = endDateElement.value;
|
||||
fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'X-CSRF-TOKEN': token },
|
||||
body: 'start_date=' + encodeURIComponent(startDate) + '&end_date=' + encodeURIComponent(endDate)
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
// document.getElementById('loadingSpinner').style.display = 'none';
|
||||
document.getElementById('loadingOverlay').style.display = 'none';
|
||||
const theReportRange = document.getElementById('reportRange');
|
||||
const theSmsUnitsValue = document.getElementById('smsUnitsValue');
|
||||
theReportRange.innerHTML = data.reportDate;
|
||||
theSmsUnitsValue.innerHTML = "SMS Units : " + data.smsUnits + "| Charge : " + data.clientChargeTotal.toFixed(2);
|
||||
|
||||
})
|
||||
.catch(error => console.error('Error:', error));
|
||||
}
|
||||
endDateElement.addEventListener('changeDate', sendDailySmsUnits);
|
||||
|
||||
|
||||
function statusDesign (cell, formatterParams){
|
||||
var value = cell.getValue();
|
||||
if (value !== null) {
|
||||
if(value.includes('SENT')){
|
||||
return "<span style='color:#3FB449; font-weight:bold;'>" + value + "</span>";
|
||||
}
|
||||
else{
|
||||
return "<span style='color:#E4A11B;'>" + value + "</span>";
|
||||
}
|
||||
}
|
||||
}
|
||||
var table = new Tabulator("#message-table", {
|
||||
ajaxURL: base_url = "client-traffic-tabulator", // "https://smsportal.clickmlapps.com/client-traffic-tabulator/",
|
||||
ajaxConfig: {
|
||||
method: "GET",
|
||||
headers: {
|
||||
"Content-type": "application/json; charset=utf-8",
|
||||
},
|
||||
},
|
||||
ajaxParams: {size: 1000},
|
||||
pagination: "remote",
|
||||
paginationSize: 20,
|
||||
paginationDataSent: {
|
||||
"page": "page",
|
||||
"size": "size"
|
||||
},
|
||||
paginationDataReceived: {
|
||||
"last_page": "totalPages",
|
||||
"data": "content",
|
||||
"current_page": "number",
|
||||
"total": "totalElements"
|
||||
},
|
||||
ajaxResponse: function(url, params, response) {
|
||||
return response.content;
|
||||
},
|
||||
columns: [
|
||||
{title: "Sender", field: "from", width:150, headerFilter:"input"},
|
||||
{title: "Msisdn", field: "to", width:150, headerFilter:"input"},
|
||||
{title:"Message", field:"message", width:650, formatter:"textarea", headerFilter:"input"},
|
||||
{
|
||||
title:"Date Created",
|
||||
field:"createdAt",
|
||||
width:200,
|
||||
formatter:"datetime",
|
||||
formatterParams:{
|
||||
inputFormat:"iso",
|
||||
outputFormat:"yyyy-MM-dd",
|
||||
invalidPlaceholder:"(invalid date)"
|
||||
},
|
||||
headerFilter:function(cell, onRendered, success, cancel){
|
||||
// Create native date input
|
||||
var input = document.createElement("input");
|
||||
input.type = "date";
|
||||
|
||||
input.addEventListener("change", function(){
|
||||
console.log(input.value);
|
||||
success(input.value); // pass value to Tabulator filter
|
||||
});
|
||||
|
||||
return input;
|
||||
},
|
||||
headerFilterFunc:function(headerValue, rowValue){
|
||||
if(!headerValue){ return true; } // no filter
|
||||
if(!rowValue){ return false; }
|
||||
|
||||
// Extract just the date portion from ISO timestamp
|
||||
const rowDate = new Date(rowValue);
|
||||
const formatted = rowDate.toISOString().split("T")[0]; // yyyy-MM-dd
|
||||
|
||||
return formatted === headerValue;
|
||||
}
|
||||
}
|
||||
],
|
||||
});
|
||||
|
||||
document.getElementById("download-pdf").addEventListener("click", function(){
|
||||
table.download("pdf", "messages.pdf", {
|
||||
orientation:"portrait", // portrait or landscape
|
||||
title:"Messages Export", // document title
|
||||
});
|
||||
});
|
||||
|
||||
document.getElementById("download-xlsx").addEventListener("click", function(){
|
||||
table.download("xlsx", "messages.xlsx", {sheetName:"Messages"});
|
||||
});
|
||||
@@ -1,217 +1,192 @@
|
||||
$(document).ready(function(){
|
||||
// $('.editUserBtn').click(function(evnt){
|
||||
$(document).ready(function() {
|
||||
$.ajaxSetup({
|
||||
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }
|
||||
});
|
||||
|
||||
const sessionModal = new bootstrap.Modal(document.getElementById('sessionModal'));
|
||||
|
||||
let currentPage = 1;
|
||||
let searchQuery = '';
|
||||
let searchTimer;
|
||||
|
||||
// });
|
||||
//
|
||||
console.log('foo bar');
|
||||
$('#editAllowedApps').select2({
|
||||
// width: "resolve",
|
||||
dropdownParent: $('#editUserModal'),
|
||||
placeholder : "Select options, multiple allowed"
|
||||
fetchSessions(currentPage, searchQuery);
|
||||
|
||||
});
|
||||
function fetchSessions(page, search = '') {
|
||||
$.ajax({
|
||||
type: "GET",
|
||||
url: base_url + `/fetch-client-users?page=${page}&search=${encodeURIComponent(search)}`,
|
||||
dataType: "json",
|
||||
success: function(response) {
|
||||
$('#clientUsersTableBody').html("");
|
||||
|
||||
if (response.sessions.data && response.sessions.data.length > 0) {
|
||||
$.each(response.sessions.data, function(key, item) {
|
||||
$('#clientUsersTableBody').append(`
|
||||
<tr>
|
||||
<td>${item.id}</td>
|
||||
<td>${item.email}</td>
|
||||
<td>${item.role}</td>
|
||||
<td>
|
||||
<button class="btn btn-success btn-sm editBtn" value="${item.id}">Edit</button>
|
||||
<button class="btn btn-danger btn-sm deleteBtn" value="${item.id}">Delete</button>
|
||||
</td>
|
||||
</tr>
|
||||
`);
|
||||
});
|
||||
|
||||
$('#allowedApps').select2({
|
||||
// width: "resolve",
|
||||
dropdownParent: $('#addUserModal'),
|
||||
placeholder : "Select options, multiple allowed"
|
||||
});
|
||||
let from = response.sessions.from;
|
||||
let to = response.sessions.to;
|
||||
let total = response.sessions.total;
|
||||
$('#paginationCounter').html(`Showing ${from} to ${to} of ${total} results`);
|
||||
} else {
|
||||
$('#clientUsersTableBody').html("<tr><td colspan='4' class='text-center'>No users found</td></tr>");
|
||||
$('#paginationCounter').html('Showing 0 to 0 of 0 results');
|
||||
}
|
||||
|
||||
renderPagination(response);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$('#inputPermissions').select2({
|
||||
// width: "resolve",
|
||||
dropdownParent: $('#editUserModal'),
|
||||
placeholder : "Select options, multiple allowed"
|
||||
});
|
||||
$('#searchInput').on('keyup', function() {
|
||||
clearTimeout(searchTimer);
|
||||
searchQuery = $(this).val();
|
||||
|
||||
// Wait 500ms after the user stops typing to trigger the request
|
||||
searchTimer = setTimeout(function() {
|
||||
currentPage = 1; // Reset to page 1 for a brand new search
|
||||
fetchSessions(currentPage, searchQuery);
|
||||
}, 500);
|
||||
});
|
||||
|
||||
$('.editUserBtn').click(function(evnt){
|
||||
evnt.preventDefault();
|
||||
var selectedUserId = $(this).siblings('.userIdinput').val();
|
||||
$(document).on('click', '.page-link', function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
if ($(this).parent().hasClass('disabled')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('user_id', selectedUserId);
|
||||
let page = $(this).data('page');
|
||||
|
||||
if (page > 0) {
|
||||
currentPage = page;
|
||||
fetchSessions(currentPage, searchQuery);
|
||||
}
|
||||
});
|
||||
|
||||
$.ajax({
|
||||
url: base_url + '/users/edit/' + selectedUserId,
|
||||
type: 'GET',
|
||||
processData: false,
|
||||
contentType: false,
|
||||
beforeSend: function() {
|
||||
$('#editSuccessArea').text("");
|
||||
$('#editErrorArea').text("Please wait ... loading user details!");
|
||||
},
|
||||
success: function(data) {
|
||||
var jason = data.data;
|
||||
if(data.success == true){
|
||||
var allowedAppsArray = [];
|
||||
if (jason['allowed_apps']) {
|
||||
allowedAppsArray = jason['allowed_apps'].split(",");
|
||||
}
|
||||
console.log(jason['full_name']);
|
||||
$('#editFullName').val(jason['full_name']);
|
||||
$('#editEmail').val(jason['email']);
|
||||
$('#editUsername').val(jason['username']);
|
||||
$('#editGender').val(jason['gender']);
|
||||
$('#editTitle').val(jason['title']);
|
||||
$('#editUaPostion').val(jason['ua_position']);
|
||||
$('#editPhone').val(jason['phone']);
|
||||
$('#editAllowedApps').val(allowedAppsArray).trigger('change');
|
||||
$('#editRegionID').val(jason['region_id']);
|
||||
$('#editDistrictId').val(jason['district_id']);
|
||||
$('#editUaPostion').val(jason['ua_position']);
|
||||
$('#editGender').val(jason['gender']);
|
||||
$("input[name='user_id']").val(jason.ua_id);
|
||||
|
||||
}
|
||||
//$('#editUserModal').modal('show');
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.error('Error:', error);
|
||||
$('#errorArea').text(error);
|
||||
$('#errorArea').text(error);
|
||||
}
|
||||
});
|
||||
function renderPagination(response) {
|
||||
let linksHtml = '';
|
||||
currentPage = response.sessions.current_page;
|
||||
let lastPage = response.sessions.last_page;
|
||||
|
||||
});
|
||||
let prevDisabled = currentPage === 1 ? 'disabled' : '';
|
||||
let prevTabIndex = currentPage === 1 ? 'tabindex="-1"' : '';
|
||||
|
||||
linksHtml += `
|
||||
<li class="page-item ${prevDisabled}">
|
||||
<a class="page-link" href="#" data-page="${currentPage - 1}" ${prevTabIndex}>Previous</a>
|
||||
</li>`;
|
||||
|
||||
$('.viewUserBtn').click(function(evnt){
|
||||
evnt.preventDefault();
|
||||
var selectedUserId = $(this).siblings('.userIdinput').val();
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('user_id', selectedUserId);
|
||||
for (let i = 1; i <= lastPage; i++) {
|
||||
let activeClass = currentPage === i ? 'active' : '';
|
||||
let activeAria = currentPage === i ? 'aria-current="page"' : '';
|
||||
|
||||
linksHtml += `
|
||||
<li class="page-item ${activeClass}" ${activeAria}>
|
||||
<a class="page-link" href="#" data-page="${i}">${i}</a>
|
||||
</li>`;
|
||||
}
|
||||
let nextDisabled = currentPage === lastPage ? 'disabled' : '';
|
||||
let nextTabIndex = currentPage === lastPage ? 'tabindex="-1"' : '';
|
||||
|
||||
linksHtml += `
|
||||
<li class="page-item ${nextDisabled}">
|
||||
<a class="page-link" href="#" data-page="${currentPage + 1}" ${nextTabIndex}>Next</a>
|
||||
</li>`;
|
||||
|
||||
$.ajax({
|
||||
url: base_url + '/users/' + selectedUserId,
|
||||
type: 'GET',
|
||||
processData: false,
|
||||
contentType: false,
|
||||
beforeSend: function() {
|
||||
$('#viewSuccessArea').text("");
|
||||
$('#viewErrorArea').text("Please wait ... loading user details!");
|
||||
},
|
||||
success: function(data) {
|
||||
var jason = data.data;
|
||||
if(data.success == true){
|
||||
var allowedAppsArray = [];
|
||||
if (jason['allowed_apps']) {
|
||||
allowedAppsArray = jason['allowed_apps'].split(",");
|
||||
}
|
||||
console.log(jason['full_name']);
|
||||
$('#viewFullName').val(jason['full_name']);
|
||||
$('#viewEmail').val(jason['email']);
|
||||
$('#viewUsername').val(jason['username']);
|
||||
$('#viewGender').val(jason['gender']);
|
||||
$('#viewTitle').val(jason['title']);
|
||||
$('#viewUaPostion').val(jason['ua_position']);
|
||||
$('#viewPhone').val(jason['phone']);
|
||||
$('#viewAllowedApps').val(allowedAppsArray).trigger('change');
|
||||
$('#viewRegionID').val(jason['region_id']);
|
||||
$('#viewDistrictId').val(jason['district_id']);
|
||||
$('#viewUaPostion').val(jason['ua_position']);
|
||||
$('#viewGender').val(jason['gender']);
|
||||
$("input[name='user_id']").val(jason.ua_id);
|
||||
|
||||
}
|
||||
//$('#editUserModal').modal('show');
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.error('Error:', error);
|
||||
$('#errorArea').text(error);
|
||||
$('#errorArea').text(error);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
$("#newUserForm").submit(function(evt){
|
||||
evt.preventDefault();
|
||||
$('#successArea').addClass('d-none');
|
||||
$('#errorsArea').removeClass('d-none');
|
||||
var formData = new FormData($(this)[0]);
|
||||
|
||||
$.ajax({
|
||||
url: base_url + '/users',
|
||||
type: 'POST',
|
||||
data: formData,
|
||||
processData: false,
|
||||
contentType: false,
|
||||
beforeSend: function() {
|
||||
$('#successArea').text("");
|
||||
$('#successArea').text("Please wait ... user creation in progress!");
|
||||
},
|
||||
success: function(data) {
|
||||
|
||||
if (data['success'] == true) {
|
||||
$('#successArea').removeClass('d-none');
|
||||
$('#errorsArea').addClass('d-none');
|
||||
|
||||
$('#successArea').text("");
|
||||
$('#successArea').text("User successfully created!");
|
||||
// location.reload();
|
||||
setTimeout(function() {
|
||||
location.reload(); // Reloads the current page
|
||||
}, 15000);
|
||||
}
|
||||
else{
|
||||
$('#successArea').addClass('d-none');
|
||||
$('#errorArea').removeClass('d-none');
|
||||
$('#errorArea').text("");
|
||||
$('#errorArea').text("User could not be created!");
|
||||
}
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.error('Error:', error);
|
||||
$('#successArea').text(error);
|
||||
$('#successArea').text(error);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$("#editUserForm").submit(function(evt){
|
||||
evt.preventDefault();
|
||||
$('#successArea').addClass('d-none');
|
||||
$('#errorsArea').removeClass('d-none');
|
||||
var formData = new FormData($(this)[0]);
|
||||
$.ajax({
|
||||
url: base_url + '/users/update/',
|
||||
type: 'POST',
|
||||
data: formData,
|
||||
processData: false,
|
||||
contentType: false,
|
||||
beforeSend: function() {
|
||||
// $('#updateBtn').addClass('d-none');
|
||||
// $('#uodateProgressBtn').removeClass('d-none');
|
||||
// $('#updateResultsDiv').removeClass('d-none');
|
||||
// $('#updateResultsParagraph').text("Processing Please wait ...");
|
||||
},
|
||||
success: function(data) {
|
||||
console.log(data);
|
||||
$('#editSuccessArea').removeClass('d-none');
|
||||
$('#editErrorArea').addClass('d-none');
|
||||
$('#editSuccessArea').text("");
|
||||
$('#editSuccessArea').text("User successfully details updated!");
|
||||
|
||||
},
|
||||
error: function(xhr, status, error) {
|
||||
console.error('Error:', error);
|
||||
$('#editSuccessArea').text(error);
|
||||
$('#editErrorArea').text(error);
|
||||
location.reload();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$('#regionID').change(function(){
|
||||
var options = $('#districtID');
|
||||
var region_id = $('#regionID').val();
|
||||
$.get( base_url + '/admin/districts/' + region_id, function (data) {
|
||||
$('#districtID').empty();
|
||||
$.each(data['districts'], function(id, row) {
|
||||
$('#districtID').append($("<option />").val(row.districtid).text(row.district_name));
|
||||
});
|
||||
});
|
||||
});
|
||||
$('#paginationLinks').html(linksHtml);
|
||||
}
|
||||
|
||||
|
||||
|
||||
});
|
||||
$(document).on('click', '.page-link', function(e) {
|
||||
e.preventDefault();
|
||||
let page = $(this).data('page');
|
||||
|
||||
if (page > 0) {
|
||||
fetchSessions(page);
|
||||
}
|
||||
});
|
||||
$('#addNewBtn').click(function() {
|
||||
$('#sessionForm')[0].reset();
|
||||
$('#sessionId').val('');
|
||||
$('#modalTitle').text('Add Session');
|
||||
sessionModal.show();
|
||||
});
|
||||
|
||||
$('#sessionForm').submit(function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
let id = $('#sessionId').val();
|
||||
let data = {
|
||||
email: $('#email').val(),
|
||||
role: $('#role').val(),
|
||||
password: $('#password').val()
|
||||
};
|
||||
|
||||
let url = id ? base_url + `/client-users/${id}` : base_url + '/client-users';
|
||||
let type = id ? "PUT" : "POST";
|
||||
|
||||
$.ajax({
|
||||
type: type,
|
||||
url: url,
|
||||
data: data,
|
||||
dataType: "json",
|
||||
success: function(response) {
|
||||
sessionModal.hide();
|
||||
showAlert(response.message, 'success');
|
||||
fetchSessions(currentPage);
|
||||
},
|
||||
error: function(xhr) {
|
||||
let errors = xhr.responseJSON.errors;
|
||||
if(errors.email) showAlert(errors.email, 'danger');
|
||||
if(errors.role) showAlert(errors.role, 'danger');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$(document).on('click', '.editBtn', function() {
|
||||
let id = $(this).val();
|
||||
$.get(base_url + `/client-users/${id}/edit`, function(response) {
|
||||
$('#sessionId').val(response.session.id);
|
||||
$('#email').val(response.session.email);
|
||||
$('#role').val(response.session.role);
|
||||
$('#modalTitle').text('Edit Session');
|
||||
sessionModal.show();
|
||||
});
|
||||
});
|
||||
|
||||
$(document).on('click', '.deleteBtn', function() {
|
||||
let id = $(this).val();
|
||||
if(confirm('Are you sure you want to delete this session?')) {
|
||||
$.ajax({
|
||||
type: "DELETE",
|
||||
url: base_url + `/client-users/${id}`,
|
||||
success: function(response) {
|
||||
showAlert(response.message, 'success');
|
||||
fetchSessions(currentPage); // Stay on current page
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
function showAlert(message, type) {
|
||||
$('#alertArea').html(`
|
||||
<div class="alert alert-${type} alert-dismissible fade show" role="alert">
|
||||
${message}
|
||||
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
||||
</div>
|
||||
`);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user