Config UI: auth/service improvements (#25573)

This commit is contained in:
Michael Geers 2025-11-26 13:50:57 +01:00 • committed by GitHub
parent ff6e4b838c
commit f35bebd895
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 50 additions and 16 deletions

View file

@ -55,7 +55,28 @@
:service-values="serviceValues[param.Name]" :service-values="serviceValues[param.Name]"
/> />
<p v-if="authError" class="text-danger">{{ authError }}</p> <p v-if="authError" class="text-danger">{{ authError }}</p>
<div class="d-flex justify-content-end">
<div class="my-4 d-flex justify-content-between align-items-baseline">
<!-- delete / cancel -->
<button
v-if="isDeletable"
type="button"
class="btn btn-link text-danger"
tabindex="0"
@click.prevent="handleRemove"
>
{{ $t("config.general.delete") }}
</button>
<button
v-else
type="button"
class="btn btn-link text-muted"
data-bs-dismiss="modal"
tabindex="0"
>
{{ $t("config.general.cancel") }}
</button>
<!-- perform auth -->
<div <div
v-if="authProviderUrl" v-if="authProviderUrl"
class="d-flex flex-column align-items-end gap-2" class="d-flex flex-column align-items-end gap-2"
@ -177,6 +198,7 @@ import {
createDeviceUtils, createDeviceUtils,
fetchServiceValues, fetchServiceValues,
} from "./index"; } from "./index";
import deepEqual from "@/utils/deepEqual";
const CUSTOM_FIELDS = ["modbus"]; const CUSTOM_FIELDS = ["modbus"];
@ -470,6 +492,17 @@ export default defineComponent({
// update on auth state change // update on auth state change
this.updateServiceValues(); this.updateServiceValues();
}, },
serviceValues: {
handler(newValue, oldValue) {
// Apply defaults only for specific params whose service values changed
Object.keys(newValue).forEach((paramName) => {
if (!deepEqual(newValue[paramName], oldValue[paramName])) {
this.applyServiceDefault(paramName);
}
});
},
deep: true,
},
}, },
methods: { methods: {
reset() { reset() {
@ -700,19 +733,16 @@ export default defineComponent({
} }
this.serviceValuesTimer = setTimeout(async () => { this.serviceValuesTimer = setTimeout(async () => {
this.serviceValues = await fetchServiceValues(this.templateParams, this.values); this.serviceValues = await fetchServiceValues(this.templateParams, this.values);
this.applyServiceDefaults();
}, 500); }, 500);
}, },
applyServiceDefaults() { applyServiceDefault(paramName: string) {
// Auto-apply single service values when field is empty and required // Auto-apply single service value when field is empty and required
Object.keys(this.serviceValues).forEach((paramName) => { const values = this.serviceValues[paramName];
const values = this.serviceValues[paramName]; const param = this.templateParams.find((p) => p.Name === paramName);
const param = this.templateParams.find((p) => p.Name === paramName); // Only auto-apply if exactly one value is returned, field is empty, and field is required
// Only auto-apply if exactly one value is returned, field is empty, and field is required if (values?.length === 1 && !this.values[paramName] && param?.Required) {
if (values?.length === 1 && !this.values[paramName] && param?.Required) { this.values[paramName] = values[0];
this.values[paramName] = values[0]; }
}
});
}, },
}, },
}); });

View file

@ -116,10 +116,11 @@ export function customChargerName(type: ConfigType, isHeating: boolean) {
export async function loadServiceValues(path: string) { export async function loadServiceValues(path: string) {
try { try {
const response = await api.get(`/config/service/${path}`); const response = await api.get(`/config/service/${path}`, {
validateStatus: (status) => status >= 200 && status < 500,
});
return (response.data as string[]) || []; return (response.data as string[]) || [];
} catch (e) { } catch {
console.error(e);
return []; return [];
} }
} }
@ -169,7 +170,10 @@ export const fetchServiceValues = async (
return; return;
} }
const url = endpoint.url(params); const url = endpoint.url(params);
result[endpoint.name] = await loadServiceValues(url); const data = await loadServiceValues(url);
if (data) {
result[endpoint.name] = data;
}
}) })
); );