This guide provides a comprehensive, step-by-step process to create your first custom widget using Angular and the TakeProfit Widget SDK (version 0.2.3). By following this guide, you’ll set up an Angular project, integrate the SDK, configure necessary security headers, and implement basic functionalities to interact with the TakeProfit platform. This example serves as a foundation for building more complex and feature-rich widgets tailored to your specific needs.
npm install -g @angular/cli
.Yes
CSS
(or your preferred choice)http://localhost:4200/
.
package.json
to ensure takeprofit-widget-sdk
is listed under dependencies
:
server.js
in the root of your project:
server.js
and add the following code:
https://takeprofit.com
.X-Frame-Options
and Content-Security-Policy
to allow embedding only from https://takeprofit.com
.dist/my-tp-widget
directory.index.html
: Ensures that Angular’s routing works correctly.dist/my-tp-widget
directory.
tp-widget-sdk.service.ts
in the src/app/services
directory.
src/app/services/tp-widget-sdk.service.ts
and add the following code:
TpWidgetSdkService
: Provides methods to initialize, cleanup, and interact with the TakeProfit Widget SDK.@Injectable
decorator, so no additional configuration is needed.
widget.component.ts
, widget.component.html
, and related files in the src/app/components/widget
directory.
src/app/components/widget/widget.component.ts
and add the following code:
if (result.isSuccessful)
to if (result.payload.isSuccessful)
to correctly access the isSuccessful
property within the payload
.${result.error}
to ${result.payload.error}
to correctly access the error
message within the payload
.ngOnInit
: Initializes the SDK and subscribes to widget and channel state changes.ngOnDestroy
: Unsubscribes from state changes and cleans up the SDK.handleWidgetChange
: Logs widget state updates.handleChannelChange
: Logs channel state updates.changeSecurity
: Requests a security change and handles the response.addMenuItem
: Adds a new menu item labeled “Refresh Data”.updateMenuItem
: Updates the existing menu item’s label to “Refresh Data (Updated)”.removeMenuItem
: Removes the menu item and resets menuItemId
.src/app/components/widget/widget.component.html
and add the following code:
src/app/components/widget/widget.component.css
and add the following styles:
src/app/app.component.html
and replace its content with the widget component selector:
widget.component.ts
) already includes methods to handle widget and channel state changes (handleWidgetChange
and handleChannelChange
). These methods log the updated states to the console. You can expand these methods to perform additional actions based on state changes as needed.
widget.component.ts
The menu item functions are already implemented in Step 5. Here’s a recap:
menuItemId
.menuItemId
and resets it.menuItemId
to prevent conflicts. In this example, we’re managing a single menu item, but you can extend this logic to handle multiple items as needed.
http://localhost:8080
.
F12
or Ctrl+Shift+I
to open the developer tools.
'http://localhost:8080'
with your actual widget URL if different.imageUrl
for the widget icon.dist/my-tp-widget
, server.js
, etc.) are correctly set up and that your server configuration (server.js
) is compatible with your hosting service.
your-app-name
with your desired app name.
Procfile
Create a Procfile
in the root of your project to specify how to run your app:
Procfile
:
main
branch, use git push heroku main
instead.
iframeUrl
in the TakeProfit platform’s local storage to point to your deployed widget’s URL:
requestId
and menuItemId
are unique to prevent conflicts.
takeprofit.com
).iframeUrl
and ensure it points to the correct widget URL.TPWidgetSDK.connect()
and TPWidgetSDK.hideLoader()
are called successfully.securityID
you are requesting is valid and recognized by the platform..catch
Blocks: Ensure that all asynchronous SDK methods have proper error handling using .catch
to manage rejected promises.