Forge repo instructions - 05-finalize-the-ui-solution

Finalize the UI

In this section, we’ll explore UI containers to enable us to layout the UI components better. We’ll also discuss how to reference images hosted from an external source.

Add ample spacing between the header and the Text components

  1. Update the content body in src/frontend/index.jsx by encapsulating them inside a Box component. First add Box to the import statement.

    import ForgeReconciler, { Text, Heading, Box } from '@forge/react';
    
    
  2. Move the Text components inside the Box.

    <>
      <Heading size='medium'>Welcome, {data ? data[0].name : '[Loading]'}!</Heading>
    
      <Box>
        <Text>Life expectancy: {data ? data[0].min_life_expectancy : '[Loading]' } – {data ? data[0].max_life_expectancy : '[Loading]' } years</Text>
        <Text>Height: {data ? data[0].max_weight_female : '[Loading]' } inches</Text>
        <Text>Weight: {data ? data[0].max_height_female : '[Loading]' } lbs</Text>
      </Box>
    </>
    
    
  3. Let’s see it in action. Let’s run the tunnel.

    forge tunnel
    
    
  4. Use the paddingBlock property of Box to add space between the header and the content.

    <>
      ...
      <Box paddingBlock='space.200'>
        ...
      </Box>
    </>
    
    

Add an Image component and reference the image from an external source

  1. To use the Image component, first add it to the import

    import ForgeReconciler, { Text, Heading, Box, Image } from '@forge/react';
    
    
  2. Add the Image component and use a chihuahua stock image from API Ninjas as source

    <>
      <Heading size='medium'>Welcome, {data ? data[0].name : '[Loading]'}!</Heading>
    
      <Image src="https://api-ninjas.com/images/dogs/chihuahua.jpg"/>
      <Box paddingBlock='space.200'>
        <Text>Life expectancy: {data ? data[0].min_life_expectancy : '[Loading]' } – {data ? data[0].max_life_expectancy : '[Loading]' } years</Text>
        <Text>Height: {data ? data[0].max_weight_female : '[Loading]' } inches</Text>
        <Text>Weight: {data ? data[0].max_height_female : '[Loading]' } lbs</Text>
      </Box>
    </>
    
    
  3. If your tunnel is running, you will get a permission-related error. Don’t worry, this is EXPECTED.

Fix the error. Forge linter to the rescue!

  1. Verify your manifest.yml if you indeed do not have the permissions defined according to the error message we got.

  2. If your tunnel is still running, stop the tunnel by pressing CTRL + C.

  3. To fix the error, we’ll use a special linter command to automatically add the missing permissions. Go to the command line, and inside your project directory run the command

    forge lint --fix
    
    
  4. Check your manifest.yml again and ensure that the necessary external permission for api-ninjas.com has been added. Your manifest file’s permission section should look like this

    permissions:
    external:
        fetch:
        backend:
            - address: api.api-ninjas.com
        images:
        - address: api-ninjas.com
    scopes: []
    
    
  5. Since you changed something in your manifest file, you need to redeploy your app and upgrade the installation.

    forge deploy --approve MAJOR_VERSION_RULE
    
    
    forge install --upgrade
    
    
    Your app will be upgraded and additionally exchange data with the following urls:
    - api-ninjas.com
    
    ? Do you want to continue? Yes
    
    
    ? Are you sure you want to proceed? Yes
    
    
  6. Once installation upgrade is done, verify that your app is working as expected.

Update the dog image to make it dynamic

  1. Before we update the UI, let’s run the tunnel again so we can see the UI changes in real time

    forge tunnel
    
    
  2. Checking the API response payload, we can see these properties:

    [
      {
        "image_link": "https://api-ninjas.com/images/dogs/chihuahua.jpg",
        "good_with_children": 1,
        "good_with_other_dogs": 3,
        "shedding": 2,
        ...
      }
    ]
    
    
  3. In src/frontend/index.jsx, Replace the static image with the image being returned by the API. Add an alt property as an alternative display if the image has not been loaded yet

    <Image src={data ? data[0].image_link : "https://api-ninjas.com/images/dogs/loading.jpg" } alt='[Loading]'/>
    {/* Replace the line below */}
    {/* <Image src="https://api-ninjas.com/images/dogs/chihuahua.jpg"/> */}
    
    
  4. After saving the index.jsx file, you should be able to see a glorious image of a chihuahua

Finishing touches: align the image with the text content

  1. Use the Inline component. Add it to the import statement

    import ForgeReconciler, { Text, Heading, Box, Image, Inline } from '@forge/react';
    
    
  2. Place the Image and Box components inside Inline

    <>
      <Heading size='medium'>Welcome, {data ? data[0].name : '[Loading]'}!</Heading>
    
      <Inline>
        <Image src={data ? data[0].image_link : "https://api-ninjas.com/images/dogs/loading.jpg" } alt='[Loading]'/>
        <Box paddingBlock='space.200'>
          <Text>Life expectancy: {data ? data[0].min_life_expectancy : '[Loading]' } – {data ? data[0].max_life_expectancy : '[Loading]' } years</Text>
          <Text>Height: {data ? data[0].max_weight_female : '[Loading]' } inches</Text>
          <Text>Weight: {data ? data[0].max_height_female : '[Loading]' } lbs</Text>
        </Box>
      </Inline>
    </>
    
    

Bonus exercises

  1. Change the image size to medium as it looks too big for the panel. Go to the Image component documentation.

  2. Move the image to the right side of the text. Try using different Inline properties and see how it affects the UI.

  3. Change the color, size, and/or weight of your Text components. Check out the Text properties.