91 lines
2.3 KiB
JavaScript
91 lines
2.3 KiB
JavaScript
/**
|
|
* CLQMS Frontend - App Entry Point (ESM)
|
|
* Imports Alpine, sets up global stores/utils, and exports Alpine.
|
|
*/
|
|
|
|
import Alpine from 'https://cdn.jsdelivr.net/npm/alpinejs@3.13.3/dist/module.esm.js';
|
|
|
|
// Make Alpine available globally for debugging if needed
|
|
window.Alpine = Alpine;
|
|
|
|
// Base URL helper
|
|
window.BASEURL = '<?= base_url() ?>'; // This logic usually needs to be injected in HTML, but we'll assume it's set in layout
|
|
|
|
// --- Global Stores ---
|
|
|
|
document.addEventListener('alpine:init', () => {
|
|
// Auth Store
|
|
Alpine.store('auth', {
|
|
user: null,
|
|
isAuthenticated: false,
|
|
setUser(userData) {
|
|
this.user = userData;
|
|
this.isAuthenticated = !!userData;
|
|
},
|
|
clearUser() {
|
|
this.user = null;
|
|
this.isAuthenticated = false;
|
|
}
|
|
});
|
|
|
|
// Toast Store
|
|
Alpine.store('toast', {
|
|
messages: [],
|
|
show(message, type = 'info', duration = 4000) {
|
|
const id = Date.now();
|
|
this.messages.push({ id, message, type });
|
|
setTimeout(() => this.dismiss(id), duration);
|
|
},
|
|
dismiss(id) {
|
|
this.messages = this.messages.filter(m => m.id !== id);
|
|
},
|
|
success(msg) { this.show(msg, 'success'); },
|
|
error(msg) { this.show(msg, 'error', 6000); },
|
|
info(msg) { this.show(msg, 'info'); }
|
|
});
|
|
});
|
|
|
|
// --- Utils ---
|
|
|
|
export const Utils = {
|
|
// Format date to locale string
|
|
formatDate(dateString) {
|
|
return new Date(dateString).toLocaleDateString('id-ID', {
|
|
year: 'numeric',
|
|
month: 'short',
|
|
day: 'numeric'
|
|
});
|
|
},
|
|
|
|
// API helper with credentials
|
|
async api(endpoint, options = {}) {
|
|
const defaultOptions = {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json'
|
|
},
|
|
credentials: 'include'
|
|
};
|
|
|
|
const response = await fetch(endpoint, { ...defaultOptions, ...options });
|
|
|
|
// Handle void responses or non-json
|
|
const contentType = response.headers.get('content-type');
|
|
let data = {};
|
|
if (contentType && contentType.includes('application/json')) {
|
|
data = await response.json();
|
|
}
|
|
|
|
if (!response.ok) {
|
|
throw new Error(data.message || 'API request failed');
|
|
}
|
|
|
|
return data;
|
|
}
|
|
};
|
|
|
|
// Export Utils globally if needed for non-module compatibility (optional)
|
|
window.Utils = Utils;
|
|
|
|
export default Alpine;
|