116 lines
3.3 KiB
JavaScript
116 lines
3.3 KiB
JavaScript
// assets/js/utils/helpers.js
|
|
const Helpers = {
|
|
formatDate: (dateString) => {
|
|
return new Date(dateString).toLocaleDateString("id-ID", {
|
|
year: "numeric",
|
|
month: "long",
|
|
day: "numeric",
|
|
});
|
|
},
|
|
|
|
formatDateShort: (dateString) => {
|
|
return new Date(dateString).toLocaleDateString("id-ID", {
|
|
year: "numeric",
|
|
month: "short",
|
|
day: "numeric",
|
|
});
|
|
},
|
|
|
|
formatDateTime: (dateString) => {
|
|
return new Date(dateString).toLocaleString("id-ID", {
|
|
year: "numeric",
|
|
month: "long",
|
|
day: "numeric",
|
|
hour: "2-digit",
|
|
minute: "2-digit",
|
|
});
|
|
},
|
|
|
|
validateEmail: (email) => {
|
|
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
return re.test(email);
|
|
},
|
|
|
|
validatePhone: (phone) => {
|
|
const re = /^(08|62)[0-9]{9,12}$/;
|
|
return re.test(phone);
|
|
},
|
|
|
|
checkPasswordStrength: (password) => {
|
|
let strength = 0;
|
|
if (password.length >= 8) strength++;
|
|
if (password.match(/[a-z]+/)) strength++;
|
|
if (password.match(/[A-Z]+/)) strength++;
|
|
if (password.match(/[0-9]+/)) strength++;
|
|
if (password.match(/[$@#&!]+/)) strength++;
|
|
|
|
if (password.length < 8) {
|
|
return "";
|
|
} else if (strength <= 2) {
|
|
return "weak";
|
|
} else if (strength <= 4) {
|
|
return "medium";
|
|
} else {
|
|
return "strong";
|
|
}
|
|
},
|
|
|
|
debounce: (func, wait) => {
|
|
let timeout;
|
|
return function executedFunction(...args) {
|
|
const later = () => {
|
|
clearTimeout(timeout);
|
|
func(...args);
|
|
};
|
|
clearTimeout(timeout);
|
|
timeout = setTimeout(later, wait);
|
|
};
|
|
},
|
|
|
|
truncateText: (text, maxLength) => {
|
|
if (text.length <= maxLength) return text;
|
|
return text.substring(0, maxLength) + "...";
|
|
},
|
|
|
|
getStatusBadgeClass: (status) => {
|
|
const statusClasses = {
|
|
unclaimed: "bg-blue-500/20 text-blue-400 border border-blue-500/50",
|
|
verified: "bg-green-500/20 text-green-400 border border-green-500/50",
|
|
pending_claim:
|
|
"bg-yellow-500/20 text-yellow-400 border border-yellow-500/50",
|
|
case_closed: "bg-slate-500/20 text-slate-400 border border-slate-500/50",
|
|
expired: "bg-red-500/20 text-red-400 border border-red-500/50",
|
|
active: "bg-green-500/20 text-green-400 border border-green-500/50",
|
|
blocked: "bg-red-500/20 text-red-400 border border-red-500/50",
|
|
pending: "bg-yellow-500/20 text-yellow-400 border border-yellow-500/50",
|
|
approved: "bg-green-500/20 text-green-400 border border-green-500/50",
|
|
rejected: "bg-red-500/20 text-red-400 border border-red-500/50",
|
|
};
|
|
return (
|
|
statusClasses[status] ||
|
|
"bg-slate-500/20 text-slate-400 border border-slate-500/50"
|
|
);
|
|
},
|
|
|
|
getRoleBadgeClass: (role) => {
|
|
const roleClasses = {
|
|
admin: "bg-red-500/20 text-red-400 border border-red-500/50",
|
|
manager: "bg-yellow-500/20 text-yellow-400 border border-yellow-500/50",
|
|
user: "bg-blue-500/20 text-blue-400 border border-blue-500/50",
|
|
};
|
|
return (
|
|
roleClasses[role] ||
|
|
"bg-slate-500/20 text-slate-400 border border-slate-500/50"
|
|
);
|
|
},
|
|
|
|
getInitials: (name) => {
|
|
if (!name) return "?";
|
|
return name.charAt(0).toUpperCase();
|
|
},
|
|
|
|
showConfirm: (message) => {
|
|
return confirm(message);
|
|
},
|
|
};
|