From 878ca8ff8a4d76217bc375780a6942838e10bfa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Imobach=20Gonz=C3=A1lez=20Sosa?= Date: Tue, 30 Jan 2024 07:08:05 +0000 Subject: [PATCH] [web] Unify addConnection and updateConnection --- web/src/client/network/index.js | 54 +++++++++---------- web/src/components/network/IpSettingsForm.jsx | 2 +- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/web/src/client/network/index.js b/web/src/client/network/index.js index 0e02c0bced..0b7c017db9 100644 --- a/web/src/client/network/index.js +++ b/web/src/client/network/index.js @@ -69,8 +69,7 @@ const NetworkEventTypes = Object.freeze({ * @property {(id: string) => Promise} getConnection * @property {(ssid: string, options: object) => boolean} addAndConnectTo * @property {(connection: Connection) => boolean} connectTo - * @property {(connection: Connection) => Promise} addConnection - * @property {(connection: Connection) => Promise} updateConnection + * @property {(connection: Connection) => Promise} saveConnection * @property {(connection: Connection) => void} deleteConnection * @property {() => NetworkSettings} settings * @property {() => void} setUp @@ -199,7 +198,7 @@ class NetworkClient { }); // the connection is automatically activated when written - return this.addConnection(connection); + return this.saveConnection(connection); } /** @@ -211,28 +210,33 @@ class NetworkClient { * @param {Connection} connection - Connection to add * @return {Promise} the added connection */ - async addConnection(connection) { - const { id } = connection; - const proxy = await this.client.proxy(CONNECTIONS_IFACE, CONNECTIONS_PATH); - const deviceType = (connection.wireless) ? DeviceType.WIRELESS : DeviceType.ETHERNET; + async saveConnection(connection) { + const { id, uuid } = connection; let path; - try { - path = await proxy.GetConnectionById(id); - } catch { + if (uuid) { + path = await this.getConnectionPathByUuid(uuid); + } else { + path = await this.getConnectionPathById(id); + } + + if (!path) { + const proxy = await this.client.proxy(CONNECTIONS_IFACE, CONNECTIONS_PATH); + const deviceType = (connection.wireless) ? DeviceType.WIRELESS : DeviceType.ETHERNET; path = await proxy.AddConnection(id, deviceType); } + await this.updateConnectionAt(path, connection); return this.connectionFromPath(path); } /** - * Returns the connection with the given ID + * Returns the connection with the given UUID * - * @param {string} uuid - Connection ID + * @param {string} uuid - Connection UUID * @return {Promise} */ async getConnection(uuid) { - const path = await this.getConnectionPath(uuid); + const path = await this.getConnectionPathByUuid(uuid); if (path) { return this.connectionFromPath(path); } @@ -309,7 +313,7 @@ class NetworkClient { * @param {string} uuid - Connection UUID * @return {Promise} - Connection D-Bus path */ - async getConnectionPath(uuid) { + async getConnectionPathByUuid(uuid) { for (const path in this.proxies.connections) { const proxy = await this.proxies.connections[path]; if (proxy.Uuid === uuid) { @@ -319,22 +323,18 @@ class NetworkClient { } /** - * Updates the connection - * - * It uses the 'uuid' to match the connection in the backend. + * Returns the D-Bus path of the connection. * - * @param {Connection} connection - Connection to update - * @return {Promise} - the promise resolves to true if the connection - * was successfully updated and to false it it does not exist. + * @param {string} id - Connection ID + * @return {Promise} - Connection D-Bus path */ - async updateConnection(connection) { - const path = await this.getConnectionPath(connection.uuid); - if (path === undefined) { - return false; + async getConnectionPathById(id) { + const proxy = await this.client.proxy(CONNECTIONS_IFACE, CONNECTIONS_PATH); + try { + return await proxy.GetConnectionById(id); + } catch { + return; } - - await this.updateConnectionAt(path, connection); - return true; } /** diff --git a/web/src/components/network/IpSettingsForm.jsx b/web/src/components/network/IpSettingsForm.jsx index 8244590143..2a5ea70ab6 100644 --- a/web/src/components/network/IpSettingsForm.jsx +++ b/web/src/components/network/IpSettingsForm.jsx @@ -111,7 +111,7 @@ export default function IpSettingsForm({ connection, onClose }) { } }; - client.network.updateConnection(updatedConnection) + client.network.saveConnection(updatedConnection) .then(onClose) // TODO: better error reporting. By now, it sets an error for the whole connection. .catch(({ message }) => setErrors({ object: message }));