async function handleConnection()

in source/ui/src/views/ConnectionForm.tsx [183:259]


  async function handleConnection(event: any): Promise<void> {
    event.preventDefault();

    try {
      const buildParams: ConnectionDefinition = {
        control: connectionName ? ConnectionControl.UPDATE : ConnectionControl.DEPLOY,
        connectionName: connection.connectionName,
        area: connection.area,
        machineName: connection.machineName,
        process: connection.process,
        protocol: connection.protocol,
        sendDataToIoTSitewise: connection.sendDataToIoTSitewise,
        sendDataToIoTTopic: connection.sendDataToIoTTopic,
        sendDataToKinesisDataStreams: connection.sendDataToKinesisDataStreams,
        siteName: connection.siteName
      };

      if (buildParams.protocol === MachineProtocol.OPCDA) {
        buildParams.opcDa = connection.opcDa;
        buildParams.opcDa!.listTags = buildTags(connection.listTags);
        buildParams.opcDa!.tags = buildTags(connection.tags);
      } else if (buildParams.protocol === MachineProtocol.OPCUA) {
        buildParams.opcUa = connection.opcUa;
      }

      const newErrors = validateConnectionDefinition(buildParams);

      /**
       * For the OPC UA, the server name should be unique, so if the server name is valid,
       * it checks if the server name is unique.
       */
      if (buildParams.protocol === MachineProtocol.OPCUA && !newErrors.opcUaServerName) {
        const serverName = buildParams.opcUa!.serverName;
        const opcUaConnection: GetConnectionResponse = await API.get(API_NAME, `/sitewise/${encodeURIComponent(serverName)}`, {});

        if (Object.keys(opcUaConnection).length > 0) {
          if (buildParams.control === ConnectionControl.DEPLOY) {
            newErrors.opcUa_serverName = I18n.get('invalid.duplicated.server.name');
          } else {
            /**
             * If it's updating the connection, the server name can be updated as well,
             * so if the server name on the form is same to the connection's server name, it's not an error.
             */
            const existingConnection: GetConnectionResponse = await API.get(API_NAME, `/connections/${encodeURIComponent(buildParams.connectionName)}`, {});
            const existingOpcUa = existingConnection.opcUa;

            if (existingOpcUa!.serverName !== serverName) {
              newErrors.opcUa_serverName = I18n.get('invalid.duplicated.server.name');
            }
          }
        }
      }

      if (Object.keys(newErrors).length > 0) {
        setErrors(newErrors);
      } else {
        setErrors({});
        setParams(buildParams);
        setShowConfirmMessageModal(true);
      }
    } catch (error) {
      const errorMessage = getErrorMessage(error);
      logger.error(errorMessage);

      setShowConfirmMessageModal(false);
      setShowMessageMessageModal(true);
      setMessageModalMessage(
        <Alert variant="danger">
          {connectionName ? I18n.get('error.message.update.connection') : I18n.get('error.message.create.connection')}
          <br />
          {I18n.get('error')}: <code>{JSON.stringify(errorMessage)}</code>
          <br /><br />
          {I18n.get('error.message.implementation.guide')}
        </Alert>
      );
    }
  }