feat: replace notifications with import status in profile dropdown
Remove the notification system entirely and show import progress directly in the user dropdown menu. Block new imports while one is already running. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -43,6 +43,7 @@ export default class extends Controller {
|
||||
}
|
||||
|
||||
this._showFeedback('Import lancé !', false);
|
||||
document.dispatchEvent(new CustomEvent('import:started'));
|
||||
setTimeout(() => this.close(), 1500);
|
||||
} catch (e) {
|
||||
this._showFeedback('Une erreur est survenue.', true);
|
||||
|
||||
99
assets/controllers/import_status_controller.js
Normal file
99
assets/controllers/import_status_controller.js
Normal file
@@ -0,0 +1,99 @@
|
||||
import { Controller } from '@hotwired/stimulus';
|
||||
|
||||
export default class extends Controller {
|
||||
static targets = ['item', 'importBtn', 'badge'];
|
||||
|
||||
connect() {
|
||||
this._poll();
|
||||
this._interval = setInterval(() => this._poll(), 5000);
|
||||
this._onImportStarted = () => this._poll();
|
||||
document.addEventListener('import:started', this._onImportStarted);
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
clearInterval(this._interval);
|
||||
document.removeEventListener('import:started', this._onImportStarted);
|
||||
}
|
||||
|
||||
async _poll() {
|
||||
try {
|
||||
const response = await fetch('/api/imports/latest');
|
||||
if (!response.ok) return;
|
||||
|
||||
const data = await response.json();
|
||||
this._update(data);
|
||||
} catch (e) {
|
||||
// silently ignore
|
||||
}
|
||||
}
|
||||
|
||||
_update(data) {
|
||||
if (!data) {
|
||||
this._showDefault();
|
||||
return;
|
||||
}
|
||||
|
||||
const isActive = data.status === 'pending' || data.status === 'processing';
|
||||
|
||||
if (isActive) {
|
||||
this._showActive(data);
|
||||
} else if (data.status === 'completed') {
|
||||
this._showCompleted(data);
|
||||
} else if (data.status === 'failed') {
|
||||
this._showFailed();
|
||||
} else {
|
||||
this._showDefault();
|
||||
}
|
||||
}
|
||||
|
||||
_showDefault() {
|
||||
this.importBtnTarget.disabled = false;
|
||||
this.importBtnTarget.textContent = 'Importer ses films';
|
||||
this._removeStatus();
|
||||
this.badgeTarget.hidden = true;
|
||||
}
|
||||
|
||||
_showActive(data) {
|
||||
this.importBtnTarget.disabled = true;
|
||||
this.importBtnTarget.textContent = 'Import en cours\u2026';
|
||||
|
||||
const progress = data.totalBatches > 0
|
||||
? Math.round((data.processedBatches / data.totalBatches) * 100)
|
||||
: 0;
|
||||
|
||||
this._setStatus(`${progress}% — ${data.totalFilms} films`, 'active');
|
||||
this.badgeTarget.hidden = true;
|
||||
}
|
||||
|
||||
_showCompleted(data) {
|
||||
this.importBtnTarget.disabled = false;
|
||||
this.importBtnTarget.textContent = 'Importer ses films';
|
||||
|
||||
const imported = data.totalFilms - data.failedFilms;
|
||||
this._setStatus(`Dernier import : ${imported}/${data.totalFilms} films`, 'completed');
|
||||
this.badgeTarget.hidden = true;
|
||||
}
|
||||
|
||||
_showFailed() {
|
||||
this.importBtnTarget.disabled = false;
|
||||
this.importBtnTarget.textContent = 'Importer ses films';
|
||||
this._setStatus('Dernier import : échoué', 'failed');
|
||||
this.badgeTarget.hidden = true;
|
||||
}
|
||||
|
||||
_setStatus(text, type) {
|
||||
let statusEl = this.itemTarget.querySelector('.import-status-text');
|
||||
if (!statusEl) {
|
||||
statusEl = document.createElement('span');
|
||||
statusEl.className = 'import-status-text';
|
||||
this.itemTarget.appendChild(statusEl);
|
||||
}
|
||||
statusEl.textContent = text;
|
||||
statusEl.className = `import-status-text import-status-${type}`;
|
||||
}
|
||||
|
||||
_removeStatus() {
|
||||
const statusEl = this.itemTarget.querySelector('.import-status-text');
|
||||
if (statusEl) statusEl.remove();
|
||||
}
|
||||
}
|
||||
@@ -1,77 +0,0 @@
|
||||
import { Controller } from '@hotwired/stimulus';
|
||||
|
||||
export default class extends Controller {
|
||||
static targets = ['badge', 'list'];
|
||||
|
||||
connect() {
|
||||
this._poll();
|
||||
this._interval = setInterval(() => this._poll(), 30000);
|
||||
}
|
||||
|
||||
disconnect() {
|
||||
clearInterval(this._interval);
|
||||
}
|
||||
|
||||
async _poll() {
|
||||
try {
|
||||
const response = await fetch('/api/notifications');
|
||||
if (!response.ok) return;
|
||||
|
||||
const data = await response.json();
|
||||
this._updateBadge(data.unreadCount);
|
||||
this._updateTitle(data.unreadCount);
|
||||
this._updateList(data.notifications);
|
||||
} catch (e) {
|
||||
// silently ignore polling errors
|
||||
}
|
||||
}
|
||||
|
||||
_updateBadge(count) {
|
||||
if (count > 0) {
|
||||
this.badgeTarget.textContent = count;
|
||||
this.badgeTarget.hidden = false;
|
||||
} else {
|
||||
this.badgeTarget.hidden = true;
|
||||
}
|
||||
}
|
||||
|
||||
_updateTitle(count) {
|
||||
const base = 'Actorle';
|
||||
document.title = count > 0 ? `(${count}) ${base}` : base;
|
||||
}
|
||||
|
||||
_updateList(notifications) {
|
||||
if (notifications.length === 0) {
|
||||
this.listTarget.innerHTML = '<p class="dropdown-empty">Aucune notification</p>';
|
||||
return;
|
||||
}
|
||||
|
||||
this.listTarget.innerHTML = notifications.map(n => `
|
||||
<div class="notification-item ${n.read ? '' : 'notification-unread'}">
|
||||
<p>${this._escapeHtml(n.message)}</p>
|
||||
<time>${this._formatDate(n.createdAt)}</time>
|
||||
</div>
|
||||
`).join('');
|
||||
}
|
||||
|
||||
async markRead() {
|
||||
await fetch('/api/notifications/read', { method: 'POST' });
|
||||
this._poll();
|
||||
}
|
||||
|
||||
_escapeHtml(text) {
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
|
||||
_formatDate(isoString) {
|
||||
const date = new Date(isoString);
|
||||
return date.toLocaleDateString('fr-FR', {
|
||||
day: 'numeric',
|
||||
month: 'short',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user