feat: add cat identity rename flow

This commit is contained in:
nyan404 committed 2026-10-13 00:30:36 +03:00
1 parent a15c22b4a2
commit 546fa37fce
10 files changed
+203 -14

No files matched your search

+80 -8
View File
@@ -103,6 +103,7 @@
<nav class="tabs section-tabs" role="tablist" aria-label="Раздел Беличьей норы">
<button class="active" id="reportsTab" type="button" role="tab" aria-selected="true" aria-controls="reportsSection" data-section="reports">Отчёты</button>
<button id="applicationsTab" type="button" role="tab" aria-selected="false" aria-controls="applicationsSection" data-section="applications">Заявки</button>
<button id="identityTab" type="button" role="tab" aria-selected="false" aria-controls="identitySection" data-section="identity">Имя кота</button>
</nav>
<section id="reportsSection" role="tabpanel" aria-labelledby="reportsTab">
@@ -248,6 +249,26 @@
<div id="applicationSuccess" class="success hidden" role="status" aria-live="polite"></div>
</div>
</section>
<section id="identitySection" class="hidden" role="tabpanel" aria-labelledby="identityTab">
<div id="identityWorkspace">
<h2>Изменить имя кота</h2>
<p class="muted">Укажите ID из существующей связки, проверьте текущее имя и сохраните новое.</p>
<form id="renameIdentityForm" class="panel">
<div class="grid">
<label>ID кота<input id="renameIdentityId" name="character_id" type="number" min="1" inputmode="numeric" required></label>
<label>Текущее имя<input id="renameIdentityCurrentName" type="text" readonly aria-readonly="true" placeholder="Сначала проверьте ID"></label>
<label class="wide">Новое имя<input id="renameIdentityNewName" name="new_name" type="text" maxlength="100" required></label>
</div>
<p class="muted" id="renameIdentityLookupStatus" role="status" aria-live="polite"></p>
<p class="error" id="renameIdentityError" role="alert"></p>
<div class="actions">
<button type="button" id="lookupIdentity">Проверить ID</button>
<button type="submit" id="renameIdentitySubmit" class="primary">Сохранить новое имя</button>
</div>
</form>
<div id="renameIdentitySuccess" class="success hidden" role="status" aria-live="polite"></div>
</div>
</section>
<footer class="site-footer">Proudly made by Nyan404 | <a href="https://git.nyan404.ru/nyan404/cwshed-clone">GitLab Page</a></footer>
</main>
<dialog id="identityDialog">
@@ -527,6 +548,7 @@
const reportsSection = document.querySelector('#reportsSection');
const applicationsSection = document.querySelector('#applicationsSection');
const identitySection = document.querySelector('#identitySection');
const sectionTabs = [...document.querySelectorAll('[data-section]')];
const applicationForm = document.querySelector('#applicationForm');
const applicationError = document.querySelector('#applicationError');
@@ -534,26 +556,76 @@
const applicationSubmit = document.querySelector('#applicationSubmit');
const applicationFeathers = document.querySelector('#applicationFeathers');
let applicationType = 'feather_donation';
const renameIdentityForm = document.querySelector('#renameIdentityForm');
const renameIdentityId = document.querySelector('#renameIdentityId');
const renameIdentityCurrentName = document.querySelector('#renameIdentityCurrentName');
const renameIdentityNewName = document.querySelector('#renameIdentityNewName');
const lookupIdentity = document.querySelector('#lookupIdentity');
const renameIdentitySubmit = document.querySelector('#renameIdentitySubmit');
const renameIdentityLookupStatus = document.querySelector('#renameIdentityLookupStatus');
const renameIdentityError = document.querySelector('#renameIdentityError');
const renameIdentitySuccess = document.querySelector('#renameIdentitySuccess');
// Changes between reports and applications while optionally updating the URL.
// Changes between reports, applications, and identity management while optionally updating the URL.
function setSection(nextSection, updateUrl = true) {
const isApplications = nextSection === 'applications';
reportsSection.classList.toggle('hidden', isApplications);
applicationsSection.classList.toggle('hidden', !isApplications);
const selectedSection = ['reports', 'applications', 'identity'].includes(nextSection) ? nextSection : 'reports';
reportsSection.classList.toggle('hidden', selectedSection !== 'reports');
applicationsSection.classList.toggle('hidden', selectedSection !== 'applications');
identitySection.classList.toggle('hidden', selectedSection !== 'identity');
sectionTabs.forEach(tab => {
const selected = tab.dataset.section === nextSection;
const selected = tab.dataset.section === selectedSection;
tab.classList.toggle('active', selected);
tab.setAttribute('aria-selected', String(selected));
});
if (updateUrl) {
const url = new URL(window.location.href);
if (isApplications) url.searchParams.set('section', 'applications');
if (selectedSection !== 'reports') url.searchParams.set('section', selectedSection);
else url.searchParams.delete('section');
window.history.pushState({}, '', url);
}
}
sectionTabs.forEach(tab => tab.addEventListener('click', () => setSection(tab.dataset.section)));
window.addEventListener('popstate', () => setSection(new URLSearchParams(window.location.search).get('section') === 'applications' ? 'applications' : 'reports', false));
window.addEventListener('popstate', () => setSection(new URLSearchParams(window.location.search).get('section'), false));
async function lookupRenameIdentity() {
renameIdentityError.textContent = '';
renameIdentitySuccess.classList.add('hidden');
renameIdentityLookupStatus.textContent = '';
const characterId = renameIdentityId.value.trim();
if (!characterId) { renameIdentityError.textContent = 'Укажите ID кота.'; return false; }
lookupIdentity.disabled = true;
lookupIdentity.textContent = 'Проверяем…';
try {
const response = await fetch('/api/resolve-identity', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({character_id: characterId})});
const data = await response.json();
if (!response.ok || data.status !== 'found') throw new Error(data.error || data.message || 'Связка с таким ID не найдена.');
renameIdentityCurrentName.value = data.name;
renameIdentityId.value = data.character_id;
renameIdentityLookupStatus.textContent = `Текущее имя: «${data.name}».`;
return true;
} catch (err) { renameIdentityCurrentName.value = ''; renameIdentityError.textContent = err.message; return false; }
finally { lookupIdentity.disabled = false; lookupIdentity.textContent = 'Проверить ID'; }
}
lookupIdentity.addEventListener('click', lookupRenameIdentity);
renameIdentityForm.addEventListener('submit', async event => {
event.preventDefault();
renameIdentityError.textContent = '';
renameIdentitySuccess.classList.add('hidden');
if (!renameIdentityCurrentName.value && !(await lookupRenameIdentity())) return;
renameIdentitySubmit.disabled = true;
renameIdentitySubmit.textContent = 'Сохраняем…';
try {
const response = await fetch('/api/rename-identity', {method: 'POST', headers: {'Content-Type': 'application/json'}, body: JSON.stringify({character_id: renameIdentityId.value, new_name: renameIdentityNewName.value.trim()})});
const data = await response.json();
if (!response.ok) throw new Error(data.error || 'Не удалось изменить имя.');
renameIdentityCurrentName.value = data.name;
renameIdentityNewName.value = '';
renameIdentityLookupStatus.textContent = `Текущее имя: «${data.name}».`;
renameIdentitySuccess.textContent = `Имя обновлено: «${data.old_name}» → «${data.name}».`;
renameIdentitySuccess.classList.remove('hidden');
} catch (err) { renameIdentityError.textContent = err.message; }
finally { renameIdentitySubmit.disabled = false; renameIdentitySubmit.textContent = 'Сохранить новое имя'; }
});
// Adds and initializes one feather row in the application form.
function addApplicationFeather(values = {}) {
@@ -636,7 +708,7 @@
addApplicationFeather();
syncApplicationForm();
selectMode(mode);
setSection(new URLSearchParams(window.location.search).get('section') === 'applications' ? 'applications' : 'reports', false);
setSection(new URLSearchParams(window.location.search).get('section'), false);
</script>
</body>
</html>