Skip to content

Commit

Permalink
UI: better handle api calls when offline (evcc-io#13596)
Browse files Browse the repository at this point in the history
  • Loading branch information
naltatis authored Apr 26, 2024
1 parent b2fce25 commit c0bf1ec
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
1 change: 1 addition & 0 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const app = createApp({
},
methods: {
raise: function (msg) {
if (this.offline) return;
if (!msg.level) msg.level = "error";
const now = new Date();
const latestMsg = this.notifications[0];
Expand Down
14 changes: 11 additions & 3 deletions assets/js/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import Modal from "bootstrap/js/dist/modal";

const auth = reactive({
configured: true,
loggedIn: false,
loggedIn: null, // true / false / null (unknown)
nextUrl: null,
});

export async function updateAuthStatus() {
try {
const res = await api.get("/auth/status", {
validateStatus: (code) => [200, 501].includes(code),
validateStatus: (code) => [200, 501, 500].includes(code),
});
if (res.status === 501) {
auth.configured = false;
Expand All @@ -20,6 +20,10 @@ export async function updateAuthStatus() {
auth.configured = true;
auth.loggedIn = res.data === true;
}
if (res.status === 500) {
auth.loggedIn = null;
console.log("unable to fetch auth status", res);
}
} catch (e) {
console.log("unable to fetch auth status", e);
}
Expand All @@ -36,7 +40,11 @@ export async function logout() {
}

export function isLoggedIn() {
return auth.loggedIn;
return auth.loggedIn === true;
}

export function statusUnknown() {
return auth.loggedIn === null;
}

export function isConfigured() {
Expand Down
9 changes: 6 additions & 3 deletions assets/js/components/TelemetrySettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,12 @@ export default {
return;
}
try {
const response = await api.get("settings/telemetry");
console.log("update in settings", response.data.result);
settings.telemetry = response.data.result;
const response = await api.get("settings/telemetry", {
validateStatus: () => true,
});
if (response.status === 200) {
settings.telemetry = response.data.result;
}
} catch (err) {
console.error(err);
}
Expand Down
4 changes: 2 additions & 2 deletions assets/js/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createRouter, createWebHashHistory } from "vue-router";
import Modal from "bootstrap/js/dist/modal";
import Main from "./views/Main.vue";
import { ensureCurrentLocaleMessages } from "./i18n";
import { openLoginModal, updateAuthStatus, isLoggedIn } from "./auth";
import { openLoginModal, statusUnknown, updateAuthStatus, isLoggedIn } from "./auth";

function hideAllModals() {
[...document.querySelectorAll(".modal.show")].forEach((modal) => {
Expand All @@ -18,7 +18,7 @@ function hideAllModals() {

async function ensureAuth(to) {
await updateAuthStatus();
if (!isLoggedIn()) {
if (!isLoggedIn() && !statusUnknown()) {
openLoginModal(to.path);
return false;
}
Expand Down
16 changes: 9 additions & 7 deletions assets/js/views/Config.vue
Original file line number Diff line number Diff line change
Expand Up @@ -466,14 +466,16 @@ export default {
},
async updateValues() {
clearTimeout(this.deviceValueTimeout);
if (!this.offline) {
const promises = [
...this.meters.map((meter) => this.updateDeviceValue("meter", meter.name)),
...this.vehicles.map((vehicle) =>
this.updateDeviceValue("vehicle", vehicle.name)
),
];
const promises = [
...this.meters.map((meter) => this.updateDeviceValue("meter", meter.name)),
...this.vehicles.map((vehicle) => this.updateDeviceValue("vehicle", vehicle.name)),
];
await Promise.all(promises);
await Promise.all(promises);
}
this.deviceValueTimeout = setTimeout(this.updateValues, 10000);
},
deviceTags(type, id) {
Expand Down

0 comments on commit c0bf1ec

Please sign in to comment.