File: /var/www/vhost/disk-apps/alq-cali.bikenow.co/public/js/notifications.js
function convertTime12to24(time12h) {
const [time, modifier] = time12h.split(" ");
let [hours, minutes] = time.split(":");
if (hours === "12") {
hours = "00";
}
if (modifier === "PM") {
hours = parseInt(hours, 10) + 12;
}
return hours + ":" + minutes;
}
function createNotification() {
$("#createNotification").validator("update");
$("#createNotification")
.validator("update")
.on("submit", function (e) {
if (e.isDefaultPrevented()) {
} else {
e.preventDefault();
$("#btn-create-notification").addClass("displayNone");
$("#spinnerButton").removeClass("displayNone");
var date1 = dateNow();
var date2 = $(" #createNotification input[name='date']").val();
var hour1 = hourNow();
var hour2 = convertTime12to24(
$(" #createNotification input[name='time']").val()
);
var fecha1 = new Date(date1 + " " + hour1);
var fecha2 = new Date(date2 + " " + hour2);
var tags = $("#createNotification select[name='generalMultiselect']").val();
if (new Date(fecha1).getTime() > new Date(fecha2).getTime()) {
swal("Advertencia", Lang.get("messagesClient.notification_tag3"), "warning");
$("#spinnerButton").addClass("displayNone");
$("#btn-create-notification").removeClass("displayNone");
return;
} else {
var email_user = $("#createNotification input[name='email_user']").val();
var user_id = $("#email_user").attr("user-id");
var link = $("#createNotification input[name='link']").val();
if (email_user.trim() != "" && user_id == "") {
swal("Advertencia", "No se encontro el usuario con el correo electrónico ingresado", "warning");
$("#spinnerButton").addClass("displayNone");
$("#btn-create-notification").removeClass(
"displayNone"
);
return;
}
if (!user_id || user_id == "") {
user_id = null;
}
var info = {
message: $(" #createNotification textarea[name='message']").val(),
when_send:
$("#createNotification input[name='date']").val() +
" " +
convertTime12to24(
$("#createNotification input[name='time']").val()
),
user_id,
link: link != "" ? link : null,
tags: tags
};
swal(Lang.get("messagesClient.notification_tag1"), {
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
goCreateNotification(info);
} else {
$("#btn-create-notification").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
}
});
}
}
});
/**
* Esta función hace un llamado AJAX y envia un json creando un nuevo registro de notificaciones
* @param info el json con la informacion de la vista
* @version 2018/05/20
*/
function goCreateNotification(info) {
$.ajax({
url: "/notifications/create",
type: "POST",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
"Content-Type": "application/json",
},
data: JSON.stringify(info),
success: function (r) {
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr("href", "/notifications");
}
});
} else {
swal("Advertencia", r.m, "warning");
$("#btn-create-notification").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
}
},
error: function (e) {
swal("Error", Lang.get("messagesClient.notification_tag2"), "error");
$("#btn-create-notification").removeClass("displayNone");
$("#spinnerButton").addClass("displayNone");
},
});
}
}
function dateNow() {
var hoy = new Date();
var dd = hoy.getDate();
var mm = hoy.getMonth() + 1;
var yyyy = hoy.getFullYear();
dd = addZero(dd);
mm = addZero(mm);
return yyyy + "-" + mm + "-" + dd;
}
function addZero(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
function hourNow() {
var hour = new Date();
hour = hour.getHours() + ":" + hour.getMinutes();
return hour;
}
function autocompleteEmail() {
$("#email_user").keyup(function (e) {
$("#email_user").attr("user-id", "");
$("#name_user").html("");
});
$("#name_user").html("");
$("#email_user").attr("user-id", "");
$("#email_user").autocomplete({
source: "/academy_user/autocomplete/mail",
minLength: 3,
select: function (event, ui) {
$("#email_user").attr("user-id", ui.item.id);
$("#email_user").val(ui.item.email);
$("#name_user").html(ui.item.first_name + ui.item.last_name);
},
});
}
/**Método para eliminar notificaciones*/
function clickDeleteNotification(value) {
swal(Lang.get("messagesClient.notification_tag5"), {
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
var info = {
id: value,
};
$.ajax({
url: "/notifications/delete",
type: "POST",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr(
"content"
),
"Content-Type": "application/json",
},
data: JSON.stringify(info),
success: function (r) {
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr("href", "/notifications");
}
});
} else {
swal("Advertencia", r.m, "warning");
}
},
error: function (e) {
swal("Error", Lang.get("messagesClient.notification_tag6"), "error");
},
});
}
});
}
function clearAllNotifications() {
swal(Lang.get("messagesClient.notification_clear_all"), {
buttons: {
cancel: "No",
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$.ajax({
url: "/notifications/clear-all",
type: "POST",
contentType: "application/json",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr(
"content"
),
"Content-Type": "application/json",
},
data: null,
success: function (r) {
if (r.r) {
swal(r.m, {
icon: "success",
buttons: {
Ok: true,
},
}).then((val) => {
if (val == "Ok") {
$(location).attr("href", "/notifications");
}
});
} else {
swal("Advertencia", r.m, "warning");
}
},
error: function (e) {
swal("Error", Lang.get("messagesClient.notification_clear_all_error"), "error");
},
});
}
});
}