aleeusgr / hs-clojure

Other
0 stars 0 forks source link

Implement UI components #4

Closed aleeusgr closed 2 months ago

aleeusgr commented 3 months ago

To check if your form works, you can follow these steps:

1. Start your Clojure API: Run your Clojure API using lein run or java -jar your-jar-file.jar (depending on how you're running your API).

2. Open your web page: Open a web browser and navigate to http://localhost:3000 (or the URL where your API is running). You should see your HTML form.

3. Fill out the form: Fill out the form with some sample data, such as:

4. Submit the form: Click the "Add Patient" button to submit the form.

5. Check the API response: Check the API response in the browser's developer tools (e.g., Chrome DevTools) or using a tool like Postman.

6. Verify the patient was added: Check your database or API logs to verify that the patient was successfully added.

7. Test error scenarios: Test error scenarios, such as:

By following these steps, you can verify that your form is working correctly and your API is processing the form data as expected.

aleeusgr commented 3 months ago

Here's an example of how you can add a route and a UI component to add a patient:

Add a route to add a patient

In your routes.clj file, add the following route:

(defroutes patient-routes
  (POST "/patients" request (add-patient-handler request)))

(defn add-patient-handler [request]
  (let [params (:params request)
        name (:name params)
        sex (:sex params)
        date-of-birth (:date-of-birth params)
        address (:address params)
        social-security-number (:social-security-number params)]
    (try
      (add-patient name sex date-of-birth address social-security-number)
      (response "Patient added successfully!")
      (catch Exception e
        (response (str "Error adding patient: " (.getMessage e)) 500)))))

This route listens for a POST request to /patients and calls the add-patient-handler function. The handler extracts the parameters from the request, calls the add-patient function, and returns a response indicating whether the patient was added successfully or not.

Add a UI component to add a patient

Create a new file add_patient.html in your resources/public directory:

<!DOCTYPE html>
<html>
  <head>
    <title>Add Patient</title>
  </head>
  <body>
    <h1>Add Patient</h1>
    <form action="/patients" method="post">
      <label for="name">Name:</label>
      <input type="text" id="name" name="name"><br><br>
      <label for="sex">Sex:</label>
      <input type="text" id="sex" name="sex"><br><br>
      <label for="date-of-birth">Date of Birth:</label>
      <input type="date" id="date-of-birth" name="date-of-birth"><br><br>
      <label for="address">Address:</label>
      <input type="text" id="address" name="address"><br><br>
      <label for="social-security-number">Social Security Number:</label>
      <input type="text" id="social-security-number" name="social-security-number"><br><br>
      <input type="submit" value="Add Patient">
    </form>
  </body>
</html>

This is a simple HTML form that allows the user to input the patient's details. When the form is submitted, it sends a POST request to /patients with the input data.

Update your handler.clj file

Add a route to serve the add_patient.html file:

(defroutes app-routes
  ...
  (GET "/add-patient" [] (resource-response "add_patient.html" {:root "public"}))
  ...)

This route serves the add_patient.html file when the user navigates to /add-patient.

That's it! Now you should be able to add a patient by navigating to /add-patient in your browser, filling out the form, and submitting it.

aleeusgr commented 3 months ago

To add a UI component to get all patients, you'll need to create a web interface that interacts with your CRUD API. One way to do this is by using a ClojureScript library like Reagent or Rum, which allow you to create reactive UI components.

Here's an example of how you could create a simple UI component using Reagent to display all patients:

First, add the necessary dependencies to your project.clj file:

(defproject hs-clojure "0.1.0-SNAPSHOT"
  ...
  :dependencies [[org.clojure/clojure "1.10.0"]
                 [org.clojure/clojurescript "1.10.520"]
                 [reagent "0.8.1"]
                 ...])

Create a new namespace for your UI component:

; src/cljs/hs_clojure/ui.cljs
(ns hs-clojure.ui
  (:require [reagent.core :as r]
            [cljs-http.client :as http]
            [cljs.core.async :refer [<! >!]])
  (:import [goog Uri]))

(defonce patients (r/atom []))

(defn get-all-patients []
  (http/get "/api/patients"
             {:handler (fn [response]
                         (reset! patients response))}))

(defn patient-list []
  [:ul
   (for [patient @patients]
     ^{:key patient} [:li (:name patient)])])

(defn main-panel []
  [:div
   [:h1 "Patients"]
   [patient-list]
   [:button {:on-click get-all-patients} "Get All Patients"]])

(defn ^:export main []
  (r/render [main-panel]
            (.getElementById js/document "app")))

Create an HTML file to render the UI component:

<!-- resources/public/index.html -->
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>HS Clojure</title>
  </head>
  <body>
    <div id="app"></div>
    <script src="js/main.js"></script>
  </body>
</html>

Update your handler.clj file to include the API endpoint for getting all patients:

; src/hs_clojure/handler.clj
(ns hs-clojure.handler
  ...
  (:require [hs-clojure.patients :as patients]))

(defroutes app-routes
  ...
  (GET "/api/patients" [] (patients/get-all-patients))
  ...)

Start your ClojureScript compiler and web server:

lein cljsbuild once
lein ring server

Open your web browser and navigate to http://localhost:3000. You should see a simple UI component that displays a list of patients when you click the "Get All Patients" button.

Note that this is just a basic example to get you started. You'll likely want to add more features, error handling, and styling to your UI component.

aleeusgr commented 3 months ago

To add a Reagent UI to your project, you'll need to create a new namespace for your UI components and use Reagent's atom and render functions to create a reactive UI.

Here's a step-by-step guide to get you started:

1. Add Reagent to your project

In your project.clj file, add the following dependency:

[reagent "0.10.0"]

2. Create a new namespace for your UI components

Create a new file src/hs_clojure/ui.cljs with the following content:

(ns hs-clojure.ui
  (:require [reagent.core :as r]
            [reagent.dom :as rd]))

3. Create a UI component

In the hs_clojure.ui namespace, create a simple UI component, such as a patient list:

(defn patient-list []
  (let [patients (r/atom [])]
    (fn []
      [:div
       [:h1 "Patients"]
       [:ul
        (for [patient @patients]
          [:li (:name patient)])]
       [:button {:on-click #(fetch-patients!)} "Fetch patients"]]))

(defn fetch-patients! []
  (GET "/api/patients" {:handler (fn [response]
                                  (reset! patients response))}))

This component uses an atom patients to store the patient data and a fetch-patients! function to retrieve the data from your API.

4. Create a Reagent app

Create a new file src/hs_clojure/app.cljs with the following content:

(ns hs-clojure.app
  (:require [hs-clojure.ui :as ui]
            [reagent.core :as r]))

(defn app []
  [ui/patient-list])

(r/render [app] (.getElementById js/document "app"))

This file defines a Reagent app that renders the patient-list component.

5. Update your HTML file

Create a new file resources/public/index.html with the following content:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>HS Clojure</title>
  </head>
  <body>
    <div id="app"></div>
    <script src="js/compiled/app.js"></script>
  </body>
</html>

This file sets up an HTML page with a div element that will be used to render the Reagent app.

6. Compile your ClojureScript code

Add the following configuration to your project.clj file:

:cljsbuild
{:builds
 {:app {:source-paths ["src/hs_clojure"]
        :compiler {:output-to "resources/public/js/compiled/app.js"
                   :optimizations :advanced
                   :pretty-print false}}}}

This configuration tells ClojureScript to compile your app.cljs file and output the compiled JavaScript code to resources/public/js/compiled/app.js.

7. Run your application

Run lein cljsbuild once to compile your ClojureScript code. Then, start your Ring server with lein ring server. Open your web browser and navigate to http://localhost:3000 to see your Reagent UI in action!

This is just a basic example to get you started. You'll need to modify the UI components and API endpoints to fit your specific requirements. requires #9

Clojure/ClojureScript