Develop the Tutorial Web UI ================================ Web UI First Steps ------------------- First we develop a simple Next.js\* application that will interact with the Tutorial Server. We develop and test it locally first on our own computer and will see how to package it in to an edge application later. .. note:: It is not essential to test the application on your own computer, if you do not have Node JS installed. When we are packaging the code at a later stage in the Dockerfile, we will be able to test it then. The application is developed in a single HTML file, which is a good way to get started with Next.js application. We will follow the `Docs `_ page where you can find much more details. Ensure `Node.js application `_ version 18 or later is installed on your computer and install Next.js using npm (part of NodeJS). .. code:: bash node --version npx create-next-app@latest tutorial-web-ui --ts --yes cd tutorial-web-ui npm add -s axios npx next dev --turbopack This creates all the needed files to run a Client and makes it available on **http://localhost:3000**. Open your web browser to this address to see the default page. It is clear that the default page is not what we want, so we will modify the code to our needs. Customizing the UI ------------------ The default page is a simple page with a link to the Next.js documentation. We will remove this and only add what we need. First we will remove the `app/page.tsx` file and create a new file **app/page.tsx** with the following content: .. code-block:: :linenos: 'use client' import {useEffect, useState} from 'react'; import axios from 'axios'; // Axios Interceptor Instance const AxiosInstance = axios.create({ baseURL: process.env.NODE_ENV === 'development' ? 'http://localhost:8000' : '/api' }); export default function Home() { const [count, setCount] = useState(0); const [greeting, setGreeting] = useState("not yet set"); const [error, setError] = useState(null); useEffect(() => { AxiosInstance.get('/counter') .then(response => { setCount(response.data.count); }) .catch(error => { setError(error.message); }); }, []); useEffect(() => { AxiosInstance.get('/') .then(response => { setGreeting(response.data.message); }) .catch(error => { setError(error.message); }); }, []); return (

{greeting}

Counter

{count}

{error &&

Error: {error}

}
); } While you do not need to understand all the details of the code, it is clear that we are using Axios library to make calls to the Tutorial Server. We are using the `useState` and `useEffect` hooks (from React) to manage the state of the local variables. `Tailwind CSS `_ is used by default with Next.js, therefore, it is easy to style the page. Verifying the UI ---------------- To verify the UI, keep the Tutorial Server running in one terminal and start the Next.js application in another with: .. code:: bash npx next dev --turbopack And open your web browser to **http://localhost:3000**. .. figure:: ../images/app-orch-tutorial-web-ui.png :alt: Tutorial Web UI with browser tools .. note:: The browser tools are open in the image above, showing the network requests and the console output. This is an essential tool to understand the requests that are going between your browser and the Tutorial Server. While your browser is still open, run the **curl** commands from the Tutorial Server page to see that requests from the UI are equivalent to those from the command line, and that the UI is updating the counter as expected when the buttons are clicked. Next steps ---------- You can now package the Tutorial Web UI and Tutorial Server in to a Container images so that we can deploy them to the edge.