Interfaccia frontend
Frontend
Forms
API Integration
Costruisci un'interfaccia utente per visualizzare, aggiungere, modificare ed eliminare libri, collegata all'API backend.
CRUD di una biblioteca (Episodio 2)
Torna all'Academy
Frontend and CRUD Interactions
Quick Summary: Connect the backend to a user interface to display, create, update, and delete books using a store system and reusable components.
1. Main Page Structure
Create the frontend page
- Create a new page for the interface
- Design: book list on the left, interactions on the right
Configure the main container
- Create a container block with styles:
- Display: flex
- Gap: random value (e.g., 12px)
- Align-items: flex-start (top alignment)
- Width: 100%
- Padding: 12px
Create the list container (booklist)
- Add a block named booklist inside the container
- Configure styles:
- Display: grid
- Gap: 2px
- Grid-template-columns: 2 columns
- Width: 50% (half the space)
- Height: 100vh
2. BookInfo Component
Create the base component
- Create a new component named book-info
- Define styles:
- Width: 100%
- Height: 80px
- Background: use a token (e.g., background-lightest)
- Padding: 12px
- Border-radius: 8px
If the component isn't visible, change preview mode from "Compact" to "Stretch"
Add dynamic properties
- Create two custom text elements in the component
- First text: write
{title}- Curly braces create a dynamic property (turns blue)
- Second text: write
{author}
Configure component properties
- Click on the book-info component
- Define input properties:
- title (string)
- author (string)
- id (string) - add for future interactions
- Set default values for testing
Link properties to texts
- Click on the first custom text
- Click the lightning icon (⚡)
- Select the title property from the component
- Repeat for author
Style the title
- Select the title's custom text
- Modify styles:
- Font-size: 24px
- Color: content-deepest (for more contrast)
3. Store - Data Management
Create the store
- Create a new store named book-store
- The store holds frontend data
- Advantage: single database query, then data reuse
Define the store schema
Create the following structure:
books (array of objects)
├─ id (string)
├─ author (string)
└─ title (string)
4. Load Data on Mount
Loading logic
- Go to the page's Logic tab
- Use the onMounted trigger (when page loads)
Fetch the data
- Pull an execution wire from onMounted
- Call the getbooks route (created in previous episode)
- The route returns a response with all data
Update the store
- Create a get node to retrieve bookstore
- Create a setproperty node
- Connect bookstore to target
- Select the books property
- Connect the response from getbooks to the books field
- Connect the execution wire
Test with Execute and Ctrl+Space: db-books data appears in the store
5. Display Books with Template
Create the loop
- In the page, delete test book-info components
- Add a book-info component
- Right-click on book-info → Wrap a component
- Select Template
- Change from if to for (to iterate)
- Select books from the store
Link dynamic data
- Click on the book-info component in the template
- Define properties:
- title: click lightning → select title from template
- author: click lightning → select author from template
- id: click lightning → select id from template
In Builder mode, set template to "All" to see all elements (instead of "Once")
6. Creation Pop-up
Create the pop-up structure
- Click + or type "pop-up" in search
- Add a block in the pop-up containing:
- Two text input elements
- One button
Style the pop-up
- To visualize in development mode, force display in properties
- Configure the block:
- Width: 200px
- Display: flex
- Flex-direction: column
- Gap: 12px
- Configure the button:
- Width: 100%
- Text: "Create"
Configure inputs
- First input:
- Placeholder: "book title"
- Second input:
- Placeholder: "author"
Create page variables
- In the page, add two variables:
- title (string)
- author (string)
Link inputs to variables
- First text input: select model → choose title
- Second text input: select model → choose author
Model creates a two-way binding between input and variable
7. Creation Logic
Test value retrieval
- On the Create button, add an onClick event
- Create a get title and display in console
- Test in preview: write in input → click Create → see value
Call the creation route
- Delete the console test
- Pull a wire from onClick
- Call the createbooks route
- For the body:
- Pull a wire → automatically create a create object
- Connect get title to title field
- Connect get author to author field
Update the store
- Create a get bookstore
- Create a setproperty
- Select books
- Connect the response from createbooks
The book is now added to the list!
8. Managing Pop-up Display
Create the control variable
- Add a createBook variable (boolean) in the page
- On the pop-up, set the model: createBook
- If createBook = true → pop-up visible
- If createBook = false → pop-up hidden
Open pop-up with + button
- Create a button in the page
- Configure the button:
- Icon: plus
- Size: large
- Shape: round
- Style with position:
- Position: absolute
- Bottom: 16px
- Right: 16px
- Add an onClick event
- Create a set createBook → check (set to true)
Close pop-up after creation
- In the Create button logic
- Add a set createBook → uncheck (set to false)
- Place after store update
Clicking outside the pop-up automatically closes it
9. Selected Book Display
Add to store schema
Create a new object in book-store:
selectedBook (object)
├─ id (string)
├─ author (string)
└─ title (string)
Create the information container
- Add a bookinfo block (different from component)
- Place at top for clarity
- Configure:
- Width: 50%
- Add:
- One custom text for the title
- One block with two icons
Add action icons
- First icon:
- Icon: trash (delete)
- Size: larger
- Color: distinct color
- Cursor: pointer
- Second icon:
- Icon: pencil (edit)
- Color: different color
- Cursor: pointer
Selection logic in component
- In the book-info component, add an onClick event on the container
- Create a get bookstore
- Create a setproperty → select selectedBook
- Automatically create a create object
- Connect properties:
- get id → id
- get author → author
- get title → title
Display selected title
- In the bookinfo container's custom text
- Write
{selectedTitle}to create the property - Define selectedTitle =
bookstore.selectedBook.title
Clicking a book automatically updates the display
10. Deleting a Book
Deletion logic
- On the trash icon, add an onClick event
- Call the deletebook route
- Create a create object for the body:
- id: retrieve with get bookstore → property selectedBook.id
Update the store
- Create a setproperty for books
- Connect the response
Reset selection
- Create another setproperty for selectedBook
- Don't connect anything (leave empty)
- Reason: book no longer exists, clear selection
Improvement with condition
- Wrap the bookinfo container in a template
- Use if logic
- Condition:
bookstore.selectedBook.idexists (not empty) - Uncheck to invert: display only if id exists
After deletion, icons automatically disappear
11. Modifying a Book
Create the modification pop-up
- Copy the creation pop-up
- Rename to "Modify" (and the other to "Create" to differentiate)
- Change button text to "Modify"
Create control variable
- Add a modifyBook variable (boolean)
- On the Modify pop-up, set the model: modifyBook
Open pop-up and pre-fill
- On the pencil icon, add an onClick event
- Create a set modifyBook → true
- Pre-fill inputs:
- Create a get bookstore
- Retrieve selectedBook
- Create a spread to destructure
- set title → connect title from spread
- set author → connect author from spread
Modification logic
- On the Modify button, add an onClick event
- Call the updatebooks route
- Create the body:
- id: retrieve from bookstore.selectedBook.id
- title: get title (from variable)
- author: get author (from variable)
Update the store
- setproperty for books → connect response
- setproperty for selectedBook → connect body (to display new values)
- set modifyBook → false (close pop-up)
Summary of Key Concepts
| Concept | Usage |
|---|---|
| Store | Centralized frontend data storage |
| Template (for) | Iterate over an array to display multiple elements |
| Template (if) | Conditionally display elements |
| Model | Two-way binding between input and variable |
| Get property | Extract a property from an object |
| Set property | Modify a property of an object or store |
| Spread | Destructure all properties of an object |
| Page variables | Store temporary values (inputs, states) |
| onMounted | Execute code when page loads |
Data Architecture
Page Variables
├─ title (string) - temporary input
├─ author (string) - temporary input
├─ createBook (boolean) - creation pop-up control
└─ modifyBook (boolean) - modification pop-up control
Book Store
├─ books (array) - complete list
└─ selectedBook (object) - currently selected book
Practical Tips
- Always name clearly variables and components
- Use "All" mode in templates to see all elements in development
- Test with Ctrl+Space to visualize data at each step
- Pop-ups automatically close when clicking outside
- Use store tokens to access nested data
- Reset selectedBook after deletion to avoid errors
- Model creates two-way binding (input change = variable change)
- Remember to close pop-ups after actions (create/modify)
- Use spread to easily destructure complex objects