等待后端更新接口
This commit is contained in:
+67
-22
@@ -29,6 +29,14 @@
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
function renderApiState(type, message) {
|
||||
if (root.ProfileUI && root.ProfileUI.renderApiState) {
|
||||
return root.ProfileUI.renderApiState(type, message);
|
||||
}
|
||||
return '<div class="api-state api-state--' + type + '" data-api-state="' + type + '">' +
|
||||
escapeHtml(message) + '</div>';
|
||||
}
|
||||
|
||||
function normalizeId(value) {
|
||||
var text;
|
||||
|
||||
@@ -129,9 +137,9 @@
|
||||
function renderJoinGenealogyOptions(data) {
|
||||
var items;
|
||||
|
||||
if (!Array.isArray(data)) return '<div class="api-empty">家谱列表数据无效,请刷新重试</div>';
|
||||
if (!Array.isArray(data)) return renderApiState('error', '家谱列表数据无效,请刷新重试');
|
||||
items = data.map(normalizeJoinGenealogy).filter(Boolean);
|
||||
if (!items.length) return '<div class="api-empty">没有找到可申请加入的家谱</div>';
|
||||
if (!items.length) return renderApiState('empty', '没有找到可申请加入的家谱');
|
||||
return items.map(function (item) {
|
||||
var meta = [
|
||||
item.surname + '氏',
|
||||
@@ -150,7 +158,7 @@
|
||||
function renderMyJoinApplies(data) {
|
||||
var items = normalizeJoinApplies(data);
|
||||
|
||||
if (!items.length) return '<div class="api-empty">当前没有加入申请</div>';
|
||||
if (!items.length) return renderApiState('empty', '当前没有加入申请');
|
||||
return items.map(function (item) {
|
||||
var details = [
|
||||
item.surname ? item.surname + '氏' : '',
|
||||
@@ -184,10 +192,40 @@
|
||||
else root.location.href = 'login.html';
|
||||
}
|
||||
|
||||
function setText(selector, message) {
|
||||
function getApiStateType(error) {
|
||||
return Number(error && (error.status || error.code)) === 403 ? 'forbidden' : 'error';
|
||||
}
|
||||
|
||||
function setText(selector, message, type) {
|
||||
var element = root.document && root.document.querySelector(selector);
|
||||
|
||||
if (element) element.textContent = message || '';
|
||||
if (!element) return;
|
||||
if (!message) {
|
||||
element.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
if (root.ProfileUI && root.ProfileUI.setApiState) {
|
||||
root.ProfileUI.setApiState(element, type || 'error', message);
|
||||
return;
|
||||
}
|
||||
element.textContent = message;
|
||||
}
|
||||
|
||||
function setOptionsState(options, type, message) {
|
||||
if (!options) return;
|
||||
if (root.ProfileUI && root.ProfileUI.setApiState) {
|
||||
root.ProfileUI.setApiState(options, type, message);
|
||||
return;
|
||||
}
|
||||
options.innerHTML = renderApiState(type, message);
|
||||
}
|
||||
|
||||
function setSubmitPending(button, pending, pendingLabel) {
|
||||
if (!button) return;
|
||||
if (!button.dataset.defaultLabel) button.dataset.defaultLabel = button.textContent;
|
||||
button.disabled = Boolean(pending);
|
||||
button.textContent = pending ? pendingLabel : button.dataset.defaultLabel;
|
||||
button.setAttribute('aria-busy', pending ? 'true' : 'false');
|
||||
}
|
||||
|
||||
function getFormValues(form) {
|
||||
@@ -222,27 +260,34 @@
|
||||
return;
|
||||
}
|
||||
|
||||
function clearSelectedGenealogy() {
|
||||
selectedGenealogyId = '';
|
||||
if (selected) selected.textContent = '尚未选择家谱';
|
||||
}
|
||||
|
||||
async function loadOptions(keyword) {
|
||||
var data;
|
||||
|
||||
setText('[data-join-apply-status]', '正在读取可申请家谱…');
|
||||
clearSelectedGenealogy();
|
||||
setText('[data-join-options-status]', '正在读取可申请家谱…', 'loading');
|
||||
setOptionsState(options, 'loading', '正在读取可申请家谱…');
|
||||
try {
|
||||
data = await api.genealogyOptions(keyword ? { keyword: keyword } : {});
|
||||
options.innerHTML = renderJoinGenealogyOptions(data);
|
||||
setText('[data-join-apply-status]', '');
|
||||
setText('[data-join-options-status]', '');
|
||||
} catch (error) {
|
||||
if (shouldRedirectToLogin(api, error)) {
|
||||
redirectToLogin(api);
|
||||
return;
|
||||
}
|
||||
options.innerHTML = '<div class="api-empty">读取家谱列表失败</div>';
|
||||
setText('[data-join-apply-status]', error.message || '读取家谱列表失败,请稍后重试');
|
||||
setOptionsState(options, getApiStateType(error), '读取家谱列表失败');
|
||||
setText('[data-join-options-status]', error.message || '读取家谱列表失败,请稍后重试', getApiStateType(error));
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (!canJoinGenealogy(await api.genealogyQuota())) {
|
||||
setText('[data-join-apply-status]', '当前没有可用的家谱加入额度');
|
||||
setText('[data-join-apply-status]', '当前没有可用的家谱加入额度', 'empty');
|
||||
applyForm.querySelector('[data-join-apply-submit]').disabled = true;
|
||||
return;
|
||||
}
|
||||
@@ -251,7 +296,7 @@
|
||||
redirectToLogin(api);
|
||||
return;
|
||||
}
|
||||
setText('[data-join-apply-status]', error.message || '无法读取家谱加入额度');
|
||||
setText('[data-join-apply-status]', error.message || '无法读取家谱加入额度', getApiStateType(error));
|
||||
applyForm.querySelector('[data-join-apply-submit]').disabled = true;
|
||||
return;
|
||||
}
|
||||
@@ -280,18 +325,18 @@
|
||||
event.preventDefault();
|
||||
if (writePending) return;
|
||||
if (!selectedGenealogyId) {
|
||||
setText('[data-join-apply-status]', '请先从家谱列表中选择要加入的家谱');
|
||||
setText('[data-join-apply-status]', '请先从家谱列表中选择要加入的家谱', 'error');
|
||||
return;
|
||||
}
|
||||
body = buildJoinApplyBody(getFormValues(applyForm));
|
||||
validation = validateJoinApplyBody(body);
|
||||
if (validation) {
|
||||
setText('[data-join-apply-status]', validation);
|
||||
setText('[data-join-apply-status]', validation, 'error');
|
||||
return;
|
||||
}
|
||||
writePending = true;
|
||||
applyForm.querySelector('[data-join-apply-submit]').disabled = true;
|
||||
setText('[data-join-apply-status]', '正在提交申请…');
|
||||
setSubmitPending(applyForm.querySelector('[data-join-apply-submit]'), true, '正在提交…');
|
||||
setText('[data-join-apply-status]', '正在提交申请…', 'loading');
|
||||
try {
|
||||
created = normalizeJoinApply(await api.applyToGenealogy(selectedGenealogyId, body));
|
||||
if (!created) throw new Error('申请响应缺少有效申请信息');
|
||||
@@ -305,10 +350,10 @@
|
||||
redirectToLogin(api);
|
||||
return;
|
||||
}
|
||||
setText('[data-join-apply-status]', error.message || '提交加入申请失败,请稍后重试');
|
||||
setText('[data-join-apply-status]', error.message || '提交加入申请失败,请稍后重试', getApiStateType(error));
|
||||
} finally {
|
||||
writePending = false;
|
||||
applyForm.querySelector('[data-join-apply-submit]').disabled = false;
|
||||
setSubmitPending(applyForm.querySelector('[data-join-apply-submit]'), false, '正在提交…');
|
||||
}
|
||||
});
|
||||
await loadOptions('');
|
||||
@@ -330,7 +375,7 @@
|
||||
async function loadApplies() {
|
||||
var data;
|
||||
|
||||
setText('[data-my-join-applies-status]', '正在读取申请记录…');
|
||||
setText('[data-my-join-applies-status]', '正在读取申请记录…', 'loading');
|
||||
try {
|
||||
data = await api.myGenealogyJoinApplies();
|
||||
list.innerHTML = renderMyJoinApplies(data);
|
||||
@@ -341,8 +386,8 @@
|
||||
redirectToLogin(api);
|
||||
return [];
|
||||
}
|
||||
list.innerHTML = '<div class="api-empty">读取加入申请失败</div>';
|
||||
setText('[data-my-join-applies-status]', error.message || '读取加入申请失败,请稍后重试');
|
||||
list.innerHTML = renderApiState(getApiStateType(error), '读取加入申请失败');
|
||||
setText('[data-my-join-applies-status]', error.message || '读取加入申请失败,请稍后重试', getApiStateType(error));
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -361,7 +406,7 @@
|
||||
if (root.confirm && !root.confirm('确认撤销这条加入申请吗?')) return;
|
||||
writePending = true;
|
||||
button.disabled = true;
|
||||
setText('[data-my-join-applies-status]', '正在撤销申请…');
|
||||
setText('[data-my-join-applies-status]', '正在撤销申请…', 'loading');
|
||||
try {
|
||||
await api.cancelGenealogyJoinApply(applyId);
|
||||
refreshed = await loadApplies();
|
||||
@@ -373,7 +418,7 @@
|
||||
redirectToLogin(api);
|
||||
return;
|
||||
}
|
||||
setText('[data-my-join-applies-status]', error.message || '撤销加入申请失败,请稍后重试');
|
||||
setText('[data-my-join-applies-status]', error.message || '撤销加入申请失败,请稍后重试', getApiStateType(error));
|
||||
} finally {
|
||||
writePending = false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user