feat(админка): действия в карточке и тост отмены

This commit is contained in:
anor
2026-11-06 22:01:23 +10:00
parent e15369c4d4
commit 0e9a95e95b
9 changed files with 166 additions and 9 deletions
+5 -1
View File
@@ -9,6 +9,10 @@ export async function api(path, opts = {}) {
if (opts.body && !headers['Content-Type']) headers['Content-Type'] = 'application/json';
const r = await fetch(path, {...opts, headers});
if (r.status === 401 || r.status === 403) throw new ApiError(r.status, 'Нужен вход оператора');
if (!r.ok) throw new ApiError(r.status, 'Ошибка ' + r.status);
if (!r.ok) {
let detail = '';
try { detail = String((await r.json()).detail || ''); } catch (e) { /* не JSON */ }
throw new ApiError(r.status, detail || ('Ошибка ' + r.status));
}
return r.json();
}
+49 -3
View File
@@ -1,8 +1,9 @@
import { h } from '/static/admin2/vendor/preact.module.js';
import { useEffect, useState } from '/static/admin2/vendor/hooks.module.js';
import { useEffect, useRef, useState } from '/static/admin2/vendor/hooks.module.js';
import htm from '/static/admin2/vendor/htm.module.js';
import { api } from '/static/admin2/api.js';
import { Drawer } from '/static/admin2/ui/drawer.js';
import { Toast } from '/static/admin2/ui/toast.js';
const html = htm.bind(h);
const rub = (v) => v == null ? '—' : Number(v).toLocaleString('ru-RU',
@@ -17,15 +18,24 @@ export function CustomerDrawer({ email, onClose }) {
const [rows, setRows] = useState([]);
const [error, setError] = useState('');
const [loading, setLoading] = useState(false);
const [reload, setReload] = useState(0);
const [undoId, setUndoId] = useState(null);
const [notice, setNotice] = useState('');
const [busy, setBusy] = useState(false);
const prevEmail = useRef(email);
useEffect(() => {
let alive = true;
setCard(null); setError(''); setTab('overview'); setRows([]); setLoading(false);
const switched = prevEmail.current !== email;
prevEmail.current = email;
setCard(null); setError('');
if (switched) { setTab('overview'); setRows([]); }
setLoading(false);
api('/api/admin/v2/customers/' + encodeURIComponent(email))
.then((d) => { if (alive) setCard(d); })
.catch((e) => { if (alive) setError(e.message || 'Не удалось загрузить'); });
return () => { alive = false; };
}, [email]);
}, [email, reload]);
useEffect(() => {
if (tab === 'overview' || !card) return;
@@ -39,14 +49,50 @@ export function CustomerDrawer({ email, onClose }) {
return () => { alive = false; };
}, [tab, card, email]);
const act = async (action, value, label) => {
setBusy(true); setNotice(''); setUndoId(null);
try {
const r = await api('/api/admin/v2/customers/' + encodeURIComponent(email) + '/action',
{method: 'POST', body: JSON.stringify({action, value})});
setUndoId(r.id); setNotice(label); setReload((n) => n + 1);
} catch (e) {
setNotice(e.message || 'Не выполнено');
} finally { setBusy(false); }
};
const undo = async () => {
if (!undoId) return;
setBusy(true);
try {
await api('/api/admin/v2/actions/' + undoId + '/undo', {method: 'POST'});
setNotice('Отменено'); setUndoId(null); setReload((n) => n + 1);
} catch (e) { setNotice(e.message || 'Не удалось отменить'); }
finally { setBusy(false); }
};
const title = card ? card.profile.email : 'Карточка клиента';
return html`
<${Drawer} title=${title} onClose=${onClose}>
${error ? html`<p class="error" role="alert">${error}</p>` : null}
${notice ? html`<${Toast} message=${notice} onUndo=${undoId ? undo : null}
onClose=${() => { setNotice(''); setUndoId(null); }} />` : null}
${!card ? html`<p class="note">Загрузка…</p>` : html`
<nav class="tabs">${TABS.map(([k, label]) => html`
<button type="button" key=${k} class=${k === tab ? 'tab active' : 'tab'}
onClick=${() => { setRows([]); setTab(k); }}>${label}</button>`)}</nav>
${!card.profile.deleted_at ? html`<div class="actions-bar">
<button type="button" disabled=${busy} onClick=${() => act('block',
{blocked: !card.profile.blocked}, card.profile.blocked ? 'Разблокирован' : 'Заблокирован')}>
${card.profile.blocked ? 'Разблокировать' : 'Заблокировать'}</button>
<button type="button" disabled=${busy} onClick=${() => act('read_only',
{read_only: !card.profile.read_only}, card.profile.read_only ? 'Работа открыта' : 'Только просмотр')}>
${card.profile.read_only ? 'Открыть работу' : 'Только просмотр'}</button>
<button type="button" disabled=${busy} onClick=${() => act('role',
{role: card.profile.role === 'admin' ? 'user' : 'admin'},
card.profile.role === 'admin' ? 'Теперь user' : 'Теперь admin')}>
${card.profile.role === 'admin' ? 'Сделать user' : 'Сделать admin'}</button>
<button type="button" disabled=${busy} onClick=${() => act('agent_access',
{agent_access: !card.profile.agent_access}, card.profile.agent_access ? 'Доступ снят' : 'Доступ выдан')}>
${card.profile.agent_access ? 'Снять агента' : 'Выдать агента'}</button>
</div>` : null}
${tab === 'overview' ? html`<div class="kv">
<div><span>Статус</span><b>${card.profile.deleted_at ? 'удалён' : card.profile.blocked ? 'заблокирован' : 'активен'}</b></div>
<div><span>Баланс</span><b>${rub(card.account && card.account.balance_rub)}</b></div>
+7
View File
@@ -60,3 +60,10 @@ tr.clickable{cursor:pointer} tr.clickable:hover td{background:#f3f6ff}
.kv div{display:flex;justify-content:space-between;gap:8px;border-bottom:1px solid var(--line);padding:6px 0}
.kv span{color:var(--muted)}
.json{white-space:pre-wrap;font-size:12px;background:#f8fafc;padding:10px;border-radius:8px;overflow:auto}
.actions-bar{display:flex;gap:6px;flex-wrap:wrap;margin:10px 0}
.actions-bar button{font:inherit;padding:6px 10px;border:1px solid var(--line);border-radius:8px;background:#fff;cursor:pointer}
.actions-bar button:disabled{opacity:.5}
.toast{display:flex;gap:8px;align-items:center;background:#ecfdf3;border:1px solid #abefc6;color:#067647;padding:8px 10px;border-radius:8px;margin:8px 0}
.toast button{font:inherit;border:0;background:transparent;color:#067647;text-decoration:underline;cursor:pointer}
.toast-close{margin-left:auto;text-decoration:none;font-size:18px}
+10
View File
@@ -0,0 +1,10 @@
import { h } from '/static/admin2/vendor/preact.module.js';
import htm from '/static/admin2/vendor/htm.module.js';
const html = htm.bind(h);
export const Toast = ({ message, onUndo, onClose }) => html`
<div class="toast" role="status">
<span>${message}</span>
${onUndo && html`<button type="button" onClick=${onUndo}>Отменить</button>`}
<button type="button" class="toast-close" onClick=${onClose} aria-label="Закрыть">×</button>
</div>`;