feat: add loading when get settings (#19)

This commit is contained in:
Dream Hunter
2023-09-07 21:16:26 +08:00
committed by GitHub
parent 6fd96988ce
commit 9d18f7b59b
2 changed files with 52 additions and 39 deletions
+1 -1
View File
@@ -4,7 +4,7 @@
"private": true, "private": true,
"scripts": { "scripts": {
"dev": "vite", "dev": "vite",
"build": "vite build", "build": "vite build -m prod --emptyOutDir",
"preview": "vite preview", "preview": "vite preview",
"deploy": "wrangler pages deploy ../dist --branch production" "deploy": "wrangler pages deploy ../dist --branch production"
}, },
+16 -3
View File
@@ -86,8 +86,8 @@ const refresh = async () => {
if (typeof address.value != 'string' || address.value.trim() === '') { if (typeof address.value != 'string' || address.value.trim() === '') {
return; return;
} }
try {
loading.value = true; loading.value = true;
try {
const response = await fetch(`${API_BASE}/api/mails`, { const response = await fetch(`${API_BASE}/api/mails`, {
method: "GET", method: "GET",
headers: { headers: {
@@ -148,7 +148,9 @@ const newEmail = async () => {
} }
}; };
const getOpenSettings = async (jwt) => { const getOpenSettings = async () => {
loading.value = true;
try {
const response = await fetch(`${API_BASE}/open_api/settings`, { const response = await fetch(`${API_BASE}/open_api/settings`, {
method: "GET", method: "GET",
headers: { headers: {
@@ -158,7 +160,6 @@ const getOpenSettings = async (jwt) => {
if (!response.ok) { if (!response.ok) {
message.error(`${response.status} ${await response.text()}` || "error"); message.error(`${response.status} ${await response.text()}` || "error");
console.error(response);
return; return;
} }
let res = await response.json(); let res = await response.json();
@@ -172,12 +173,21 @@ const getOpenSettings = async (jwt) => {
}) })
}; };
emailDomain.value = openSettings.value.domains[0].value; emailDomain.value = openSettings.value.domains[0].value;
} catch (error) {
message.error(error.message || "error");
console.error(error);
}
finally {
loading.value = false;
}
} }
const getSettings = async (jwt) => { const getSettings = async (jwt) => {
if (typeof jwt != 'string' || jwt.trim() === '' || jwt === 'undefined') { if (typeof jwt != 'string' || jwt.trim() === '' || jwt === 'undefined') {
return; return;
} }
loading.value = true;
try {
const response = await fetch(`${API_BASE}/api/settings`, { const response = await fetch(`${API_BASE}/api/settings`, {
method: "GET", method: "GET",
headers: { headers: {
@@ -195,6 +205,9 @@ const getSettings = async (jwt) => {
let res = await response.json(); let res = await response.json();
address.value = res["address"]; address.value = res["address"];
await refresh(); await refresh();
} finally {
loading.value = false;
}
} }
watch(jwt, async (jwt, old) => getSettings(jwt)) watch(jwt, async (jwt, old) => getSettings(jwt))