Files
smeta-web/static/admin2/pages/customer.js
T

64 lines
3.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { h } from '/static/admin2/vendor/preact.module.js';
import { useEffect, 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';
const html = htm.bind(h);
const rub = (v) => v == null ? '—' : Number(v).toLocaleString('ru-RU',
{minimumFractionDigits: 2, maximumFractionDigits: 2}) + ' ₽';
const when = (ts) => ts ? new Date(ts * 1000).toLocaleString('ru-RU') : '—';
const TABS = [['overview', 'Обзор'], ['dialogs', 'Диалоги'], ['ledger', 'Счёт'],
['files', 'Файлы'], ['history', 'История']];
export function CustomerDrawer({ email, onClose }) {
const [card, setCard] = useState(null);
const [tab, setTab] = useState('overview');
const [rows, setRows] = useState([]);
const [error, setError] = useState('');
const [loading, setLoading] = useState(false);
useEffect(() => {
let alive = true;
setCard(null); setError(''); 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]);
useEffect(() => {
if (tab === 'overview' || !card) return;
let alive = true;
const suffix = {dialogs: 'dialogs', ledger: 'ledger', files: 'files', history: 'history'}[tab];
setError(''); setLoading(true);
api('/api/admin/v2/customers/' + encodeURIComponent(email) + '/' + suffix)
.then((d) => { if (alive) setRows(d.items || d.entries || d); })
.catch((e) => { if (alive) setError(e.message || 'Не удалось загрузить вкладку'); })
.finally(() => { if (alive) setLoading(false); });
return () => { alive = false; };
}, [tab, card, email]);
const title = card ? card.profile.email : 'Карточка клиента';
return html`
<${Drawer} title=${title} onClose=${onClose}>
${error ? html`<p class="error" role="alert">${error}</p>` : 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>
${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>
<div><span>Доступ</span><b>${card.profile.agent_access ? 'агент' : 'справка'} · ${card.profile.agent_provider || 'по умолч.'}</b></div>
<div><span>Был(а)</span><b>${when(card.activity.last_seen)}</b></div>
<div><span>Сообщений</span><b>${card.activity.messages}</b></div>
<div><span>Файлов</span><b>${card.activity.files}${card.activity.files_capped ? '+' : ''}</b></div>
</div>` : html`<div class="tab-body">${loading
? html`<p class="note">Загрузка…</p>`
: rows.length === 0 ? html`<p class="note">Пусто.</p>`
: html`<pre class="json">${JSON.stringify(rows.slice(0, 20), null, 2)}</pre>`}</div>`}
`}
</${Drawer}>`;
}