feat(user): add local avatar management flow
This commit is contained in:
1 parent
1b4ba8938a
commit
e94437ef60
20 files changed
+289
-186
No files matched your search
@@ -206,6 +206,7 @@
|
||||
var button = document.getElementById(upload.buttonId || 'aksrt-upload-avatar-btn');
|
||||
var input = document.getElementById(upload.inputId || 'aksrt-avatar-file-input');
|
||||
var image = document.getElementById(upload.imageId || 'aksrt-user-avatar-image');
|
||||
var removeButton = document.getElementById(upload.removeButtonId || 'aksrt-remove-avatar-btn');
|
||||
if (!button || !input || button.dataset.aksrtAvatarBound === '1') return;
|
||||
button.dataset.aksrtAvatarBound = '1';
|
||||
|
||||
@@ -246,6 +247,7 @@
|
||||
var url = payload.url || (payload.data && payload.data.url) || '';
|
||||
if (!url) throw payload;
|
||||
if (image) image.src = url;
|
||||
if (removeButton) removeButton.hidden = false;
|
||||
notify(payload.message || upload.successText || '头像已更新', 'success');
|
||||
})
|
||||
.catch(function (error) {
|
||||
@@ -256,6 +258,40 @@
|
||||
input.value = '';
|
||||
});
|
||||
});
|
||||
|
||||
if (removeButton) {
|
||||
var removeLabel = removeButton.querySelector('span');
|
||||
var removeIdleText = upload.removeText || (removeLabel ? removeLabel.textContent : '恢复默认头像');
|
||||
removeButton.addEventListener('click', function () {
|
||||
if (!window.confirm(upload.confirmRemoveText || '确定恢复为默认头像?')) return;
|
||||
removeButton.disabled = true;
|
||||
if (removeLabel) removeLabel.textContent = upload.removingText || '恢复中…';
|
||||
|
||||
fetch(upload.restUrl, {
|
||||
method: 'DELETE',
|
||||
credentials: 'same-origin',
|
||||
headers: { 'X-WP-Nonce': upload.restNonce || '' }
|
||||
})
|
||||
.then(function (response) {
|
||||
return response.json().then(function (payload) {
|
||||
if (!response.ok || !payload || !payload.success) throw payload || {};
|
||||
return payload;
|
||||
});
|
||||
})
|
||||
.then(function (payload) {
|
||||
if (image && payload.url) image.src = payload.url;
|
||||
removeButton.hidden = true;
|
||||
notify(payload.message || upload.removeSuccessText || '已恢复默认头像', 'success');
|
||||
})
|
||||
.catch(function (error) {
|
||||
notify((error && error.message) || upload.errorText || '头像操作失败', 'error');
|
||||
})
|
||||
.finally(function () {
|
||||
removeButton.disabled = false;
|
||||
if (removeLabel) removeLabel.textContent = removeIdleText;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function init() {
|
||||
|
||||
Reference in new issue
Block a user