<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: async function messing up Login in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/async-function-messing-up-login/m-p/1553614#M85995</link>
    <description>&lt;P&gt;Normally it would be good practice not to mix async..await and Promise when writing in JS. Maybe you can change the whole function to Promise.&lt;/P&gt;</description>
    <pubDate>Wed, 30 Oct 2024 00:48:58 GMT</pubDate>
    <dc:creator>RamaniGanason</dc:creator>
    <dc:date>2024-10-30T00:48:58Z</dc:date>
    <item>
      <title>async function messing up Login</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/async-function-messing-up-login/m-p/1552865#M85967</link>
      <description>&lt;P&gt;Im basically trying to merge these two samples:&lt;/P&gt;&lt;P&gt;Login:&amp;nbsp;&lt;A href="https://developers.arcgis.com/javascript/latest/sample-code/identity-oauth-basic/live/" target="_blank"&gt;https://developers.arcgis.com/javascript/latest/sample-code/identity-oauth-basic/live/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Related Tables:&amp;nbsp;&lt;A href="https://developers.arcgis.com/javascript/latest/sample-code/widgets-featuretable-relates/" target="_blank"&gt;https://developers.arcgis.com/javascript/latest/sample-code/widgets-featuretable-relates/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;But I'm stuck with the await (line 187 &amp;amp; 193). If I follow the related table example and put async function at top, then the sign in part stops working.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm just not sure where to start and stop the async function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;&amp;lt;html lang="en"&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&amp;gt;
    &amp;lt;meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no" /&amp;gt;
    &amp;lt;title&amp;gt;Access ArcGIS Online items using OAuth 2.0 | Sample | ArcGIS Maps SDK for JavaScript 4.30&amp;lt;/title&amp;gt;

    &amp;lt;script type="module" src="https://js.arcgis.com/calcite-components/2.11.1/calcite.esm.js"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;link rel="stylesheet" type="text/css" href="https://js.arcgis.com/calcite-components/2.11.1/calcite.css" /&amp;gt;
    &amp;lt;link rel="stylesheet" href="https://js.arcgis.com/4.30/esri/themes/light/main.css" /&amp;gt;
    &amp;lt;script src="https://js.arcgis.com/4.30/"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;style&amp;gt;
      html,
      body {
        width: 100%;
        height: 100%;
        margin: 0px;
        padding: 0px;
        display: flex;
      }

     

      #viewDiv {
        height: calc(100% - 65px);
        width: 50%;
        margin-top:65px;
      }

      .container {
        height: calc(100% - 65px);
        width: 50%;
        margin-top:65px;
      }
      calcite-modal {
        --calcite-modal-scrim-background: rgb(34, 77, 122, 0.7);
      }

      #modal hr {
        margin: 1rem 0;
        border: 0;
        border-bottom: 1px solid var(--calcite-color-border-3);
      }

      #modal ul li {
        margin-bottom: 0.5rem;
      }

      #modal calcite-notice {
        margin-bottom: 1.25rem;
      }
    &amp;lt;/style&amp;gt;
    &amp;lt;script&amp;gt;
      require([

        "esri/portal/Portal",
        "esri/identity/OAuthInfo",
        "esri/identity/IdentityManager",
        "esri/portal/PortalQueryParams",
        "esri/Map",
        "esri/views/MapView",
        "esri/WebMap",
        "esri/widgets/FeatureTable",
        "esri/core/reactiveUtils",
        "esri/config"

      ], (Portal, OAuthInfo, esriId, PortalQueryParams, Map, MapView, WebMap, FeatureTable, reactiveUtils, esriConfig) =&amp;gt; {
          console.log("startup")
        // UI elements
        const signInButton = document.getElementById("sign-in-button");
        const navLogo = document.getElementById("nav-logo");
        const navigationUser = document.getElementById("nav-user");
        const galleryPanel = document.getElementById("item-panel");
        const itemGallery = document.getElementById("item-gallery");
        signInButton.addEventListener("click", signInOrOut);
        navigationUser.addEventListener("click", signInOrOut);

        esriConfig.portalUrl = "https://webgis.cushwake.com/portal";
        //Create a new OAuthInfo object.
        const info = new OAuthInfo({
          // Swap this ID out with an app ID registered in your ArcGIS Organization.
          appId: "9jjg9al9Mm3gFiLy",
          // Add the portalUrl property if using your own portal.
          portalUrl: esriConfig.portalUrl,
          // Set the authNamespace property to prevent the user's signed in state
          // from being shared with other apps on the same domain with the same authNamespace value.
          authNamespace: "portal_oauth_inline",
          // Set popup to true to show the OAuth sign-in page in a separate popup window.
          popup: false
        });

        // Add the OAuthInfo to the IdentityManager.
        esriId.registerOAuthInfos([info]);



        // Call the checkSignIn function to see if the user is already signed in.
        checkSignIn();

        // Function to check the current sign in status and query the portal if signed in.
         function checkSignIn() {
          esriId
            .checkSignInStatus(info.portalUrl + "/sharing")
            .then(() =&amp;gt; {
              // If signed in, show the username in the UI.
              navigationUser.hidden = false;
              signInButton.hidden = true;
              const portal = new Portal({
                authMode: "immediate"
              });
              // Check if using a portal other than ArcGIS Online.
              if (info.portalUrl !== "https://webgis.cushwake.com/portal") {
                portal.url = info.portalUrl;
              }
              // Load the portal, display the name and username, then call the query items function.
              portal.load().then(() =&amp;gt; {
                navigationUser.fullName = portal.user.fullName;
                navigationUser.username = portal.user.username;
                navLogo.description =
                  "Gallery of queried portal items displayed below. To sign out, click on the logged in user button.";
                //queryItems(portal);
              });
            })
            .catch(() =&amp;gt; {
              // If not signed in, then show the sign in button.
              signInButton.hidden = false;
              navigationUser.hidden = true;
              navLogo.description = "Use OAuth to log in to an ArcGIS Organization to view your items.";
            });
        }



        // Function to sign in or out of the portal used by the sign in/out button click event.
        function signInOrOut() {
          esriId
            .checkSignInStatus(info.portalUrl + "/sharing")
            .then(() =&amp;gt; {
              // If already signed in, then destroy the credentials to sign out.
              esriId.destroyCredentials();
              window.location.reload();
            })
            .catch(() =&amp;gt; {
              // If the user is not signed in, generate a new credential.
              esriId
                .getCredential(info.portalUrl + "/sharing", {
                  // Set the following property to false to not show a dialog
                  // before the OAuth popup window is open.
                  //oAuthPopupConfirmation: false,
                })
                .then(() =&amp;gt; {
                  // Once a credential is returned from the promise, check the
                  // sign in status to query the portal for items.
                  checkSignIn();
                });
            });
        }

        const infoButton = document.createElement("calcite-action");
          infoButton.text = "Sample information";
          infoButton.icon = "information";
          infoButton.label = "Info";

          const modalDiv = document.getElementById("modal");

          // Add event listener to the button
          infoButton.addEventListener("click", () =&amp;gt; {
            // Open the modal
            modalDiv.open = true;
          });
   
          let features = [];
          const webmap = new WebMap({
            portalItem: {
              id: "4c658233bb5c4dfe9aacf272a195ce75"
            }
          });
          const view = new MapView({
            container: "viewDiv",
            map: webmap,
            popupEnabled: false
          });



   
       const calciteLoader = document.getElementById("calcite-loader");
        //  await reactiveUtils.whenOnce(() =&amp;gt; !view.updating);


          // Once the view is finished updating, get the first layer in the webmap
          const featureLayer = webmap.layers.getItemAt(0);
          // Make sure the layer is loaded before constructing the FeatureTable
         // await reactiveUtils.whenOnce(() =&amp;gt; featureLayer.loaded);
        
         
    //do what you need here

   // Create the feature table
          // The title is created as a function that dynamically updates based on the amount of selected features within the table.
          // The actionColumnConfig property creates a new action column within the table that creates an action that zooms to the feature in that row.
          const featureTable = new FeatureTable({
            title: () =&amp;gt; {
              if (!featureTable) {
                return;
              }

              const state = featureTable.state;
              switch (state) {
                case "loading":
                  return "Loading layer data...";
                case "loaded":
                  const title = featureTable.layer?.title;
                  const rowCount = featureTable.size;
                  const selectedCount = featureTable.highlightIds?.length ?? 0;
                  return `${title} (${selectedCount} rows selected out of ${rowCount} total)`;
                case "error":
                  return "Error loading layer.";
                default:
                  return;
              }
            },
            description: "Table Description",
            actionColumnConfig: {
              label: "Go to feature",
              icon: "zoom-to-object",
              callback: (params) =&amp;gt; {
                view.goTo(params.feature);
              }
            },
            view: view,
            editingEnabled: true, // required to enable editing
            relatedRecordsEnabled: true, // required to enable related records
            layer: featureLayer,
            tableTemplate: {
              // TableTemplate is autocastable
              columnTemplates: [
                {
                  type: "field", // FieldColumnTemplate is autocastable, 'type' must be set
                  fieldName: "property_id",
                  label: "MNCAR",
                  icon: "key",
                  autoWidth: true // set this to have the columns automatically adjust width
                },
                {
                  type: "field",
                  fieldName: "MANUFACTURER",
                  label: "Manufacturer",
                  autoWidth: true
                },
                {
                  type: "field",
                  fieldName: "OPERABLE",
                  label: "Operable",
                  autoWidth: true
                },
                {
                  type: "field",
                  fieldName: "LASTSERVICE",
                  label: "Date of last service",
                  autoWidth: true
                },
                {
                  type: "field",
                  fieldName: "FLOW",
                  label: "Flow rate",
                  autoWidth: true
                }
              ]
            },
            container: document.getElementById("tableDiv")
          });

          reactiveUtils.when(
            () =&amp;gt; view.stationary,
            () =&amp;gt; {
              // Filter out and show only the visible features in the feature table.
              featureTable.filterGeometry = view.extent;
            },
            {
              initial: true
            }
          );
        

           // Listen for the view's click event.
          // Perform a hitTest on the clicked location
          // grab any features that fall within it
          view.on("click", async (event) =&amp;gt; {
            const response = await view.hitTest(event);
            const candidate = response.results.find(
              (result) =&amp;gt; result.graphic &amp;amp;&amp;amp; result.graphic.layer === featureLayer
            );

            if (candidate) {
              const objectId = candidate.graphic.attributes.OBJECTID;
              const index = featureTable.highlightIds.indexOf(objectId);

              // If there are objectIds in the highlightIds, remove the clicked feature
              // from the array. If there are no more objectIds, remove the filter
              // to show only selected records
              if (index &amp;gt; -1) {
                featureTable.highlightIds.splice(index, 1);
                if (featureTable.highlightIds.length === 0) {
                  featureTable.filterBySelectionEnabled = false;
                }
              } else {
                // Add the objectId of the clicked feature into the highlightIds.
                // This selects the feature in the table and sets a filter to only display
                // the selected rows
                featureTable.highlightIds.push(objectId);
                featureTable.filterBySelectionEnabled = true;
              }
            }
          });


           // Check if the highlights (selection) are being changed on the table by
          // checking and unchecking the rows,
          // and update the features array to match the table selection by adding
          // or removing as appropriate
          featureTable.highlightIds.on("change", async (event) =&amp;gt; {
            event.removed.forEach((item) =&amp;gt; {
              const data = features.find((data) =&amp;gt; {
                return data === item;
              });
              if (data) {
                features.splice(features.indexOf(data), 1);
              }
              // Check if there are no more selected rows in the table,
              // Once everything is unchecked, remove the filter for selected records
              if (featureTable.highlightIds.length === 0) {
                featureTable.filterBySelectionEnabled = false;
              }
            });

            // If the selection is added, push all added selections to array
            event.added.forEach((item) =&amp;gt; {
              features.push(item);
            });
          });


        });
      














       
      
      
    &amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;

  &amp;lt;body&amp;gt;
    
    &amp;lt;calcite-shell&amp;gt;
      &amp;lt;calcite-navigation slot="header"&amp;gt;
        &amp;lt;calcite-navigation-logo
          id="nav-logo"
          slot="logo"
          heading="Cushman Wakefield"
          description="Something Else"
        &amp;gt;&amp;lt;/calcite-navigation-logo&amp;gt;
        &amp;lt;calcite-button id="sign-in-button" slot="user"&amp;gt;Sign in&amp;lt;/calcite-button&amp;gt;
        &amp;lt;calcite-navigation-user hidden id="nav-user" slot="user"&amp;gt; &amp;lt;/calcite-navigation-user&amp;gt;
      &amp;lt;/calcite-navigation&amp;gt;



      
    &amp;lt;/calcite-shell&amp;gt;


    &amp;lt;calcite-modal id="modal"&amp;gt;
      &amp;lt;div slot="header"&amp;gt;About this application&amp;lt;/div&amp;gt;
      &amp;lt;div slot="content"&amp;gt;
        &amp;lt;calcite-notice open&amp;gt;
          &amp;lt;span slot="title"
            &amp;gt;This sample demonstrates how to display and edit related data using the
            &amp;lt;calcite-link
              target="_blank"
              href="https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-FeatureTable.html"
              &amp;gt;FeatureTable&amp;lt;/calcite-link
            &amp;gt;
            widget.
          &amp;lt;/span&amp;gt;
        &amp;lt;/calcite-notice&amp;gt;
        This example illustrates the main areas of functionality:
        &amp;lt;ul&amp;gt;
          &amp;lt;li&amp;gt;
            &amp;lt;calcite-link
              target="_blank"
              href="https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-FeatureTable.html#related-records"
              &amp;gt;Working with related records in the table&amp;lt;/calcite-link
            &amp;gt;
          &amp;lt;/li&amp;gt;
          &amp;lt;li&amp;gt;
            &amp;lt;calcite-link
              target="_blank"
              href="https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-FeatureTable.html#selection"
              &amp;gt;Syncing selection between table and view&amp;lt;/calcite-link
            &amp;gt;
          &amp;lt;/li&amp;gt;
          &amp;lt;li&amp;gt;
            &amp;lt;calcite-link
              target="_blank"
              href="https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-FeatureTable.html#editing-in-featuretable"
              &amp;gt;Editing in the table&amp;lt;/calcite-link
            &amp;gt;
          &amp;lt;/li&amp;gt;
          &amp;lt;li&amp;gt;
            &amp;lt;calcite-link
              target="_blank"
              href="https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-FeatureTable.html#filtering-and-sorting"
              &amp;gt;Filtering in the table&amp;lt;/calcite-link
            &amp;gt;
          &amp;lt;/li&amp;gt;
          &amp;lt;li&amp;gt;
            &amp;lt;calcite-link
              target="_blank"
              href="https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-FeatureTable.html#column-configuration"
              &amp;gt;Configuring columns&amp;lt;/calcite-link
            &amp;gt;
          &amp;lt;/li&amp;gt;
        &amp;lt;/ul&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/calcite-modal&amp;gt;

    &amp;lt;div id="viewDiv"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;div class="container"&amp;gt;
      &amp;lt;div id="tableDiv"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Oct 2024 16:23:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/async-function-messing-up-login/m-p/1552865#M85967</guid>
      <dc:creator>CW-GIS</dc:creator>
      <dc:date>2024-10-28T16:23:34Z</dc:date>
    </item>
    <item>
      <title>Re: async function messing up Login</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/async-function-messing-up-login/m-p/1552904#M85969</link>
      <description>&lt;P&gt;When I use the OAuth login, I have to wrap the code that requires the authenticated data in the checkSignInStatus method.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;const portalUrl = 'my portal url';
