Files
backend/public/dist/js/uploads.js
2024-02-01 09:53:53 +00:00

419 lines
12 KiB
JavaScript

var baseUrl = "https://rms.rmto.ir";
// var baseUrl = "http://localhost:3000";
// Get announcements data
$.ajax({
url: `${baseUrl}/events`,
type: "GET",
cache: false,
contentType: false,
processData: false,
success: function (result) {
//console.log(result);
result = result.data;
let rowNumber = 0;
if (result.length < 1) {
// No announcements available!
$(".card-body-container").append(
"<p>اطلاعیه ای جهت نمایش وجود ندارد.</p>"
);
} else {
// If we have 1 or 4 or ... announcements
if (result.length % 3 == 1) {
for (let index = 0; index < result.length; index++) {
if (index == result.length - 1) {
// Last element that is numebr 1 or 4 or ...
rowNumber = index;
$(".card-body-container").append(
'\
<div class="card-group" id="row' +
index +
'">\
<div\
class="card col-md-4"\
style="\
margin: 15px;\
border: 1px solid rgba(0, 0, 0, 0.125);\
border-radius: 0.25rem;\
padding: 0;\
"\
>\
<img src="' +
result[index].image +
'" class="card-img-top" alt="..." height="300px" />\
<div class="card-body">\
<h5 class="card-title" style="font-size: 1rem; line-height: 1.5;"><b>عنوان: </b>' +
result[index].title +
'</h5>\
<p class="card-text">\
<b>ضرب العجل: </b>' +
result[index].deadline +
'</p>\
<p class="card-text">\
<b>توضیحات: </b>' +
result[index].description +
'</p>\
<p class="card-text">' +
showButtons(
result[index].files,
result[index].id,
result[index].admin
) +
"\
</p>\
</div>\
</div>\
</div>\
"
);
$("#row" + rowNumber).append(
'\
<div\
class="card col-md-4"\
style="\
margin: 15px;\
border: 1px solid rgba(255, 255, 255, 0.125);\
border-radius: 0.25rem;\
box-shadow: none;\
padding: 0;\
"\
>\
</div>\
'
);
$("#row" + rowNumber).append(
'\
<div\
class="card col-md-4"\
style="\
margin: 15px;\
border: 1px solid rgba(255, 255, 255, 0.125);\
border-radius: 0.25rem;\
box-shadow: none;\
padding: 0;\
"\
>\
</div>\
'
);
} else {
showCardBoxes(result, index);
}
}
}
// If we have 2 or 3 or ... announcements
else {
for (let index = 0; index < result.length; index++) {
showCardBoxes(result, index);
}
}
}
},
error: function (error) {
//console.log(error);
},
});
function showCardBoxes(result, index) {
if (index % 3 == 0) {
rowNumber = index;
$(".card-body-container").append(
'\
<div class="card-group row" id="row' +
index +
'">\
<div\
class="card col-md-4"\
style="\
margin: 15px;\
border: 1px solid rgba(0, 0, 0, 0.125);\
border-radius: 0.25rem;\
padding: 0;\
"\
>\
<img src="' +
result[index].image +
'" class="card-img-top" alt="..." height="300px" />\
<div class="card-body">\
<h5 class="card-title" style="font-size: 1rem; line-height: 1.5;"><b>عنوان: </b>' +
result[index].title +
'</h5>\
<p class="card-text">\
<b>ضرب العجل: </b>' +
result[index].deadline +
'</p>\
<p class="card-text">\
<b>توضیحات: </b>' +
result[index].description +
'</p>\
<p class="card-text">' +
showButtons(
result[index].files,
result[index].id,
result[index].admin
) +
"\
</p>\
</div>\
</div>\
</div>\
"
);
} else {
$("#row" + rowNumber).append(
'\
<div\
class="card col-md-4"\
style="\
margin: 15px;\
border: 1px solid rgba(0, 0, 0, 0.125);\
border-radius: 0.25rem;\
padding: 0;\
"\
>\
<img src="' +
result[index].image +
'" class="card-img-top" alt="..." height="300px" />\
<div class="card-body">\
<h5 class="card-title" style="font-size: 1rem; line-height: 1.5;"><b>عنوان: </b>' +
result[index].title +
'</h5>\
<p class="card-text">\
<b>ضرب العجل: </b>' +
result[index].deadline +
'</p>\
<p class="card-text">\
<b>توضیحات: </b>' +
result[index].description +
'</p>\
<p class="card-text">' +
showButtons(
result[index].files,
result[index].id,
result[index].admin
) +
"\
</p>\
</div>\
</div>\
"
);
}
}
function showButtons(files, id, admin) {
let appendedButtons = "";
if (files != null) {
//rms.rmto.ir/events/user/download/${id}
https: appendedButtons =
'\
<button type="button" class="btn btn-outline-success"\
onclick="window.open(`/events/user/download/' +
id +
'`)">\
دانلود فایل\
</button>';
}
// else appendedButtons = "<span></span>";
if (admin == 0) {
// Towns
appendedButtons +=
'\
<button type="button" class="btn btn-outline-primary">\
<label for="upload-file' + id + '" class="upload-label">آپلود فایل</label>\
<input type="file" id="upload-file' + id + '" style="display: none;" onchange="showConfirmBox(' +
id +
')">\
</button>\
';
} else if (admin == 1) {
// The capital of the province
appendedButtons +=
'\
<button type="button" class="btn btn-outline-primary">\
<label for="upload-file' + id + '" class="upload-label">آپلود فایل</label>\
<input type="file" id="upload-file' + id + '" style="display: none;" onchange="showConfirmBox(' +
id +
')">\
</button>\
<button type="button" class="btn btn-outline-warning" onclick="window.open(`/events/histories/' +
id +
'`)">\
تاریخچه\
</button>\
';
} else if (admin == 2) {
// Admin user (General Manager)
appendedButtons +=
'\
<button type="button" class="btn btn-outline-warning" onclick="window.open(`/events/histories/' +
id +
'`)">\
تاریخچه\
</button>\
<button type="button" class="btn btn-outline-info" onclick="window.open(`/events/edit/' +
id +
'`)">\
ویرایش\
</button>\
<button type="button" class="btn btn-outline-secondary" onclick="archive(' +
id +
')">\
آرشیو اطلاعیه\
</button>\
<button type="button" class="btn btn-outline-danger" onclick="deleteAnEvent(' +
id +
')">\
حذف\
</button>\
';
}
return appendedButtons;
}
function showConfirmBox(id) {
let fileName = $("#upload-file" + id)[0].files[0].name;
let filesize = $("#upload-file" + id)[0].files[0].size / 1000000 ;
var formData = new FormData();
console.log(id);
formData.append("files", $("#upload-file" + id)[0].files[0]);
Swal.fire({
title: "آیا از ارسال این فایل مطمئن هستید؟",
text: "نام فایل: " + fileName,
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
cancelButtonText: "لغو عملیات",
confirmButtonText: "ارسال فایل",
showLoaderOnConfirm: true,
preConfirm: () => {
if (filesize <= 60){
return fetch(`https://rms.rmto.ir/events/user/upload/${id}`, {
method: "POST",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
},
body: formData,
})
.then((response) => {
//console.log(response);
if (!response.ok) {
throw new Error(response.statusCode);
}
return response.json();
})
.catch((error) => {
Swal.showValidationMessage(
`ارسال فایل با خطا مواجه گردید. لطفا دوباره تلاش کنید.`
);
});
}
else {
$("#upload-file").val("");
Swal.showValidationMessage(
'حجم فایل بیشتر از 60 مگابایت میباشد '
);
}
},
}).then((result) => {
//console.log(result);
$("#upload-file").val("");
if (result.dismiss === Swal.DismissReason.cancel) {
} else if (result.value.status == "succeed") {
Swal.fire("فایل با موفقیت ارسال شد!", "", "success");
} else {
Swal.fire(
"ارسال فایل با خطا مواجه گردید. لطفا دوباره تلاش کنید.",
"",
"error"
);
}
});
}
function deleteAnEvent(id) {
Swal.fire({
title: "آیا از حذف این مورد مطمئن هستید؟",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
cancelButtonText: "لغو عملیات",
confirmButtonText: "تایید و حذف",
showLoaderOnConfirm: true,
preConfirm: () => {
return fetch(`https://rms.rmto.ir/events/delete/${id}`, {
method: "POST",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
},
})
.then((response) => {
//console.log(response);
if (!response.ok) {
throw new Error(response.statusCode);
}
return response.json();
})
.catch((error) => {
Swal.showValidationMessage(`خطایی رخ داده است!`);
});
},
}).then((result) => {
//console.log(result);
if (result.dismiss === Swal.DismissReason.cancel) {
//
} else if (result.value.status == "succeed") {
window.open("/events/list", "_self");
} else {
Swal.fire("خطایی رخ داده است!", "", "error");
}
});
}
function archive(id) {
Swal.fire({
title: "آیا از آرشیو این مورد مطمئن هستید؟",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#3085d6",
cancelButtonColor: "#d33",
cancelButtonText: "لغو عملیات",
confirmButtonText: "تایید و حذف",
showLoaderOnConfirm: true,
preConfirm: () => {
return fetch(`https://rms.rmto.ir/events/archive/${id}`, {
method: "POST",
headers: {
"X-CSRF-TOKEN": $('meta[name="csrf-token"]').attr("content"),
},
})
.then((response) => {
//console.log(response);
if (!response.ok) {
throw new Error(response.statusCode);
}
return response.json();
})
.catch((error) => {
Swal.showValidationMessage(`خطایی رخ داده است!`);
});
},
}).then((result) => {
//console.log(result);
if (result.dismiss === Swal.DismissReason.cancel) {
//
} else if (result.value.status == "succeed") {
window.open("/events/list", "_self");
} else {
Swal.fire("خطایی رخ داده است!", "", "error");
}
});
}