Purpose: To incorporate the Admin, ProfessionalsAdd, and ServicesAdd pages into React. These pages submit data from the server. To recap, we have followed this progression:
In Exercise 5.4A, you moved your Javascript AJAX helper functions into reactapp/src/assets/api.js. In this exercise, make api.js the central place for every reference to your Flask server and every piece of code that uses the fetch function or other AJAX functions to communicate with it, as follows:
The purpose of separating concerns this way is to put connection code for the Flask server all in one place, similar to the way we put model code that connects to files or databases in one place in our server code. That way, if we ever decide to change out fetch for another library or make other changes to the server connection, we can change that connection without changing other code.
In the reactapp/src/pages directory, create Admin.jsx. Copy the HTML / Jinja from flaskapp/templates/admin.html. Convert to JSX. Test that the data_backup and data_restore links communicate with the Flask server, not the React server.
Path Issue with <Link to> inside <Outlet>: When you change the <a href> links in Admin.jsx to <Link to> components, you may find they point to the wrong link. That will be because the Admin page is called within the <Outlet> component. All React Router links within the <Outlet> component act like they are in a subpath. Correct as needed using the ../ parent path.
In the reactapp/src/pages directory, create ProfessionalsAdd.jsx. Copy your HTML / Jinja from flaskapp/templates/professionals_add.html. Convert to JSX, including replacing the {% include %} directives with the ProfessionalsList component. The sample app below is meant to help guide you in making the connections with the flask app and in refreshing the list in React.
Sample App with ProfessionalsAdd. This sample app has working Professionals and ProfessionalsAdd pages. Please download and take the time to experiment with it until you understand how it works. You do not need to mimic it exactly, but do retype any code you take from it at least once, per the class policy. This sample app is designed to work only in localhost dev mode. You will have to extend it to work in other modes.
Connecting to Flask App. Make sure any AJAX-related javascript is in api.js, has the export keyword, and is imported into your component code. Modify the AJAX-related javascript as needed to use apiURL from api.js.
Refreshing the List (not using AJAX Includes). In Project Part 4.5, you refreshed the list of professionals and services using an AJAX include after AJAX form submission. The sample app above does not use AJAX includes, even though React can be forced to do it (see FlaskPatch in Exercise 5.4A). Anything that inserts HTML directly is considered bad form in React, and it also defeats the purpose of an API-driven back end. Instead, the sample app uses a state variable and conditional rendering, a much more React-friendly approach. The strategy is to unmount the list just before form submission and re-mount it just after. Take time to understand what is happening in order by both running the code, looking at the code, and modifying the code as needed, until you see what is happening with each of the steps below:
This is not the only way or even the most efficient way to refresh the list. For example, it would be one step more efficient to refresh the list by returning JSON from the professionals_add route in the Flask server and using that data to refresh the list. Responding in AJAX would be a more common professional approach, but the main reason we are not doing it is to modify our code minimally and keep to our scheme of simple text error and ok messages.
In the reactapp/src/pages directory, create ServicesAdd.jsx. Copy the HTML / Jinja from flaskapp/templates/services_add.html and convert to JSX. The ServicesAdd page component is more complex than ProfessionalsAdd, both because the data is two dimensional and because there is an Add Service form at the end of each category, and because there are two types of forms.
Add Category Form. The Add Category Form works almost identically to the Add Professionals Form, so your code can stay close to what you did for that, both for rendering the form and refreshing the list after form submission.
Add Service Forms. The Add Services Forms were rendered inside of the services_list.html in your Flask app. You may already have copied over the code for them while creating the ServicesList component in the previous exercise. If not, now is the time to put it in. In Project Part 4.5, you used a variable to control whether this form appears when ServicesList is used. The same effect can be achieved by passing in a prop.
Refreshing the List After Add Service: It is slightly trickier to refresh the list after adding a service, because the form is in the ServicesList component, not servicesAdd component. Conditional rendering of the ServicesList component would be awkward, since you are already inside the ServicesList component, although it could be done by splitting up the component into subcomponents. However, there is a simpler way to refresh that you might prefer. If you created a getServices function that is similar to the getProfessionals function, you can just call that function when the AJAX query is done. The whole point of mounting and unmounting using conditional rendering was to force useEffect to run that function anyway, and so it is simpler just to skip that step and run the function.
At the end of this step, you have fully transitioned your front end content into your React app. It is time to delete needless flask routes and files.
app.py: Look over flaskapp/app.py. Look carefully at the return statement in each route. If you have routes that deliver API responses or images, you still need those and should NOT delete them. However, you no longer need routes that render_template or redirect to routes that render_template. If you have both GET and POST routes for your professionals_add and services_add, and the GET routes render_template, you need the POST routes but not the GET routes. You no longer need routes that deliver CSS or JS. Delete all needless routes, and test to make sure your app still works. If you find it difficult to delete code, remember that you have saved your code in a prior git version and can go back to it any time.
Files: Look at the flaskapp/templates directory. You no longer need anything in flaskapp/templates and can delete the whole directory. Look at the flaskapp/static directory. You no longer need any CSS, JS, or front-end images in your static directory. Delete all needless files.
FlaskPatch: Look at reactapp/src. If you used the FlaskPatch component, you are no longer using it and no longer need it. Delete the import in App.jsx, then delete the file FlaskPatch.jsx. It was meant for the temporary purpose of showing your Flask pages as you transitioned to React.