const info = new OAuthInfo({
  appId: "myAppId", //*** Your Client ID value goes here ***//
  popup: false // inline redirects don't require any additional app configuration
      });

identityManager.registerOAuthInfos([info]);
identityManager.getCredential(portalUrl);

identityManager.checkSignInStatus(portalUrl).then(function () {
  layer = new FeatureLayer({
    portalItem: {
      id: "myPortalItem"
    },
    outFields: ["*"]
  });

  map = new Map({
    basemap: 'oceans',
    layers: [layer]
  });

  view = new MapView({
    container: "viewDiv",
    map: map,
    center: [-153, 63],
    zoom: 4,
    popup: {
      autoOpenEnabled: false
    }
  });

  //etc.
});

      &lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 28 Oct 2024 17:30:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/async-function-messing-up-login/m-p/1552904#M85969</guid>
      <dc:creator>KenBuja</dc:creator>
      <dc:date>2024-10-28T17:30:53Z</dc:date>
    </item>
    <item>
      <title>Re: async function messing up Login</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/async-function-messing-up-login/m-p/1553614#M85995</link>
      <description>&lt;P&gt;Normally it would be good practice not to mix async..await and Promise when writing in JS. Maybe you can change the whole function to Promise.&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2024 00:48:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/async-function-messing-up-login/m-p/1553614#M85995</guid>
      <dc:creator>RamaniGanason</dc:creator>
      <dc:date>2024-10-30T00:48:58Z</dc:date>
    </item>
  </channel>
</rss>

