Implement profile params via GetUserParam and add-block updates
This commit is contained in:
parent
3a412fcd51
commit
50e41a7fe0
@ -3,27 +3,21 @@ import { profile } from '../mock-data.js?v=20260405171816';
|
|||||||
import { state } from '../state.js?v=20260405171816';
|
import { state } from '../state.js?v=20260405171816';
|
||||||
import {
|
import {
|
||||||
loadProfileSnapshot,
|
loadProfileSnapshot,
|
||||||
profileFieldDefs,
|
saveProfileParamBlock,
|
||||||
saveProfileParams,
|
|
||||||
saveProfileToggle,
|
saveProfileToggle,
|
||||||
} from '../services/user-profile-params.js?v=20260405171816';
|
} from '../services/user-profile-params.js?v=20260405171816';
|
||||||
|
|
||||||
export const pageMeta = { id: 'profile-view', title: 'Профиль' };
|
export const pageMeta = { id: 'profile-view', title: 'Профиль' };
|
||||||
|
|
||||||
function formatDateTime(timeMs) {
|
function getDisplayName(fields) {
|
||||||
if (!timeMs) return 'ещё не заполнено';
|
const firstName = fields.find((field) => field.key === 'first_name')?.value?.trim() || '';
|
||||||
return new Date(timeMs).toLocaleString('ru-RU');
|
const lastName = fields.find((field) => field.key === 'last_name')?.value?.trim() || '';
|
||||||
}
|
|
||||||
|
|
||||||
function getDisplayName(fieldMap) {
|
|
||||||
const firstName = fieldMap.get('first_name')?.value?.trim() || '';
|
|
||||||
const lastName = fieldMap.get('last_name')?.value?.trim() || '';
|
|
||||||
const fullName = `${firstName} ${lastName}`.trim();
|
const fullName = `${firstName} ${lastName}`.trim();
|
||||||
return fullName || profile.name;
|
return fullName || profile.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleText(enabled) {
|
function toggleText(enabled) {
|
||||||
return enabled ? 'yes' : 'no';
|
return enabled ? 'Yes' : 'No';
|
||||||
}
|
}
|
||||||
|
|
||||||
export function render({ navigate }) {
|
export function render({ navigate }) {
|
||||||
@ -55,21 +49,21 @@ export function render({ navigate }) {
|
|||||||
<p class="meta-muted">${login}</p>
|
<p class="meta-muted">${login}</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<button class="primary-btn" type="button" data-open-edit="true">Обновить</button>
|
<button class="primary-btn" type="button" data-reload="true">Обновить</button>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const badgesRow = document.createElement('div');
|
const badgesRow = document.createElement('div');
|
||||||
badgesRow.className = 'row';
|
badgesRow.className = 'row';
|
||||||
badgesRow.innerHTML = `
|
badgesRow.innerHTML = `
|
||||||
<button class="badge profile-toggle-btn" type="button" data-toggle="official">✔ Официальный: no</button>
|
<button class="badge profile-toggle-btn is-no" type="button" data-toggle="official">Официальный: No</button>
|
||||||
<button class="badge alt profile-toggle-btn" type="button" data-toggle="shine">✨ Сияющий: no</button>
|
<button class="badge profile-toggle-btn is-no" type="button" data-toggle="shine">Сияющий: No</button>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const hint = document.createElement('div');
|
const hint = document.createElement('div');
|
||||||
hint.className = 'card profile-data-help';
|
hint.className = 'card profile-data-help';
|
||||||
hint.innerHTML = `
|
hint.innerHTML = `
|
||||||
<div class="meta-muted">Личные данные пользователя</div>
|
<div class="meta-muted">Личные данные пользователя</div>
|
||||||
<p>Поля ниже читаются из реальных пользовательских параметров сервера (ListUserParams). Любое изменение отправляется как блокчейн-запись параметра и требует подпись ключом пользователя.</p>
|
<p>Параметры читаются через API GetUserParam. Изменения записываются через ADD-блок с подписью blockchain key.</p>
|
||||||
`;
|
`;
|
||||||
|
|
||||||
const status = document.createElement('div');
|
const status = document.createElement('div');
|
||||||
@ -79,65 +73,57 @@ export function render({ navigate }) {
|
|||||||
const listWrap = document.createElement('div');
|
const listWrap = document.createElement('div');
|
||||||
listWrap.className = 'stack profile-param-list';
|
listWrap.className = 'stack profile-param-list';
|
||||||
|
|
||||||
const editModal = document.createElement('div');
|
|
||||||
editModal.className = 'profile-help-modal';
|
|
||||||
editModal.hidden = true;
|
|
||||||
editModal.innerHTML = `
|
|
||||||
<div class="profile-help-backdrop" data-close="true"></div>
|
|
||||||
<div class="profile-help-dialog card" role="dialog" aria-modal="true" aria-labelledby="profile-edit-title" tabindex="-1">
|
|
||||||
<div class="row" style="align-items:flex-start;">
|
|
||||||
<div>
|
|
||||||
<div class="meta-muted" style="margin-bottom:4px;">Обновление личных данных</div>
|
|
||||||
<h3 id="profile-edit-title" style="font-size:18px;">Редактирование профиля</h3>
|
|
||||||
</div>
|
|
||||||
<button class="icon-btn profile-help-close" type="button" aria-label="Закрыть">✕</button>
|
|
||||||
</div>
|
|
||||||
<p class="profile-help-text">После сохранения по каждому полю отправляется запись параметра в блокчейн. Для подписи используется ключ пользователя на устройстве.</p>
|
|
||||||
<form class="stack" data-profile-form="true"></form>
|
|
||||||
<div class="row">
|
|
||||||
<button class="ghost-btn" type="button" data-cancel-edit="true">Отмена</button>
|
|
||||||
<button class="primary-btn" type="button" data-save-profile="true">Сохранить</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
`;
|
|
||||||
|
|
||||||
const profileNameEl = topRow.querySelector('[data-profile-name="true"]');
|
const profileNameEl = topRow.querySelector('[data-profile-name="true"]');
|
||||||
const openEditBtn = topRow.querySelector('[data-open-edit="true"]');
|
const reloadBtn = topRow.querySelector('[data-reload="true"]');
|
||||||
const officialBtn = badgesRow.querySelector('[data-toggle="official"]');
|
const officialBtn = badgesRow.querySelector('[data-toggle="official"]');
|
||||||
const shineBtn = badgesRow.querySelector('[data-toggle="shine"]');
|
const shineBtn = badgesRow.querySelector('[data-toggle="shine"]');
|
||||||
const formEl = editModal.querySelector('[data-profile-form="true"]');
|
|
||||||
const dialogEl = editModal.querySelector('.profile-help-dialog');
|
|
||||||
const saveBtn = editModal.querySelector('[data-save-profile="true"]');
|
|
||||||
|
|
||||||
let currentFields = profileFieldDefs.map((field) => ({ ...field, value: '', timeMs: 0 }));
|
let currentFields = [];
|
||||||
let currentToggles = [
|
let currentToggles = [];
|
||||||
{ key: 'official', enabled: false, timeMs: 0 },
|
|
||||||
{ key: 'shine', enabled: false, timeMs: 0 },
|
function updateToggleButton(button, prefix, enabled) {
|
||||||
];
|
button.textContent = `${prefix}: ${toggleText(enabled)}`;
|
||||||
|
button.classList.remove('is-no', 'is-yes-official', 'is-yes-shine');
|
||||||
|
|
||||||
|
if (!enabled) {
|
||||||
|
button.classList.add('is-no');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (prefix === 'Официальный') {
|
||||||
|
button.classList.add('is-yes-official');
|
||||||
|
} else {
|
||||||
|
button.classList.add('is-yes-shine');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function updateTogglesUi() {
|
function updateTogglesUi() {
|
||||||
const official = currentToggles.find((item) => item.key === 'official') || { enabled: false };
|
const official = currentToggles.find((item) => item.key === 'official') || { enabled: false };
|
||||||
const shine = currentToggles.find((item) => item.key === 'shine') || { enabled: false };
|
const shine = currentToggles.find((item) => item.key === 'shine') || { enabled: false };
|
||||||
|
updateToggleButton(officialBtn, 'Официальный', official.enabled);
|
||||||
officialBtn.textContent = `✔ Официальный: ${toggleText(official.enabled)}`;
|
updateToggleButton(shineBtn, 'Сияющий', shine.enabled);
|
||||||
shineBtn.textContent = `✨ Сияющий: ${toggleText(shine.enabled)}`;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function renderFields(fields) {
|
function renderFields(fields) {
|
||||||
const fieldMap = new Map(fields.map((field) => [field.key, field]));
|
profileNameEl.textContent = getDisplayName(fields);
|
||||||
profileNameEl.textContent = getDisplayName(fieldMap);
|
|
||||||
|
|
||||||
|
const readyFields = fields.filter((field) => String(field.value || '').trim());
|
||||||
listWrap.innerHTML = '';
|
listWrap.innerHTML = '';
|
||||||
fields.forEach((field) => {
|
|
||||||
|
if (!readyFields.length) {
|
||||||
|
const emptyState = document.createElement('div');
|
||||||
|
emptyState.className = 'meta-muted';
|
||||||
|
emptyState.textContent = 'Пока нет заполненных персональных параметров.';
|
||||||
|
listWrap.append(emptyState);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
readyFields.forEach((field) => {
|
||||||
const row = document.createElement('div');
|
const row = document.createElement('div');
|
||||||
row.className = 'card profile-param-item';
|
row.className = 'card profile-param-item row';
|
||||||
row.innerHTML = `
|
row.innerHTML = `
|
||||||
<div class="profile-param-head">
|
<div class="profile-param-value"><b>${field.label}</b>: ${field.value}</div>
|
||||||
<span class="meta-muted">${field.label}</span>
|
<button class="ghost-btn" type="button" data-edit-field="${field.key}">Изменить</button>
|
||||||
<span class="meta-muted">${field.key}</span>
|
|
||||||
</div>
|
|
||||||
<div class="profile-param-value">${field.value || '—'}</div>
|
|
||||||
<div class="meta-muted profile-param-time">Обновлено: ${formatDateTime(field.timeMs)}</div>
|
|
||||||
`;
|
`;
|
||||||
listWrap.append(row);
|
listWrap.append(row);
|
||||||
});
|
});
|
||||||
@ -146,7 +132,7 @@ export function render({ navigate }) {
|
|||||||
async function refreshProfileSnapshot() {
|
async function refreshProfileSnapshot() {
|
||||||
status.className = 'status-line';
|
status.className = 'status-line';
|
||||||
status.textContent = 'Загрузка параметров...';
|
status.textContent = 'Загрузка параметров...';
|
||||||
openEditBtn.disabled = true;
|
reloadBtn.disabled = true;
|
||||||
officialBtn.disabled = true;
|
officialBtn.disabled = true;
|
||||||
shineBtn.disabled = true;
|
shineBtn.disabled = true;
|
||||||
|
|
||||||
@ -154,77 +140,35 @@ export function render({ navigate }) {
|
|||||||
const snapshot = await loadProfileSnapshot(login);
|
const snapshot = await loadProfileSnapshot(login);
|
||||||
currentFields = snapshot.fields;
|
currentFields = snapshot.fields;
|
||||||
currentToggles = snapshot.toggles;
|
currentToggles = snapshot.toggles;
|
||||||
renderFields(snapshot.fields);
|
|
||||||
updateTogglesUi();
|
|
||||||
status.className = 'status-line is-available';
|
|
||||||
status.textContent = 'Актуальные параметры загружены с сервера.';
|
|
||||||
} catch (error) {
|
|
||||||
renderFields(currentFields);
|
renderFields(currentFields);
|
||||||
updateTogglesUi();
|
updateTogglesUi();
|
||||||
|
|
||||||
|
status.className = 'status-line is-available';
|
||||||
|
status.textContent = 'Актуальные параметры загружены.';
|
||||||
|
} catch (error) {
|
||||||
status.className = 'status-line is-unavailable';
|
status.className = 'status-line is-unavailable';
|
||||||
status.textContent = `Не удалось загрузить параметры: ${error.message || 'ошибка сети'}`;
|
status.textContent = `Не удалось загрузить параметры: ${error.message || 'ошибка сети'}`;
|
||||||
} finally {
|
} finally {
|
||||||
openEditBtn.disabled = false;
|
reloadBtn.disabled = false;
|
||||||
officialBtn.disabled = false;
|
officialBtn.disabled = false;
|
||||||
shineBtn.disabled = false;
|
shineBtn.disabled = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function closeEditModal() {
|
|
||||||
editModal.hidden = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
function openEditModal() {
|
|
||||||
formEl.innerHTML = '';
|
|
||||||
|
|
||||||
currentFields.forEach((field) => {
|
|
||||||
const fieldWrap = document.createElement('label');
|
|
||||||
fieldWrap.className = 'stack';
|
|
||||||
fieldWrap.innerHTML = `
|
|
||||||
<span class="field-label">${field.label}</span>
|
|
||||||
<input class="input" type="text" name="${field.key}" placeholder="${field.placeholder || ''}" value="${field.value || ''}" />
|
|
||||||
`;
|
|
||||||
formEl.append(fieldWrap);
|
|
||||||
});
|
|
||||||
|
|
||||||
editModal.hidden = false;
|
|
||||||
dialogEl.focus();
|
|
||||||
}
|
|
||||||
|
|
||||||
async function saveChanges() {
|
|
||||||
const valuesByKey = {};
|
|
||||||
currentFields.forEach((field) => {
|
|
||||||
const input = formEl.querySelector(`input[name="${field.key}"]`);
|
|
||||||
valuesByKey[field.key] = input instanceof HTMLInputElement ? input.value : '';
|
|
||||||
});
|
|
||||||
|
|
||||||
saveBtn.disabled = true;
|
|
||||||
try {
|
|
||||||
await saveProfileParams(login, valuesByKey);
|
|
||||||
closeEditModal();
|
|
||||||
await refreshProfileSnapshot();
|
|
||||||
} catch (error) {
|
|
||||||
status.className = 'status-line is-unavailable';
|
|
||||||
status.textContent = `Не удалось сохранить: ${error.message || 'ошибка сети'}`;
|
|
||||||
} finally {
|
|
||||||
saveBtn.disabled = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function onToggleClick(toggleKey) {
|
async function onToggleClick(toggleKey) {
|
||||||
const toggle = currentToggles.find((item) => item.key === toggleKey) || { enabled: false };
|
const toggle = currentToggles.find((item) => item.key === toggleKey) || { enabled: false };
|
||||||
const nextEnabled = !toggle.enabled;
|
const nextEnabled = !toggle.enabled;
|
||||||
const title = toggleKey === 'official' ? 'Официальный аккаунт' : 'Сияющий аккаунт';
|
const title = toggleKey === 'official' ? 'официальный' : 'сияющий';
|
||||||
|
|
||||||
const confirmed = window.confirm(
|
const confirmed = window.confirm(
|
||||||
`Изменить параметр «${title}» на ${toggleText(nextEnabled)}?\n\n` +
|
`Хотите изменить «${title}» на ${toggleText(nextEnabled)}?\n` +
|
||||||
'Внимание: изменение будет записано как блокчейн-параметр пользователя и требует подписи ключом блокчейна/пользователя на устройстве.',
|
'Будет создана запись в блокчейне (ADD-блок).',
|
||||||
);
|
);
|
||||||
|
|
||||||
if (!confirmed) return;
|
if (!confirmed) return;
|
||||||
|
|
||||||
status.className = 'status-line';
|
status.className = 'status-line';
|
||||||
status.textContent = 'Отправка изменения в блокчейн...';
|
status.textContent = 'Сохранение в блокчейн...';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await saveProfileToggle(login, toggleKey, nextEnabled);
|
await saveProfileToggle(login, toggleKey, nextEnabled);
|
||||||
@ -235,25 +179,44 @@ export function render({ navigate }) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
openEditBtn.addEventListener('click', openEditModal);
|
async function onEditFieldClick(fieldKey) {
|
||||||
saveBtn.addEventListener('click', saveChanges);
|
const field = currentFields.find((item) => item.key === fieldKey);
|
||||||
|
if (!field) return;
|
||||||
|
|
||||||
|
const entered = window.prompt(`Введите новое значение для «${field.label}»:`, field.value || '');
|
||||||
|
if (entered === null) return;
|
||||||
|
|
||||||
|
const confirmed = window.confirm(
|
||||||
|
`Записать новое значение параметра «${field.label}» в блокчейн?`,
|
||||||
|
);
|
||||||
|
if (!confirmed) return;
|
||||||
|
|
||||||
|
status.className = 'status-line';
|
||||||
|
status.textContent = 'Сохранение в блокчейн...';
|
||||||
|
|
||||||
|
try {
|
||||||
|
await saveProfileParamBlock(login, field.key, entered);
|
||||||
|
await refreshProfileSnapshot();
|
||||||
|
} catch (error) {
|
||||||
|
status.className = 'status-line is-unavailable';
|
||||||
|
status.textContent = `Не удалось изменить ${field.key}: ${error.message || 'ошибка сети'}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
listWrap.addEventListener('click', (event) => {
|
||||||
|
const target = event.target;
|
||||||
|
if (!(target instanceof HTMLElement)) return;
|
||||||
|
const fieldKey = target.dataset.editField;
|
||||||
|
if (!fieldKey) return;
|
||||||
|
onEditFieldClick(fieldKey);
|
||||||
|
});
|
||||||
|
|
||||||
|
reloadBtn.addEventListener('click', refreshProfileSnapshot);
|
||||||
officialBtn.addEventListener('click', () => onToggleClick('official'));
|
officialBtn.addEventListener('click', () => onToggleClick('official'));
|
||||||
shineBtn.addEventListener('click', () => onToggleClick('shine'));
|
shineBtn.addEventListener('click', () => onToggleClick('shine'));
|
||||||
editModal.querySelector('[data-cancel-edit="true"]').addEventListener('click', closeEditModal);
|
|
||||||
|
|
||||||
editModal.addEventListener('click', (event) => {
|
|
||||||
const target = event.target;
|
|
||||||
if (target instanceof HTMLElement && (target.dataset.close === 'true' || target.classList.contains('profile-help-close'))) {
|
|
||||||
closeEditModal();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
editModal.addEventListener('keydown', (event) => {
|
|
||||||
if (event.key === 'Escape') closeEditModal();
|
|
||||||
});
|
|
||||||
|
|
||||||
card.append(topRow, badgesRow, hint, status, listWrap);
|
card.append(topRow, badgesRow, hint, status, listWrap);
|
||||||
screen.append(card, editModal);
|
screen.append(card);
|
||||||
|
|
||||||
refreshProfileSnapshot();
|
refreshProfileSnapshot();
|
||||||
|
|
||||||
|
|||||||
@ -289,6 +289,52 @@ export class AuthService {
|
|||||||
return response.payload?.logins || [];
|
return response.payload?.logins || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
async getUserParam(login, param) {
|
||||||
|
const cleanLogin = (login || '').trim();
|
||||||
|
const cleanParam = (param || '').trim();
|
||||||
|
if (!cleanLogin || !cleanParam) throw new Error('Не переданы login/param');
|
||||||
|
|
||||||
|
const response = await this.ws.request('GetUserParam', { login: cleanLogin, param: cleanParam });
|
||||||
|
if (response.status === 200) return response.payload || {};
|
||||||
|
|
||||||
|
if (response.status === 404 || response.status === 204) return {};
|
||||||
|
|
||||||
|
throw opError('GetUserParam', response);
|
||||||
|
}
|
||||||
|
|
||||||
|
async addUserParamBlock({ login, param, value, timeMs = Date.now(), storagePwd }) {
|
||||||
|
const cleanLogin = (login || '').trim();
|
||||||
|
const cleanParam = (param || '').trim();
|
||||||
|
if (!cleanLogin || !cleanParam) throw new Error('Не переданы login/param');
|
||||||
|
if (!storagePwd) throw new Error('Не передан storagePwd для подписи блокчейна');
|
||||||
|
|
||||||
|
const user = await this.getUser(cleanLogin);
|
||||||
|
const blockchainKey = String(user?.blockchainKey || '').trim();
|
||||||
|
if (!blockchainKey) throw new Error('GetUser не вернул blockchainKey');
|
||||||
|
|
||||||
|
const savedKeys = await loadEncryptedUserSecrets(cleanLogin, storagePwd);
|
||||||
|
const blockchainPrivatePkcs8 = savedKeys?.blockchainKey;
|
||||||
|
if (!blockchainPrivatePkcs8) throw new Error('На устройстве нет сохраненного приватного blockchainKey');
|
||||||
|
|
||||||
|
const privateKey = await importPkcs8Ed25519(blockchainPrivatePkcs8);
|
||||||
|
const cleanValue = String(value ?? '');
|
||||||
|
const signText = `${USER_PARAMETER_PREFIX}${cleanLogin}${cleanParam}${timeMs}${cleanValue}`;
|
||||||
|
const signature = await signBase64(privateKey, signText);
|
||||||
|
|
||||||
|
const response = await this.ws.request('AddUserParamBlock', {
|
||||||
|
login: cleanLogin,
|
||||||
|
param: cleanParam,
|
||||||
|
time_ms: Number(timeMs),
|
||||||
|
value: cleanValue,
|
||||||
|
blockchain_key: blockchainKey,
|
||||||
|
signature,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (response.status !== 200) throw opError('AddUserParamBlock', response);
|
||||||
|
return response.payload || {};
|
||||||
|
}
|
||||||
async listUserParams(login) {
|
async listUserParams(login) {
|
||||||
const cleanLogin = (login || '').trim();
|
const cleanLogin = (login || '').trim();
|
||||||
if (!cleanLogin) throw new Error('Не передан login');
|
if (!cleanLogin) throw new Error('Не передан login');
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
import { authService, state } from '../state.js?v=20260403081123';
|
import { authService, state } from '../state.js?v=20260403081123';
|
||||||
|
|
||||||
export const profileFieldDefs = [
|
export const profileFieldDefs = [
|
||||||
{ key: 'first_name', readKeys: ['first_name', 'name'], label: 'First name', placeholder: 'Введите имя' },
|
{ key: 'first_name', readKeys: ['first_name', 'ашкые_name'], label: 'Имя', placeholder: 'Введите имя' },
|
||||||
{ key: 'last_name', readKeys: ['last_name'], label: 'Last name', placeholder: 'Введите фамилию' },
|
{ key: 'last_name', readKeys: ['last_name'], label: 'Фамилия', placeholder: 'Введите фамилию' },
|
||||||
{ key: 'address_physical', readKeys: ['address_physical'], label: 'Address physical', placeholder: 'Город, улица, дом' },
|
{ key: 'address_physical', readKeys: ['address_physical'], label: 'Физический адрес', placeholder: 'Город, улица, дом' },
|
||||||
{ key: 'address_web', readKeys: ['address_web'], label: 'Address web', placeholder: 'Сайт или профиль' },
|
{ key: 'address_web', readKeys: ['address_web'], label: 'Веб-адрес', placeholder: 'Сайт или профиль' },
|
||||||
{ key: 'phone', readKeys: ['phone'], label: 'Phone', placeholder: '+7 ...' },
|
{ key: 'phone', readKeys: ['phone'], label: 'Телефон', placeholder: '+7 ...' },
|
||||||
];
|
];
|
||||||
|
|
||||||
export const profileToggleDefs = [
|
export const profileToggleDefs = [
|
||||||
@ -13,27 +13,29 @@ export const profileToggleDefs = [
|
|||||||
{ key: 'shine', label: 'Сияющий' },
|
{ key: 'shine', label: 'Сияющий' },
|
||||||
];
|
];
|
||||||
|
|
||||||
function normalizeItems(responsePayload) {
|
function normalizeItem(param, payload) {
|
||||||
const params = responsePayload?.params;
|
if (!param) return null;
|
||||||
if (!Array.isArray(params)) return [];
|
|
||||||
return params
|
|
||||||
.map((item) => ({
|
|
||||||
param: String(item?.param || '').trim(),
|
|
||||||
value: String(item?.value || ''),
|
|
||||||
timeMs: Number(item?.time_ms || 0),
|
|
||||||
}))
|
|
||||||
.filter((item) => item.param);
|
|
||||||
}
|
|
||||||
|
|
||||||
function getLatestByAliases(items, aliases) {
|
if (Array.isArray(payload?.params)) {
|
||||||
let latest = null;
|
const latest = payload.params
|
||||||
items.forEach((item) => {
|
.map((item) => ({
|
||||||
if (!aliases.includes(item.param)) return;
|
value: String(item?.value || ''),
|
||||||
if (!latest || item.timeMs >= latest.timeMs) {
|
timeMs: Number(item?.time_ms || 0),
|
||||||
latest = item;
|
}))
|
||||||
}
|
.sort((a, b) => b.timeMs - a.timeMs)[0];
|
||||||
});
|
|
||||||
return latest;
|
if (!latest) return null;
|
||||||
|
return { param, value: latest.value, timeMs: latest.timeMs };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (payload && typeof payload === 'object') {
|
||||||
|
const value = String(payload?.value || payload?.param_value || '');
|
||||||
|
const timeMs = Number(payload?.time_ms || payload?.timeMs || 0);
|
||||||
|
if (!value && !timeMs) return null;
|
||||||
|
return { param, value, timeMs };
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseToggleValue(value) {
|
function parseToggleValue(value) {
|
||||||
@ -49,54 +51,68 @@ async function getStoragePwd() {
|
|||||||
return storagePwd;
|
return storagePwd;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function loadProfileSnapshot(login) {
|
async function loadLatestByAliases(login, aliases) {
|
||||||
const payload = await authService.listUserParams(login);
|
const collected = [];
|
||||||
const items = normalizeItems(payload);
|
|
||||||
|
|
||||||
const fields = profileFieldDefs.map((field) => {
|
for (let i = 0; i < aliases.length; i += 1) {
|
||||||
const latest = getLatestByAliases(items, field.readKeys);
|
const alias = aliases[i];
|
||||||
return {
|
try {
|
||||||
|
const payload = await authService.getUserParam(login, alias);
|
||||||
|
const normalized = normalizeItem(alias, payload);
|
||||||
|
if (normalized) collected.push(normalized);
|
||||||
|
} catch {
|
||||||
|
// Пусто — параметр ещё не создан или endpoint не отвечает для конкретного ключа.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!collected.length) return null;
|
||||||
|
return collected.sort((a, b) => b.timeMs - a.timeMs)[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function loadProfileSnapshot(login) {
|
||||||
|
const fields = [];
|
||||||
|
for (let i = 0; i < profileFieldDefs.length; i += 1) {
|
||||||
|
const field = profileFieldDefs[i];
|
||||||
|
const latest = await loadLatestByAliases(login, field.readKeys);
|
||||||
|
fields.push({
|
||||||
key: field.key,
|
key: field.key,
|
||||||
label: field.label,
|
label: field.label,
|
||||||
placeholder: field.placeholder,
|
placeholder: field.placeholder,
|
||||||
value: latest?.value || '',
|
value: latest?.value || '',
|
||||||
timeMs: latest?.timeMs || 0,
|
timeMs: latest?.timeMs || 0,
|
||||||
};
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
const toggles = profileToggleDefs.map((toggle) => {
|
const toggles = [];
|
||||||
const latest = getLatestByAliases(items, [toggle.key]);
|
for (let i = 0; i < profileToggleDefs.length; i += 1) {
|
||||||
return {
|
const toggle = profileToggleDefs[i];
|
||||||
|
const latest = await loadLatestByAliases(login, [toggle.key]);
|
||||||
|
toggles.push({
|
||||||
key: toggle.key,
|
key: toggle.key,
|
||||||
label: toggle.label,
|
label: toggle.label,
|
||||||
enabled: latest ? parseToggleValue(latest.value) : false,
|
enabled: latest ? parseToggleValue(latest.value) : false,
|
||||||
rawValue: latest?.value || 'no',
|
rawValue: latest?.value || 'no',
|
||||||
timeMs: latest?.timeMs || 0,
|
timeMs: latest?.timeMs || 0,
|
||||||
};
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
return { fields, toggles };
|
return { fields, toggles };
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function saveProfileParams(login, valuesByKey) {
|
export async function saveProfileParamBlock(login, key, value) {
|
||||||
const storagePwd = await getStoragePwd();
|
const storagePwd = await getStoragePwd();
|
||||||
const baseTime = Date.now();
|
await authService.addUserParamBlock({
|
||||||
|
login,
|
||||||
for (let i = 0; i < profileFieldDefs.length; i += 1) {
|
param: key,
|
||||||
const field = profileFieldDefs[i];
|
value: String(value ?? '').trim(),
|
||||||
await authService.upsertUserParam({
|
timeMs: Date.now(),
|
||||||
login,
|
storagePwd,
|
||||||
param: field.key,
|
});
|
||||||
value: String(valuesByKey[field.key] || '').trim(),
|
|
||||||
timeMs: baseTime + i,
|
|
||||||
storagePwd,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function saveProfileToggle(login, key, enabled) {
|
export async function saveProfileToggle(login, key, enabled) {
|
||||||
const storagePwd = await getStoragePwd();
|
const storagePwd = await getStoragePwd();
|
||||||
await authService.upsertUserParam({
|
await authService.addUserParamBlock({
|
||||||
login,
|
login,
|
||||||
param: key,
|
param: key,
|
||||||
value: enabled ? 'yes' : 'no',
|
value: enabled ? 'yes' : 'no',
|
||||||
|
|||||||
@ -97,6 +97,24 @@
|
|||||||
background: rgba(83, 216, 251, 0.11);
|
background: rgba(83, 216, 251, 0.11);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.badge.profile-toggle-btn.is-no {
|
||||||
|
border-color: rgba(170, 180, 205, 0.3);
|
||||||
|
color: #c5cedd;
|
||||||
|
background: rgba(152, 164, 190, 0.14);
|
||||||
|
}
|
||||||
|
|
||||||
|
.badge.profile-toggle-btn.is-yes-official {
|
||||||
|
border-color: rgba(132, 244, 161, 0.5);
|
||||||
|
color: #ddffe7;
|
||||||
|
background: rgba(132, 244, 161, 0.2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.badge.profile-toggle-btn.is-yes-shine {
|
||||||
|
border-color: rgba(183, 122, 255, 0.6);
|
||||||
|
color: #f4e7ff;
|
||||||
|
background: rgba(176, 102, 255, 0.22);
|
||||||
|
}
|
||||||
|
|
||||||
.profile-help-modal[hidden] {
|
.profile-help-modal[hidden] {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user