Skip to content

Commit

Permalink
Merge pull request #36 from Noelryn/department_sidebar
Browse files Browse the repository at this point in the history
 Department view in hospitals page
  • Loading branch information
Noelryn authored Nov 17, 2018
2 parents a30acea + a5f2bce commit fff1e07
Show file tree
Hide file tree
Showing 23 changed files with 201 additions and 34 deletions.
27 changes: 22 additions & 5 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
//= require_tree .

const sharedDeviseLinkHandler = (event) => {
if (event.target.classList.contains('shared_devise_link')){
if (event.target.classList.contains('shared_devise_link')) {
var modalElement = document.getElementById('modal')
modalElement.remove()
}
}
}

const closeModal = (modalElement) => {
modalElement.remove()
Expand All @@ -46,22 +46,39 @@ const hideMainContent = () => {
}

const displayTemplate = html => {
hideRightBar();
hideMainContent();
App.tempContent.innerHTML = html
}

const rightTemplate = html => {
App.rightContent.innerHTML = html
}

const displayModal = html => {
hideMainContent();
App.tempContent.innerHTML = html
hideRightBar()
closeModalListener('cancel_modal')
}

const hideRightBar = () => {
App.rightContent.innerHTML = ""
}

document.addEventListener('turbolinks:load', () => {
App.mainContent = document.getElementById('main_content')
App.tempContent = document.getElementById('temp_content')
App.rightContent = document.getElementById('rightbar')

let flashElement = document.getElementById('messages')
setTimeout(()=>{
flashElement.style.display = 'none'
}, 2000);
if (flashElement) {
setTimeout(() => {
flashElement.style.display = 'none'
}, 2000);
}

})



24 changes: 24 additions & 0 deletions app/controllers/departments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class DepartmentsController < ApplicationController
def create
department = Department.create(department_params)
if department.persisted?
hospital = Hospital.find(params[:department][:hospital_id])
html = render_to_string('hospitals/_list', formats: :html, layout: false, locals: { hospital: hospital })
render json: { message: 'Department has been added', html: html }
else
redirect_to hospitals_path, notice: 'Department already exists. Your department could not be saved'
end
end

def get_departments
hospital = Hospital.find_by_name(params['hospital'])
html = render_to_string('hospitals/_department_select', formats: :html, layout: false, locals: { hospital: hospital })
render json: { html: html }
end

private

def department_params
params.require(:department).permit(:name, :hospital_id)
end
end
4 changes: 4 additions & 0 deletions app/controllers/hospitals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def create
end
end

def show
@hospital = Hospital.find(params[:id])
end

def destroy
if Hospital.find(params[:id]).destroy
redirect_to hospitals_path, notice: 'Hospital was successfully removed.'
Expand Down
1 change: 1 addition & 0 deletions app/models/department.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
class Department < ApplicationRecord
belongs_to :hospital
validates_presence_of :name
end
3 changes: 2 additions & 1 deletion app/models/hospital.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Hospital < ApplicationRecord
has_many :jobs
has_many :jobs
belongs_to :region
has_many :departments
validates :name, uniqueness: true
end
5 changes: 5 additions & 0 deletions app/views/hospitals/_department_select.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.to-field-wrapper
.to-field-full-width
= label_tag 'job[department]', 'Department', class: "form-label"
= select_tag 'job[department]', options_for_select(hospital.departments.collect {|c| [ c.name ] }, { include_blank: true }), id: "select_department", class: "form-input-field"

3 changes: 2 additions & 1 deletion app/views/hospitals/_index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
.list-grid
.list-name= hospital.name
.list-button-area
= link_to "Remove", hospital_path(hospital, locale: I18n.locale), method: :delete, data: {confirm: 'Are you sure?'}, remote: true, class: "button"
= link_to "Show", hospital_path(hospital, locale: I18n.locale), class: "button text-center", remote: true
= link_to "Remove", hospital_path(hospital, locale: I18n.locale), method: :delete, data: {confirm: 'Are you sure?'}, remote: true, class: "button text-center"
= form_with url: hospitals_path, scope: :hospital do |f|
.list-grid
.list-name
Expand Down
5 changes: 5 additions & 0 deletions app/views/hospitals/_list.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%ul
- hospital.departments.each do |department|
.list-grid
%li.list-name
= department.name
13 changes: 13 additions & 0 deletions app/views/hospitals/_show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#rightbar
#error_messages
.list-card
.form-header Departments
.list-item.flex#list
= render partial: 'list', locals: {hospital: @hospital}
= form_with url: departments_path, scope: :department, id: :create_department do |f|
= f.hidden_field :hospital_id, value: @hospital.id
.list-grid
.list-name
= f.text_field :name, class: "form-input-field"
.list-button-area
= f.submit value: "Add Department", class: "button text-center"
22 changes: 22 additions & 0 deletions app/views/hospitals/show.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

var templateContent = '<%= escape_javascript(render partial: 'show') %>'
rightTemplate(templateContent)

var departmentForm = document.getElementById('create_department')
console.log('show panel')

departmentForm.addEventListener('ajax:success', (event) => {
templateContent = event.detail[0].html
document.getElementById('list').innerHTML = templateContent;
var errorMessageDisplayElement = document.getElementById('messages')
var message = event.detail[0].message
errorMessageDisplayElement.innerHTML = message
})

departmentForm.addEventListener('ajax:error', (event) => {
console.log('added listener for error...')

var errorMessageDisplayElement = document.getElementById('error_messages')
var message = event.detail[0].message
errorMessageDisplayElement.innerHTML = message
})
7 changes: 2 additions & 5 deletions app/views/jobs/_new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@
.to-field-wrapper
.to-field-full-width
= form.label :hospital, class: "form-label"
= form.select :hospital, Hospital.all.collect {|c| [ c.name ] }, { include_blank: true }, id: "Hospital", class: "form-input-field"
.to-field-wrapper
.to-field-full-width
= form.label :department, class: "form-label"
= form.select :department, Department.all.collect {|c| [ c.name ] }, { include_blank: true }, class: "form-input-field"
= form.select :hospital, Hospital.all.collect {|c| [ c.name ] }, { include_blank: true }, id: "select_hospital", class: "form-input-field"
#select_department
.to-field-wrapper
.to-field-full-width
= form.label :license, class: "form-label"
Expand Down
14 changes: 14 additions & 0 deletions app/views/jobs/new.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,24 @@ App.tempContent.innerHTML = modalContent
closeModalListener('cancel_modal')

var form = document.getElementById('create_job')
var hospitalSelect = document.getElementById('select_hospital')
var departmentSelect = document.getElementById('select_department')

form.addEventListener('ajax:error', (event) => {
let errorMessageDisplayElement = document.getElementById('error_messages')
let message = event.detail[0].message
errorMessageDisplayElement.innerHTML = message
})

hospitalSelect.addEventListener('change', () => {
var params = `hospital=${hospitalSelect.options[hospitalSelect.selectedIndex].value}`
Rails.ajax({
type: 'GET',
url: '<%= get_departments_path %>',
data: params,
success: (response) => {
departmentSelect.innerHTML = response.html;
}

})
})
9 changes: 5 additions & 4 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@
= render partial: 'partials/header'
.grid-container.bg-grey-darker
= render partial: 'partials/sidebar'
= render partial: 'partials/rightbar'
#content
- if notice || alert
#messages
= notice
= alert
#temp_content
#main_content
- if notice || alert
#messages
= notice
= alert
= yield
2 changes: 2 additions & 0 deletions app/views/partials/_rightbar.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#rightbar
= content_for :rightbar
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
}
root controller: :jobs, action: :index
resources :jobs, only: [:new, :create, :destroy]
resources :departments, only: [:create]
resources :invitations, only: [:create, :new]
resources :staffcos, only: [:create, :index, :new, :destroy]
resources :hospitals, only: [:index, :create, :new, :destroy]
resources :hospitals, only: [:index, :create, :new, :show, :destroy]
resources :regions, only: [:create, :new]
get :get_departments, controller: :departments, action: :get_departments
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AssociationBetweenHospitalsAndDepartments < ActiveRecord::Migration[5.2]
def change
add_reference :departments, :hospital, foreign_key: true
end
end
7 changes: 5 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_11_13_150059) do
ActiveRecord::Schema.define(version: 2018_11_16_141742) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -25,6 +25,8 @@
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.bigint "hospital_id"
t.index ["hospital_id"], name: "index_departments_on_hospital_id"
end

