From 74a26609c9d072d0fcc28a685cab51cf0f57c1d4 Mon Sep 17 00:00:00 2001 From: Khavin Shankar Date: Thu, 9 Dec 2021 17:15:21 +0530 Subject: [PATCH 1/4] refactor (Schedule): renamed all compents with prefix Schedule --- app/javascript/schedule/Schedule.res | 14 +++++++------- .../{Filter.res => Schedule__Filter.res} | 2 +- .../{Pagination.res => Schedule__Pagination.res} | 0 .../{Patients.res => Schedule__Patients.res} | 6 +++--- .../{Search.res => Schedule__Search.res} | 0 ...Patients.res => Schedule__SelectedPatients.res} | 0 .../components/{Sort.res => Schedule__Sort.res} | 0 ...Patient.res => Schedule__UnselectedPatient.res} | 0 8 files changed, 11 insertions(+), 11 deletions(-) rename app/javascript/schedule/components/{Filter.res => Schedule__Filter.res} (97%) rename app/javascript/schedule/components/{Pagination.res => Schedule__Pagination.res} (100%) rename app/javascript/schedule/components/{Patients.res => Schedule__Patients.res} (95%) rename app/javascript/schedule/components/{Search.res => Schedule__Search.res} (100%) rename app/javascript/schedule/components/{SelectedPatients.res => Schedule__SelectedPatients.res} (100%) rename app/javascript/schedule/components/{Sort.res => Schedule__Sort.res} (100%) rename app/javascript/schedule/components/{UnselectedPatient.res => Schedule__UnselectedPatient.res} (100%) diff --git a/app/javascript/schedule/Schedule.res b/app/javascript/schedule/Schedule.res index b052841c..67578503 100644 --- a/app/javascript/schedule/Schedule.res +++ b/app/javascript/schedule/Schedule.res @@ -10,10 +10,10 @@ let make = (~props: props) => { let (sortOption, setSortOption) = React.useState(_ => "next_visit") let (sortAscending, setSortAscending) = React.useState(_ => true) let (patients, updatePatients) = React.useReducer( - Patients.reducer, + Schedule__Patients.reducer, {unselectedPatients: props.patients, selectedPatients: []}, ) - let (filters, updateFilters) = React.useReducer(Filter.reducer, {procedures: [], wards: []}) + let (filters, updateFilters) = React.useReducer(Schedule__Filter.reducer, {procedures: [], wards: []}) React.useEffect4(() => { let procedure_filtered_patients = !(filters.procedures->length == 0) @@ -41,20 +41,20 @@ let make = (~props: props) => { let sorted_patients = filtered_patients->Schedule__Utils.jssort(sortOption, sortAscending) - Patients.SetUnSelectedPatients(sorted_patients)->updatePatients + Schedule__Patients.SetUnSelectedPatients(sorted_patients)->updatePatients None }, (searchTerm, sortOption, sortAscending, filters))
- - - + + Schedule__Utils.jsunion("procedures")} selectedFilters={filters} updateFilters />
- +
} diff --git a/app/javascript/schedule/components/Filter.res b/app/javascript/schedule/components/Schedule__Filter.res similarity index 97% rename from app/javascript/schedule/components/Filter.res rename to app/javascript/schedule/components/Schedule__Filter.res index 88069aa4..9b4912ff 100644 --- a/app/javascript/schedule/components/Filter.res +++ b/app/javascript/schedule/components/Schedule__Filter.res @@ -82,7 +82,7 @@ module FilterSection = {
{s(name ++ "(s)")}
- {searchbar ? :
} + {searchbar ? :
}
{options ->Belt.Array.map(option => diff --git a/app/javascript/schedule/components/Pagination.res b/app/javascript/schedule/components/Schedule__Pagination.res similarity index 100% rename from app/javascript/schedule/components/Pagination.res rename to app/javascript/schedule/components/Schedule__Pagination.res diff --git a/app/javascript/schedule/components/Patients.res b/app/javascript/schedule/components/Schedule__Patients.res similarity index 95% rename from app/javascript/schedule/components/Patients.res rename to app/javascript/schedule/components/Schedule__Patients.res index 7d4aeb3e..ad65f0bb 100644 --- a/app/javascript/schedule/components/Patients.res +++ b/app/javascript/schedule/components/Schedule__Patients.res @@ -43,20 +43,20 @@ let make = (~patients, ~updatePatients) => { upatients ->Js.Array2.slice(~start=(pageNumber - 1) * perPage, ~end_=pageNumber * perPage) ->Js.Array2.map(patient => - patient->SelectPatient->updatePatients} /> )
- patient->UnselectPatient->updatePatients} />
    {patientList->React.array}
- Date: Thu, 9 Dec 2021 17:28:35 +0530 Subject: [PATCH 2/4] refactor (Patients): created rescript boilerplate --- app/javascript/Patient/Patients.res | 9 +++++++++ app/javascript/packs/PatientsPack.res | 14 ++++++++++++++ app/views/patients/index.html.erb | 15 +++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 app/javascript/Patient/Patients.res create mode 100644 app/javascript/packs/PatientsPack.res diff --git a/app/javascript/Patient/Patients.res b/app/javascript/Patient/Patients.res new file mode 100644 index 00000000..cd4b6d12 --- /dev/null +++ b/app/javascript/Patient/Patients.res @@ -0,0 +1,9 @@ +let s = React.string + +@react.component +let make = () => { + +
+

{s("Patients")}

+
+} \ No newline at end of file diff --git a/app/javascript/packs/PatientsPack.res b/app/javascript/packs/PatientsPack.res new file mode 100644 index 00000000..20fb4e17 --- /dev/null +++ b/app/javascript/packs/PatientsPack.res @@ -0,0 +1,14 @@ +// type props = Patient__Types.props + +// let decode: Js.Json.t => props = json => { +// open Json.Decode +// { +// schedules: field("patients", array(Agenda__Types.decode), json), +// } +// } +// let props = DomUtils.parseJSONTag(~id="patients-data", ()) |> decode + +switch ReactDOM.querySelector(`#patients`) { +| Some(id) => ReactDOM.render(, id) +| None => () +} \ No newline at end of file diff --git a/app/views/patients/index.html.erb b/app/views/patients/index.html.erb index 5d0bf09c..eb94c8a7 100644 --- a/app/views/patients/index.html.erb +++ b/app/views/patients/index.html.erb @@ -1,3 +1,18 @@ +<%=content_for(:tail) do %> + <%= javascript_pack_tag 'PatientsPack', "data-turbo-track": "reload", nonce: true %> +<% end %> + + + +
+ + + +
From 3e19e332401ebb459f4d7ca5c35a417d5e031624 Mon Sep 17 00:00:00 2001 From: Khavin Shankar Date: Fri, 10 Dec 2021 21:11:01 +0530 Subject: [PATCH 3/4] refactor (Patients): setup props --- app/controllers/patients_controller.rb | 15 ++++++++++++-- app/javascript/Patient/Patients.res | 6 +++++- app/javascript/Patient/Patients__Types.res | 23 ++++++++++++++++++++++ app/javascript/packs/PatientsPack.res | 19 +++++++++--------- app/views/patients/_table-row.html.erb | 2 +- app/views/patients/index.html.erb | 2 +- 6 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 app/javascript/Patient/Patients__Types.res diff --git a/app/controllers/patients_controller.rb b/app/controllers/patients_controller.rb index e8c07f70..7feb346f 100644 --- a/app/controllers/patients_controller.rb +++ b/app/controllers/patients_controller.rb @@ -10,8 +10,19 @@ def index @facilities.unshift(["all", nil]) # filter and paginate - @patients = filter_patients(@search_text, @facility_id) - authorize Patient + # @patients = filter_patients(@search_text, @facility_id) + + @patients = Patient.all.map do |patient| + { + name: patient.full_name, + dob: patient.dob, + phone: patient.phone, + address: patient.address, + id: patient.id + } + end + puts @patients + # authorize Patient end # GET /patients/new diff --git a/app/javascript/Patient/Patients.res b/app/javascript/Patient/Patients.res index cd4b6d12..a2587b54 100644 --- a/app/javascript/Patient/Patients.res +++ b/app/javascript/Patient/Patients.res @@ -1,7 +1,11 @@ +type patient = Patients__Types.patient +type props = Patients__Types.props + let s = React.string @react.component -let make = () => { +let make = (~props: props) => { + Js.log(props)

{s("Patients")}

diff --git a/app/javascript/Patient/Patients__Types.res b/app/javascript/Patient/Patients__Types.res new file mode 100644 index 00000000..fa5e91cf --- /dev/null +++ b/app/javascript/Patient/Patients__Types.res @@ -0,0 +1,23 @@ +type patient = { + id: string, + name: string, + dob: Js.Date.t, + phone: string, + address: string, +} + +type patients = array + +type props = {patients: patients} + + +let decode = json => { + open Json.Decode + { + id: field("id", string, json), + name: field("name", string, json), + dob: field("dob", date, json), + phone: field("phone", string, json), + address: field("address", string, json) + } +} \ No newline at end of file diff --git a/app/javascript/packs/PatientsPack.res b/app/javascript/packs/PatientsPack.res index 20fb4e17..55b08493 100644 --- a/app/javascript/packs/PatientsPack.res +++ b/app/javascript/packs/PatientsPack.res @@ -1,14 +1,15 @@ -// type props = Patient__Types.props +type props = Patients__Types.props + +let decode: Js.Json.t => props = json => { + open Json.Decode + { + patients: field("patients", array(Patients__Types.decode), json), + } +} +let props = DomUtils.parseJSONTag(~id="patients-data", ()) |> decode -// let decode: Js.Json.t => props = json => { -// open Json.Decode -// { -// schedules: field("patients", array(Agenda__Types.decode), json), -// } -// } -// let props = DomUtils.parseJSONTag(~id="patients-data", ()) |> decode switch ReactDOM.querySelector(`#patients`) { -| Some(id) => ReactDOM.render(, id) +| Some(id) => ReactDOM.render(, id) | None => () } \ No newline at end of file diff --git a/app/views/patients/_table-row.html.erb b/app/views/patients/_table-row.html.erb index aa26c1d8..e0ef8212 100644 --- a/app/views/patients/_table-row.html.erb +++ b/app/views/patients/_table-row.html.erb @@ -1,6 +1,6 @@ - <%= patient.full_name %> + <%= patient.name %> diff --git a/app/views/patients/index.html.erb b/app/views/patients/index.html.erb index eb94c8a7..1b974628 100644 --- a/app/views/patients/index.html.erb +++ b/app/views/patients/index.html.erb @@ -81,7 +81,7 @@ <% @patients.each do |patient| %> - <%= render partial: 'table-row', locals: { patient: patient} %> + patient <% end %> From 87986d98fc7d9c2132fabefd56110c8156f78e3f Mon Sep 17 00:00:00 2001 From: Khavin Shankar Date: Fri, 10 Dec 2021 22:28:46 +0530 Subject: [PATCH 4/4] refactor (Patients): ui coverted to rescript --- app/controllers/patients_controller.rb | 30 +------ app/javascript/Patient/Patients.res | 24 +++++- app/javascript/Patient/components/Link.res | 15 ++++ .../components/Patients__PatientsTable.res | 84 +++++++++++++++++++ app/models/patient.rb | 12 +++ app/views/patients/index.html.erb | 84 +------------------ 6 files changed, 134 insertions(+), 115 deletions(-) create mode 100644 app/javascript/Patient/components/Link.res create mode 100644 app/javascript/Patient/components/Patients__PatientsTable.res diff --git a/app/controllers/patients_controller.rb b/app/controllers/patients_controller.rb index 7feb346f..d00f5628 100644 --- a/app/controllers/patients_controller.rb +++ b/app/controllers/patients_controller.rb @@ -3,26 +3,7 @@ class PatientsController < ApplicationController # GET /patients/ def index - @search_text = params.fetch(:search, "") - @facility_id = params.fetch(:facility, "") - - @facilities = policy_scope(Facility).map { |f| [f.name, f.id] } - @facilities.unshift(["all", nil]) - - # filter and paginate - # @patients = filter_patients(@search_text, @facility_id) - - @patients = Patient.all.map do |patient| - { - name: patient.full_name, - dob: patient.dob, - phone: patient.phone, - address: patient.address, - id: patient.id - } - end - puts @patients - # authorize Patient + @patients = Patient.get_formatted end # GET /patients/new @@ -113,13 +94,4 @@ def patient_params :facility_id, ) end - - def filter_patients(search_text, facility_id) - return policy_scope(Patient).where(facility_id: facility_id) if (facility_id != "") - - policy_scope(Patient).where( - "full_name ILIKE :search_text", - search_text: "%#{search_text}%", - ) - end end diff --git a/app/javascript/Patient/Patients.res b/app/javascript/Patient/Patients.res index a2587b54..94168cef 100644 --- a/app/javascript/Patient/Patients.res +++ b/app/javascript/Patient/Patients.res @@ -5,9 +5,27 @@ let s = React.string @react.component let make = (~props: props) => { - Js.log(props) -
-

{s("Patients")}

+
+
+
+
{s("Patients")}
+ +
+ +
+
+
+
+
+ +
+
+
+
} \ No newline at end of file diff --git a/app/javascript/Patient/components/Link.res b/app/javascript/Patient/components/Link.res new file mode 100644 index 00000000..db8984db --- /dev/null +++ b/app/javascript/Patient/components/Link.res @@ -0,0 +1,15 @@ +@val external window: {..} = "window" + +let s = React.string + +@react.component +let make = (~className: string, ~href: string, ~text: string) => { + { + event->ReactEvent.Mouse.stopPropagation + window["location"]["href"] = href + }}> + {s(text)} + +} \ No newline at end of file diff --git a/app/javascript/Patient/components/Patients__PatientsTable.res b/app/javascript/Patient/components/Patients__PatientsTable.res new file mode 100644 index 00000000..da062f07 --- /dev/null +++ b/app/javascript/Patient/components/Patients__PatientsTable.res @@ -0,0 +1,84 @@ +type patient = Patients__Types.patient +type patients = Patients__Types.patients + +let s = React.string + + +module Patient = { + @react.component + let make = (~patient: patient) => { + + + {s(patient.name)} + + + + {s(patient.dob->Js.Date.toDateString)} + + + + {s(patient.phone)} + + + + {s(patient.address)} + + + + + + + } +} + + +@react.component +let make = (~patients: patients) => { + + +
+ + + + + + + + + + + + { + patients->Js.Array2.map(patient => + + )->React.array + } + +
+ {s("Full Name")} + + {s("Date of Birth")} + + {s("Phone")} + + {s("Address")} + + {s("Actions")} +
+
+} \ No newline at end of file diff --git a/app/models/patient.rb b/app/models/patient.rb index ebc457f8..97361253 100644 --- a/app/models/patient.rb +++ b/app/models/patient.rb @@ -16,6 +16,18 @@ class Patient < ApplicationRecord very_poor: "Very Poor", } + def self.get_formatted + all.map do |patient| + { + name: patient.full_name, + dob: patient.dob, + phone: patient.phone, + address: patient.address, + id: patient.id + } + end + end + def add_users(user_ids) user_ids.each do |user_id| self.users << User.find(user_id) diff --git a/app/views/patients/index.html.erb b/app/views/patients/index.html.erb index 1b974628..afb48950 100644 --- a/app/views/patients/index.html.erb +++ b/app/views/patients/index.html.erb @@ -8,86 +8,4 @@ } -
- - - - -
-
-
-
Patients
- -
- <%= form_with url: "/patients", method: :get, class: "flex space-x-4 items-center" do |form| %> - <%= form.label "Filter By Facility" %> - <%= form.select :facility, @facilities, {selected: @facility_id}, {class: "border border-gray-300 rounded-md shadow-md w-32", onchange: "this.form.submit()"} %> - <% end %> - <%= link_to 'Create New Patient', new_patient_path, class: 'inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500' %> -
-
-
-
-
- <%= form_tag patients_path, method: :get do %> -
- <%= text_field_tag :search, @search_text,placeholder: "Search for Patient name", class: "block w-full bg-white border border-gray-300 rounded-md py-2 pl-3 text-sm placeholder-gray-500 focus:outline-none focus:text-gray-900 focus:placeholder-gray-400 focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" %> -
- -
-
- <% end %> -
-
- - - - - - - - - - - - <% @patients.each do |patient| %> - patient - <% end %> - -
- Full Name - - Date of Birth - - Phone - - Address - - Actions -
-
-
-
-
-
-
+
\ No newline at end of file