This tutorial shows you how to build a countdown widget, which counts down to a specific date. It also contains a registration widget where site visitors can register and receive an email notification the day before your event. You will also learn how to install your countdown widget on a site and explore all the key features of Blocks.
To make things easier, we've created a template so that you can get started right away.
The template contains:
Get the template
To build this widget, start with this template and edit it under your Wix account. The template has been left incomplete so that you can learn how to add elements to your widget.
The widget is designed with a grid so that you can easily align and place elements.
Learn more about designing your widget
You can define an API for your widget, so that the site builder who installs the widget can interact with it through code. The API can contain properties, events and exported functions.
This widget uses two properties:
Learn more about widget API properties
First you need to give your widget's elements an ID so that you can easily refer to them in the code.
secondsTxt
. Note that the other elements have already been give IDs.endDate
.emailId
. This is a Text type property with no default value.To make your widget count down we need to create a function in the code that calculates the remaining time until the end date. To do this, your template comes with some pre-installed utilities that you can find in the Public & Backend menu.
Blocks uses a new global variable, $widget and the property, $widget.props, that holds all the properties that you have defined for your widget.
Blocks also uses Velo autocomplete, so that you can write code more easily.
Learn more about the widget API
Create a function, updateTime()
, so that your countdown widget will update the count. Your new function should look like this:
You also need to set an interval for how often the widget updates the remaining time.
To do this, add the following code in your onReady()
function. This updates the widget every second.
Edit time condition Velo code only runs when you click preview or when you open the live site. The Blocks onReady code runs also during editing time, so that you can see it in action while working in the editor. We use the above condition so that your countdown widget doesn't count down during editing time.
You are ready to preview your widget and its API properties.
You can create a custom settings panel so that site builders can change the settings of your widget when they install it on a site. In this example the custom settings panel allows the site builder to change the end date and define an email ID that determines which email will be sent to users when they register to receive notifications.
Learn more about panels
endDateInput
.emailIdInput
.The design of your custom panel is complete. Now you need to implement it by adding code.
You want your custom panel to display the current end date and email ID when it loads. The site builder uses the panel to change these values. You need to add code to the panel so that when these inputs change, it updates your widget.
Learn more about adding code to your custom panels
To enable panel elements to interact with your widget and perform actions in the editor, you can use the Velo wix-widget
module in your code.
To use the Widget API, import wixWidget
from the wix-widget
module.
Insert the following code before your onReady()
function.
You want your widget to update when the values change so you need to register an onChange
event. You also need to add async
in your onReady ()
function as you will be using some asynchronous functions.
Your onReady()
function should look like this:
Click Run or Preview to check your code
Now that your panel is designed and coded you need to configure your widget so that your panel connects to one of your widget's action bar buttons.
The Configuration tab within the Editor Experience Panel enables you to control how your widget behaves when a site builder installs and customizes it on a site. You can give your widget and its elements display names, so that it is clear to site builders what your widget does. You can also make changes to the floating action bars that appear in the editors when site builders select elements in your widget.
Learn more about configuration.
The template comes with a second widget called Registration. You can find it under Widgets and Design.
When you click Layers you can see that it has been created as a multi-state box with three states. It has a button labelled Register, which changes to Submit when the site visitor clicks. It also has a field box where site visitors can add their email address. A thank-you message appears when a site visitor has registered.
This widget also comes with a design preset which can be used for mobile view.
Learn more about design presets
You're now ready to add your registration widget to your first widget, the countdown widget. In Blocks, you can create lots of different widgets and add them to other widgets.
Learn more about working with widgets within widgets
registration
in Properties & Events .Another way to add a widget You can also add an inner widget by clicking the Add Elements menu. Select MY WIDGETS and drag and drop the widget you want to add into the first widget.
You'll implement the code for user registeratiion in the main (countdown) widget, using the pre-installed utility, contactUtils.js
. It uses the wix-crm
API to connect your contact to your collection.
This code directs the information to your collection. It uses the utility you will use later when you create a collection. You can see getSubscriptionsCollectionName()
in the code.
onSubmit
. It uses the create contact utility.Your code should look like this:
In your onReady()
, after your updateTime()
function, add the following code:
Backend code is already included The backend code is already implemented in your template in Blocks. It includes an export function, notify(), which uses the pre-installed utilities. It checks the remaining time, triggers the email when ready and updates the collection. You need to invoke this in the editor, as explained below in Step 10.
Now you are ready to build your app for the first time. Your first build will be a major build. Later, when you are working on your app you can choose to do a minor build or a major build. A minor version is updated automatically on all the sites where it appears. You just need to refresh the page. When you do a major build a site builder needs to update your app manually. A dot appears next to the name of the app Installed Apps panel in the Editor so that site builders know there is a new version available.
Learn more about versions in Blocks
When you click build for the first time blocks prompts you to give your app a namespace. This namespace is used to refer to your collection in Velo code in the editors and in the app’s code in Blocks.
Learn more about the app namespace
Now you need to create a collection to store all of the subscriptions. Collections in Blocks are empty placeholders that you design in Blocks, which will be filled with data once the app is installed on a site. Learn more about collections in Blocks
The template has a pre-installed utility called getSubscriptionsCollectionName()
, in collectionUtils.js under Public & Backend. It constructs the full name of the collection so that you don't have to add your full app namespace every time you refer to it in the code.
Your code should look like this, with your own namespace.
You want any site visitor to be able to add content to your collection by subscribing.
You can install your widget on any of the editors. The following example uses the Wix Editor. Before you install your app you need to build your app a second time. This will be a major build because you have created a collection.
You widget is fluid and you can select its elements so that you can customize the widget to your site. You can also open the settings panel and change the default end date.
Users can register for an email notification. You need to create the email that will be sent and to get an ID for it so that the widget API can identify which email needs to be sent.
Backend code is included with your app in Blocks, but you need to invoke the notification in the editor.
backend.jsw
.Your code should look like this:
Now create an export function invokeNotify
in backend.jsw in your site's code section.
Your code should look like this:
Now you need to create a scheduled job.
Your code should look like this:
Now you are ready to test your app.
Check your triggered emails You can also check in Triggered Emails under Developer Tools in your dashboard to see if the email has been sent.