Purpose: To add required finishing touches on Project Part 5. These finishing touches chiefly involve rearranging code and require very little adding. They do, however, require you to understand concepts taught in previous exercises, and they serve as an opportunity to firm up your understanding by applying them in the context of your project.
The general rule is that page components should be simple and high-level. The HTML forms in ProfessionalsAdd, ServicesAdd, and ServicesList are somewhat messy. In the reactapp/src/widgets directory, add components to make the ProfessionalsAdd and ServicesAdd pages simpler and more high-level:
Props Needed: You may notice that these new subforms no longer have access to the state variables that are part of the way submission is handled. However, if you set up each component to accept a "handleSubmit" attribute and pass the handleSubmit function as shown below, it should work. The ServicesAddForm will also need a categoryid, which should be passed as a prop.
Passing functions down from parent to child components in this way is called "prop drilling."
Sidenote on Required vs. Encouraged Components: The components named above are required components in the project, among others. Although you are only required to make certain components, you are encouraged to make as many additional components as desired to give your code a clean and manageable feel.
In Exercise 5.2C, you learned to convert your image button to add an image preview by hiding the file input and using another object simulate clicking on it, using useRef to track. In reactapp/src/widgets, create an ImageUpload component that does this. Replace the <input type="file"> in your ProfessionalsAddForm and ServicesAddCategoryForm with ImageUpload.
The goal of this step is to create a separate nav for the Admin, ProfessionalsAdd, and ServicesAdd routes, using nested Routes in App.jsx. The basic idea of this was taught in Exercise 5.2D "Nesting and Using <Outlet>". However, our current situation differs in two ways from the one in that exercise: (1) We are loading nav JSON from a file, and (2) our Nav links are buried in several layers of components (App > Layout > Nav > NavLinks). The solution has three parts:
Create Admin Nav File. Your admin nav should point to the "home", "admin", "professionals_add", and "services_add" routes. Create a new JSON file, reactapp/src/assets/navAdmin.json to set up an alternate nav with these links. Use the same Layout and Nav components you used earlier, but pass in the JSON object with these links as a prop.
Add Prop Drilling: In order to control the nav from App.jsx, we must pass down information from App to Layout to Nav to NavLinks. To do this, you will need to add a prop to Layout, to Nav, and to NavLinks. Passing information down from parent to child to child over multiple layers is often called "prop drilling." It is a necessary, though annoying, need that often comes up in React projects because of the way React is built. You have basically two options for what prop to pass down, in order to control from App.jsx:
Either option is acceptable. Neither requires much code changing, but you may find option 2 requires slightly less change.
Add Nested Routes in App.jsx: Finally, in App.jsx, wrap the Admin, ProfessionalsAdd, and ServicesAdd routes in the alternate layout, as shown in Exercise 5.2D.
Add titles to your pages as taught in Exercise 5.2D "useLocation Hook". You may use your nav.json data to set these titles, or you may use custom JSON of your own. You may set page titles from any component if you use document.title. Each page title should look like "Company Name - Page" where Page uniquely identifies the page.
In the previous exercise, you made const apiURL point to "http://localhost:5000", the Flask Dev Server. To prepare to deploy the project, you will need const apiURL to point to the right place on both localhost and webhost, dev and prod mode.
Assuming you run "pnpm run build" and copy your dist folder to htdocs/p5, and also assuming you set up Flask CGI at htdocs/p5api, and you also deploy to the Webhost as instructed, the following nine configurations are possible on localhost (using student1.cs3660.xyz as your subdomain), though the highlighted ones are the most interesting:
| React Dev Mode | React Prod Mode | React on Webhost | |
| Flask Dev Mode |
React: http://localhost:5173 Flask: http://localhost:5000 |
React: https://localhost/p5 Flask: http://localhost:5000 |
React: https://student1.cs3660.xyz/p5 Flask: http://localhost:5000 |
| Flask Prod Mode |
React: http://localhost:5173 Flask: http://localhost/p5api |
React: http://localhost/p5 Flask: http://localhost/p5api |
React: https://student1.cs3660.xyz/p5 Flask: http://localhost/p5api |
| Flask on Webhost |
React: http://localhost:5173 Flask: https://student1.cs3660.xyz/p5api |
React: http://localhost/p5 Flask: https://student1.cs3660.xyz/p5api |
React: https://student1.cs3660.xyz/p5 Flask: https://student1.cs3660.xyz/p5api |
You may notice that in both the blue and yellow configurations above, React is running from the dist folder, and const apiURL = "../p5api"; would point correctly to the Flask server.
Vite gives us global variables that can help us determine which version of React is running:
Because of this, in the above configuration, the following line should cover all three highlighted scenarios:
Since your code is required only to work in the green and yellow configurations above, you could also use an absolute path instead of a relative one, although there is a preference in this class to make code deployable anywhere. It is worth noting that import.meta.env.VITE_variable name will also pull a variable name from a .env file if the variable name begins with "VITE_" as a prefix, although a server-dependent .env file should not be needed if you use the configuration above.
When building React code that involves React Router, remember the important instructions in Exercise 5.3 "React Prod Mode and URL Issues" about making a .htaccess file and putting basename in <BrowserRouter>, or your built React won't work right. Also remember, when deploying to webhost, to follow the steps in this order: