Hi Guys, welcome to the video.
In this video we will build a dynamic country list page.
For this, we will request the JSON data using http processor
and store it in
fastn
records and later in the video, we will create a country list page that
will display the list of countries in form of cards. Each country card will
display country's flag and country's common
name and also display values of
population
, region
and capital
.
In the first part, we will do data modelling by declaring all the records
in
a separate document.
In the second part, we will create a card
component that will contain the
data.
http processor
to
request the data and store in a list and display the data by calling the
component.
So the country has name, capital, region, population, and flags properties at
one level. common
and official
names of a country are grouped under the
name
property.
Some countries have more than one capital, so we will create a list for capital property.
Also, flags have nested properties "svg" and "png" in the JSON data. We will utilize the "svg" property.So I have this package country-details in my machine.
I will create a separate document called models.ftd
where we will do the data
modelling using records
.
record
.-- record country:
country-name name:
integer population:
string region:
string list capital:
country-flag flags:
name
property has a type that itself is a record
which we will create in a
bit.
population
is an integer while region
and capital
are of string type.
Also, some countries have more than one capital hence we will create the list
of capital
.
Last but not the least, flags
also has a record
datatype.
country-name
and country-flag
records too.-- record country-name:
optional string common:
string official:
-- record country-flag:
caption svg:
The country-name
record has two properties common
and official
, both of
string type.
And the country-flag
record has svg property which can be passed as caption.
card
componentMoving to the second part of the video, we will create a card
component.
I will put all the components inside a components folder.
In this folder, I have created a card.ftd
document.
models.ftd
hence at the top of the card.ftd
we will import that document.-- import: country-details/models
In the import line we will write the package name, slash, and the document name we are importing, that is, models
Now, create a component let's saycountry-card
.-- component country-card:
-- end: country-card
caption models.country country:
And structure the card in a way that I showed at the start using columns and row and putting the flag, common name, population, region and capital. And apply fastn properties appropriately.
Main column-- ftd.column:
width.fixed.px: 260
height.fixed.px: 375
overflow: auto
border-radius.rem: 0.5
margin.rem: 2
cursor: pointer
border-width.px: 1
border-color: #dedede
-- ftd.image:
src: $country-card.country.flags.svg
width: fill-container
height.fixed.percent: 50
-- ftd.column:
padding.rem: 1
spacing.fixed.rem: 0.5
width: fill-container
border-color: #dedede
height: hug-content
border-top-width.px: 1
-- ftd.text: $country-card.country.name.common
style: bold
role: $inherited.types.copy-regular
-- ftd.row:
spacing.fixed.rem: 1
-- ftd.column:
spacing.fixed.rem: 0.5
-- ftd.text: Population:
role: $inherited.types.label-large
style: semi-bold
-- ftd.text: Region:
role: $inherited.types.label-large
style: semi-bold
-- ftd.text: Capital:
if: { len(country-card.country.capital) > 0 }
style: semi-bold
role: $inherited.types.label-large
-- end: ftd.column
-- ftd.column:
spacing.fixed.rem: 0.5
-- ftd.integer: $country-card.country.population
role: $inherited.types.label-large
-- ftd.text: $country-card.country.region
role: $inherited.types.label-large
-- ftd.text: $capital-name
style: bold
role: $inherited.types.label-large
for: $capital-name, $index in $country-card.country.capital
-- end: ftd.column
-- end: ftd.row
-- end: ftd.column
-- end: ftd.column
-- end: country-card
-- ftd.shadow default-card-shadow:
color: #efefef
blur.px: 5
spread.rem: 0.2
-- ftd.shadow hovered-card-shadow:
color: #d5e3db
blur.px: 5
spread.rem: 0.2
So, we will add shadow property to the component, and create a mutable boolean variable.
;; shadow properties-- component country-card:
caption models.country country:
optional ftd.shadow shadow: ;;<hl>
boolean $is-hovered: false ;;<hl>
shadow: $default-card-shadow
shadow if { country-card.is-hovered }: $hovered-card-shadow
$on-mouse-enter$: $ftd.set-bool( $a = $country-card.is-hovered, v = true )
$on-mouse-leave$: $ftd.set-bool( $a = $country-card.is-hovered, v = false )
We are done with the second part. Everything needed to display the data is ready. Now, We will request the JSON data, and display the data in the card using the component.
In the index.ftd document , We will need the two documents and processors so import theprocessors
and the two documents.-- import: fastn/processors as pr
-- import: backend/models
-- import: backend/components/card
countries
and the datatype will be record country
that we created in models
document.-- models.country list countries:
$processor$: pr.http
url: https://restcountries.com/v3.1/all
The data will be stored using processors by doing http request to the endpoint we will give as URL.
Now we will call the componentcountry-card
from card
document and we will
wrap it inside the row container component.-- ftd.row:
wrap: true
spacing: space-around
padding.rem: 2
border-radius.rem: 1
-- card.country-card: $country
for: $country in $countries
-- end: ftd.row
We have passed the reference value of property country of the country-card component in the caption.
And, applied for loop.I hope you have learnt with me and found this video easy to follow.
Join us on Discord, and share your package which you will create following this
video. You can share it on the discord's show-and-tell
channel.
Thank you guys, keep watching these videos to learn more about fastn. Checkout
the fastn
website.