Updating Options
Update integration options from within the integration
The following recipe walks through how you can allow users to update an option from within their integration.
This recipe will assume you are allowing a user to update an option with the key apiKey. The code can easily be modified to support updating any option. When implemented, your users will see the following prompt when the API key for the integration they are using is about to expire:

Adding Styles
We will first add the following styles to our stylesheet.
Updating our Template
With our styles in place we are ready to add our template code. The template code will display a message to the user and provide an input where the user can enter a new API key. The template is only shown if the apiKeyExpired computed property is set to true.
Implementing Component Logic
Next we need to focus on our component code.
Injecting Services
To begin, we will be using several built-in services. We can inject these services into our component using the Ember.inject.service() method at the top of the component file.
Session Service
The first service we inject is the session service which will allow us to make REST API requests on behalf of the user. This is required to make the REST request necessary to update the logged in user's option.
Store Service
The store service is used to push our updated option value into the in-memory store used by the Polarity web application. This is important to make sure that after we update the option value on the server, the option value is also updated locally on the client.
Flash Messages
The flashMessages service is a service that makes it easy to show a toast notification to the user. We can use it to inform the user when we've successfully updated the option as well as let the user know when an error occurred. Flash messages are best used for short, transient messages.
Initializing Variables
We need a boolean value indicating whether or not our API key is expired. For this sample, we will use a computed property that returns a value, __apiKeyExpired , set by the server in the integration's integration.js file. If this value is true then the computed property apiKeyExpired will return true and our template code will be shown to the user.
We'll also add an alias for the block._state property which we will initialize below:
In addition to the apiKeyExpired computed property we need to add an init method which is called when the component is first rendered. This init method will initialize an object where we will store all our variables related to updating the options.
If your integration already has an init method, just ensure that the init method is initializing the block._state variable (referenced by our state alias in the example above), and that the state variable also has the option and option.newApiKey variables initialized (lines 3, 4, 5).
Update Option Method
In our component code we will add a method called updateOption that is able to update a given option based on the option's unique id. This method can be added outside of the actions hash within the block.js component file.
The updateOption method constructs the proper payload to update the option specified by optionId and then uses the native fetch API to issue a REST API request to the Polarity server. It uses the logged in user's existing session by fetching it via the session helper this.session.authenticatedHeaders().
This method can throw an error in the even the fetch call fails. It can also return an error on the __errorMessage property which is added to the fetch response object.
Save API Key Action
Now that we have our updateOption method in place, we can wire up our saveApiKey action. This method is an action on the components "action" hash that is triggered when the user clicks on the "Save" button we added our template.
The saveApiKey action will call the updateOption method and also handle updating any state that should be displayed in the template such as an updating icon or error messages.
Remember that the saveApiKey method is an action and should be included as part of your actions hash:
Note that within the saveApiKey method on line 33 we set the __apiKeyExpired value to false. In your own implementation you may be storing this value on a different variable. You will want to make sure you set any variable indicating that the apiKey is still expired or expiring to false so that the template no longer shows the update API key form to the user.
Also note that on line 18 we are setting the option key to apiKey. If your option key is something different (e.g., password), then you can modify this line to match the key for your option.
Flash Messages
The final method we will add is for displaying our toast notifications. This method should be located outside of the actions hash. The method uses the flashMessages service that we injected earlier to display toast notifications.
Putting it all together
The final component code will look like this:
The Final Result
When the apiKeyExpired computed property is set to true, the user will be prompted to update their API Key:

Once they enter their API key and click "Save", they will be shown a success message like this:

Last updated