Skip to content

Commit

Permalink
[web] Unify addConnection and updateConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Jan 30, 2024
1 parent 1022b04 commit 878ca8f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
54 changes: 27 additions & 27 deletions web/src/client/network/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,7 @@ const NetworkEventTypes = Object.freeze({
* @property {(id: string) => Promise<Connection>} getConnection
* @property {(ssid: string, options: object) => boolean} addAndConnectTo
* @property {(connection: Connection) => boolean} connectTo
* @property {(connection: Connection) => Promise<any>} addConnection
* @property {(connection: Connection) => Promise<any>} updateConnection
* @property {(connection: Connection) => Promise<any>} saveConnection
* @property {(connection: Connection) => void} deleteConnection
* @property {() => NetworkSettings} settings
* @property {() => void} setUp
Expand Down Expand Up @@ -199,7 +198,7 @@ class NetworkClient {
});

// the connection is automatically activated when written
return this.addConnection(connection);
return this.saveConnection(connection);
}

/**
Expand All @@ -211,28 +210,33 @@ class NetworkClient {
* @param {Connection} connection - Connection to add
* @return {Promise<Connection>} 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<Connection|undefined>}
*/
async getConnection(uuid) {
const path = await this.getConnectionPath(uuid);
const path = await this.getConnectionPathByUuid(uuid);
if (path) {
return this.connectionFromPath(path);
}
Expand Down Expand Up @@ -309,7 +313,7 @@ class NetworkClient {
* @param {string} uuid - Connection UUID
* @return {Promise<string|undefined>} - 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) {
Expand All @@ -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<boolean>} - 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<string|undefined>} - 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;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion web/src/components/network/IpSettingsForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
Expand Down

0 comments on commit 878ca8f

Please sign in to comment.