create_table "hospitals", force: :cascade do |t|
Expand All @@ -46,6 +48,7 @@
t.string "department"
t.string "license"
t.string "care_type"
t.bigint "hospital_id"
t.text "requirements"
t.string "other_requirements"
t.text "description"
Expand All @@ -61,7 +64,6 @@
t.string "other_budget"
t.text "other_comment"
t.string "reference_number"
t.bigint "hospital_id"
t.bigint "region_id"
t.index ["hospital_id"], name: "index_jobs_on_hospital_id"
t.index ["region_id"], name: "index_jobs_on_region_id"
Expand Down Expand Up @@ -129,6 +131,7 @@
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end

add_foreign_key "departments", "hospitals"
add_foreign_key "hospitals", "regions"
add_foreign_key "jobs", "hospitals"
add_foreign_key "jobs", "regions"
Expand Down
24 changes: 15 additions & 9 deletions features/rsc_can_create_new_job.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,47 @@ Feature: Rsc can add new job

Background: Landing page, clicking new and directed to the form page
Given the following licenses exists
| name |
| name |
| Kirurgi |

Given the following hospitals exists
| name |
| stockholm sjukhuset |

Given the following profession exists
| name |
| Doctor |
| name |
| Doctor |

Given the following department exists
| name |
| Surgery |
| name | hospital |
| Surgery | stockholm sjukhuset |

Given the following caretype exists
| name |
| Homecare |
| name |
| Homecare |

Given the following other requirement exists
| name |
| Drivers License |



Given I am on the landing page
And I click on "Add New Job"

Scenario: RSC creates a new job
Then I should see "Create New Job"
And I fill in the job form with "valid" information
And I fill in the job form with "valid" information
And I select "stockholm sjukhuset" from "select_hospital"
And I wait 3 seconds
And I select "Surgery" from "select_department"
And I click on "Submit"
Then I should see "The job was successfully created"
And I should see "Doctor"

Scenario: RSC fills out form incorrectly
And I fill in the job form with "invalid" information
And I select "stockholm sjukhuset" from "select_hospital"
And I select "Surgery" from "select_department"
And I click on "Submit"
Then I should see "Profession can't be blank,License can't be blank,Scope can't be blank"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Feature: Posting a job to staffing companies with a webhook
| Doctor |

And the following department exists
| name |
| Surgery |
| name | hospital |
| Surgery | stockholm sjukhuset |

And the following caretype exists
| name |
Expand All @@ -47,6 +47,8 @@ Feature: Posting a job to staffing companies with a webhook
Scenario: RSC posts a job to staffing companies using a webhook
Then I should see "Create New Job"
And I fill in the job form with "valid" information
And I select "stockholm sjukhuset" from "select_hospital"
And I select "Surgery" from "select_department"
And I click on "Submit"
And I should see "The job was successfully created"
And I wait 2 seconds
Expand Down
Loading

0 comments on commit fff1e07

Please sign in to comment.