181 lines
5.8 KiB
JavaScript
181 lines
5.8 KiB
JavaScript
(function () {
|
|
var selectedAgency = null;
|
|
|
|
function $(id) {
|
|
return document.getElementById(id);
|
|
}
|
|
|
|
function t(key, fallback) {
|
|
return window.HyAppI18n && window.HyAppI18n.t
|
|
? window.HyAppI18n.t(key, fallback)
|
|
: fallback || key;
|
|
}
|
|
|
|
function setStatus(key, kind) {
|
|
var node = $('joinAgencyStatus');
|
|
if (!node) return;
|
|
if (!key) {
|
|
node.hidden = true;
|
|
node.textContent = '';
|
|
node.className = 'join-agency-status';
|
|
return;
|
|
}
|
|
node.hidden = false;
|
|
node.className = 'join-agency-status' + (kind ? ' is-' + kind : '');
|
|
node.textContent = t(key);
|
|
}
|
|
|
|
function setSearching(searching) {
|
|
var input = $('agencySearchInput');
|
|
var button = $('agencySearchButton');
|
|
if (input) input.disabled = searching;
|
|
if (button) {
|
|
button.disabled = searching;
|
|
button.textContent = searching
|
|
? t('host_center.searching', 'Searching...')
|
|
: t('host_center.search', 'Search');
|
|
}
|
|
}
|
|
|
|
function setApplying(applying) {
|
|
var button = $('joinAgencyApplyButton');
|
|
if (!button) return;
|
|
button.disabled = applying || !selectedAgency;
|
|
button.textContent = applying
|
|
? t('host_center.applying', 'Submitting...')
|
|
: t('host_center.apply', 'Apply');
|
|
}
|
|
|
|
function agencyID(agency) {
|
|
return String((agency && (agency.agency_id || agency.agencyId)) || '');
|
|
}
|
|
|
|
function agencyName(agency) {
|
|
return String((agency && agency.name) || '');
|
|
}
|
|
|
|
function renderAgency(agency) {
|
|
selectedAgency = agency || null;
|
|
var result = $('joinAgencyResult');
|
|
if (!result) return;
|
|
if (!selectedAgency) {
|
|
result.hidden = true;
|
|
setApplying(false);
|
|
return;
|
|
}
|
|
var name =
|
|
agencyName(selectedAgency) || t('host_center.agency', 'Agency');
|
|
result.hidden = false;
|
|
$('agencyName').textContent = name;
|
|
$('agencyAccount').textContent = 'ID: ' + agencyID(selectedAgency);
|
|
$('agencyInitial').textContent =
|
|
name.trim().charAt(0).toUpperCase() || 'A';
|
|
setApplying(false);
|
|
}
|
|
|
|
function searchAgency(event) {
|
|
if (event) event.preventDefault();
|
|
if (!window.HyAppAPI || !window.HyAppAPI.host) return;
|
|
var input = $('agencySearchInput');
|
|
var shortID = input ? input.value.trim() : '';
|
|
selectedAgency = null;
|
|
renderAgency(null);
|
|
if (!shortID) {
|
|
setStatus('host_center.search_required', 'error');
|
|
return;
|
|
}
|
|
setStatus('host_center.searching');
|
|
setSearching(true);
|
|
window.HyAppAPI.host
|
|
.searchAgencies(shortID, 20)
|
|
.then(function (data) {
|
|
var items = (data && (data.items || data.agencies)) || [];
|
|
if (!items.length) {
|
|
setStatus('host_center.no_agency', 'error');
|
|
return;
|
|
}
|
|
setStatus('');
|
|
renderAgency(items[0]);
|
|
})
|
|
.catch(function () {
|
|
setStatus('host_center.search_failed', 'error');
|
|
})
|
|
.finally(function () {
|
|
setSearching(false);
|
|
});
|
|
}
|
|
|
|
function applyAgency() {
|
|
if (!selectedAgency || !window.HyAppAPI || !window.HyAppAPI.host)
|
|
return;
|
|
setStatus('host_center.applying');
|
|
setApplying(true);
|
|
window.HyAppAPI.host
|
|
.applyToAgency({
|
|
agency_id: agencyID(selectedAgency),
|
|
command_id:
|
|
'host_apply_' +
|
|
Date.now() +
|
|
'_' +
|
|
Math.random().toString(16).slice(2),
|
|
})
|
|
.then(function () {
|
|
setStatus('host_center.apply_success', 'success');
|
|
})
|
|
.catch(function () {
|
|
setStatus('host_center.apply_failed', 'error');
|
|
})
|
|
.finally(function () {
|
|
setApplying(false);
|
|
});
|
|
}
|
|
|
|
function renderUser(current) {
|
|
var user = current && current.user;
|
|
var profile = user && (user.profile || user.user || user);
|
|
if (!profile) return;
|
|
var name = profile.username || profile.name || profile.nickname || '';
|
|
var uid =
|
|
profile.display_user_id ||
|
|
profile.displayUserId ||
|
|
profile.user_id ||
|
|
profile.userId ||
|
|
'';
|
|
if (name && $('profileName')) {
|
|
$('profileName').removeAttribute('data-i18n');
|
|
$('profileName').textContent = name;
|
|
}
|
|
if (uid && $('profileUID')) {
|
|
$('profileUID').removeAttribute('data-i18n');
|
|
$('profileUID').textContent = 'UID: ' + uid;
|
|
}
|
|
}
|
|
|
|
function init() {
|
|
if (window.HyAppParams) {
|
|
window.HyAppParams.parse();
|
|
window.HyAppParams.loadUser()
|
|
.then(renderUser)
|
|
.catch(function () {});
|
|
}
|
|
var form = $('joinAgencyForm');
|
|
if (form) form.addEventListener('submit', searchAgency);
|
|
var applyButton = $('joinAgencyApplyButton');
|
|
if (applyButton) applyButton.addEventListener('click', applyAgency);
|
|
var backButton = $('backButton');
|
|
if (backButton) {
|
|
backButton.addEventListener('click', function () {
|
|
if (window.HyAppBridge) window.HyAppBridge.back();
|
|
});
|
|
}
|
|
setApplying(false);
|
|
}
|
|
|
|
window.addEventListener('hyapp:i18n-ready', function () {
|
|
setSearching(false);
|
|
setApplying(false);
|
|
});
|
|
|
|
document.addEventListener('DOMContentLoaded', init);
|
|
})();
|