feat(админка): действия в карточке и тост отмены
This commit is contained in:
@@ -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>
|
||||
|
||||
Reference in New Issue
Block a user