Fix landing-page dns update request (#22932)

* Fix landing-page dns update request

* clear  _dnsPrimaryInterface
This commit is contained in:
Wendelin 2024-11-21 11:10:20 +01:00 committed by GitHub
parent 79c7cf59ee
commit 881ff13832
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 31 additions and 15 deletions

View File

@ -33,6 +33,8 @@ class LandingPageNetwork extends LitElement {
@state() private _getNetworkInfoError = false; @state() private _getNetworkInfoError = false;
@state() private _dnsPrimaryInterfaceNameservers?: string;
@state() private _dnsPrimaryInterface?: string; @state() private _dnsPrimaryInterface?: string;
protected render() { protected render() {
@ -55,11 +57,11 @@ class LandingPageNetwork extends LitElement {
> >
<p> <p>
${this.localize("network_issue.description", { ${this.localize("network_issue.description", {
dns: this._dnsPrimaryInterface || "?", dns: this._dnsPrimaryInterfaceNameservers || "?",
})} })}
</p> </p>
<p>${this.localize("network_issue.resolve_different")}</p> <p>${this.localize("network_issue.resolve_different")}</p>
${!this._dnsPrimaryInterface ${!this._dnsPrimaryInterfaceNameservers
? html` ? html`
<p> <p>
<b>${this.localize("network_issue.no_primary_interface")} </b> <b>${this.localize("network_issue.no_primary_interface")} </b>
@ -71,7 +73,7 @@ class LandingPageNetwork extends LitElement {
({ translationKey }, key) => ({ translationKey }, key) =>
html`<ha-button html`<ha-button
.index=${key} .index=${key}
.disabled=${!this._dnsPrimaryInterface} .disabled=${!this._dnsPrimaryInterfaceNameservers}
@click=${this._setDns} @click=${this._setDns}
>${this.localize(translationKey)}</ha-button >${this.localize(translationKey)}</ha-button
>` >`
@ -106,22 +108,30 @@ class LandingPageNetwork extends LitElement {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.error(err); console.error(err);
this._getNetworkInfoError = true; this._getNetworkInfoError = true;
this._dnsPrimaryInterfaceNameservers = undefined;
this._dnsPrimaryInterface = undefined;
return; return;
} }
this._getNetworkInfoError = false; this._getNetworkInfoError = false;
const primaryInterface = data.interfaces.find(
(intf) => intf.primary && intf.enabled
);
if (primaryInterface) {
this._dnsPrimaryInterfaceNameservers = [
...(primaryInterface.ipv4?.nameservers || []),
...(primaryInterface.ipv6?.nameservers || []),
].join(", ");
this._dnsPrimaryInterface = primaryInterface.interface;
} else {
this._dnsPrimaryInterfaceNameservers = undefined;
this._dnsPrimaryInterface = undefined;
}
if (!data.host_internet) { if (!data.host_internet) {
this._networkIssue = true; this._networkIssue = true;
const primaryInterface = data.interfaces.find(
(intf) => intf.primary && intf.enabled
);
if (primaryInterface) {
this._dnsPrimaryInterface = [
...(primaryInterface.ipv4?.nameservers || []),
...(primaryInterface.ipv6?.nameservers || []),
].join(", ");
}
} else { } else {
this._networkIssue = false; this._networkIssue = false;
} }
@ -135,7 +145,10 @@ class LandingPageNetwork extends LitElement {
private async _setDns(ev) { private async _setDns(ev) {
const index = ev.target?.index; const index = ev.target?.index;
try { try {
const response = await setSupervisorNetworkDns(index); const response = await setSupervisorNetworkDns(
index,
this._dnsPrimaryInterface!
);
if (!response.ok) { if (!response.ok) {
throw new Error("Failed to set DNS"); throw new Error("Failed to set DNS");
} }

View File

@ -37,8 +37,11 @@ export async function getSupervisorNetworkInfo() {
return fetch("/supervisor/network/info"); return fetch("/supervisor/network/info");
} }
export const setSupervisorNetworkDns = async (dnsServerIndex: number) => export const setSupervisorNetworkDns = async (
fetch("/supervisor/network/dns", { dnsServerIndex: number,
primaryInterface: string
) =>
fetch(`/supervisor/network/interface/${primaryInterface}/update`, {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
ipv4: { ipv4: {