Skip to content

Commit

Permalink
Create free slots
Browse files Browse the repository at this point in the history
  • Loading branch information
aziflaj committed Sep 4, 2019
1 parent 6d6f210 commit d53be48
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 8 deletions.
13 changes: 11 additions & 2 deletions app/assets/javascripts/dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,20 @@ document.addEventListener('DOMContentLoaded', function () {

const fabs = document.querySelectorAll('.fixed-action-btn');
M.FloatingActionButton.init(fabs);

const modals = document.querySelectorAll('.modal');
M.Modal.init(modals);

const datepickers = document.querySelectorAll('.datepicker');
M.Datepicker.init(datepickers);

const timepickers = document.querySelectorAll('.timepicker');
M.Timepicker.init(timepickers);
});

const loadData = async () => {
const free_slots_response = await fetch('/free_slots');
const upcoming_meetings_response = await fetch('/upcoming_meetings');
const free_slots_response = await fetch('/slots/free');
const upcoming_meetings_response = await fetch('/slots/upcoming');

window.slots = {
upcoming: await upcoming_meetings_response.json(),
Expand Down
18 changes: 17 additions & 1 deletion app/controllers/slots_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
class SlotsController < ApplicationController
respond_to :json
protect_from_forgery with: :null_session

def create
slot = Slots::Builder.new(current_user, params).call

if slot.save
redirect_to dashboard_path
else
redirect_to dashboard_path, alert: slot.errors.full_messages.join("\n")
end
end

def free
slots = FreeSlotsForUser::Query.new(current_user).call
Expand All @@ -12,4 +22,10 @@ def upcoming

render json: slots
end

private

def slot_params

end
end
4 changes: 3 additions & 1 deletion app/queries/free_slots_for_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ def initialize(user)
end

def call
Slot.where(user: user, guest_email: [nil, ''])
Slot
.where(user: user, guest_email: [nil, ''])
.order(:scheduled_at)
end

private
Expand Down
5 changes: 4 additions & 1 deletion app/queries/upcoming_slots_for_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ def initialize(user)
end

def call
Slot.where(user: user).where.not(guest_email: [nil, ''])
Slot
.where(user: user)
.where.not(guest_email: [nil, ''])
.order(:scheduled_at)
end

private
Expand Down
30 changes: 30 additions & 0 deletions app/services/slots/builder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Slots
class Builder
def initialize(user, params)
@user = user
@params = params
end

def call
Slot.new(slot_params)
end

private

attr_reader :user, :params

def slot_params
{ user: user,
scheduled_at: scheduled_at,
notes: safe_params[:notes] }
end

def safe_params
params.require(:slot).permit(:scheduled_at_date, :scheduled_at_time, :notes)
end

def scheduled_at
DateTime.parse("#{safe_params[:scheduled_at_date]} #{safe_params[:scheduled_at_time]}")
end
end
end
4 changes: 3 additions & 1 deletion app/views/dashboard/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
</div>

<div class="fixed-action-btn">
<a class="btn-floating btn-large">
<a class="btn-floating btn-large modal-trigger" data-target="slot-creation-modal">
<i class="large material-icons">add</i>
</a>
</div>

<%= render 'slots/form' %>
</div>
25 changes: 25 additions & 0 deletions app/views/slots/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<div id="slot-creation-modal" class="modal">
<%= form_with model: Slot.new, html: { class: 'new-slot' } do |f| %>
<div class="modal-content">
<h4>Create a new slot</h4>
<div class="row">
<div class="input-field col s12">
<i class="material-icons prefix">date_range</i>
<%= f.text_field :scheduled_at_date, class: 'datepicker', placeholder: 'Scheduled date' %>
</div>

<div class="input-field col s12">
<i class="material-icons prefix">access_time</i>
<%= f.text_field :scheduled_at_time, class: 'timepicker', placeholder: 'Scheduled time' %>
</div>
<div class="input-field col s12">
<i class="material-icons prefix">event_note</i>
<%= f.text_area :notes, placeholder: 'Notes', rows: 10 %>
</div>
</div>
</div>
<div class="modal-footer">
<%= f.submit 'Create slot', class: 'modal-close waves-effect waves-green btn-flat' %>
</div>
<% end %>
</div>
8 changes: 6 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

get 'dashboard', to: 'dashboard#index'

get 'free_slots', to: 'slots#free'
get 'upcoming_meetings', to: 'slots#upcoming'
resources :slots, only: :create do
collection do
get 'free'
get 'upcoming'
end
end
end

0 comments on commit d53be48

Please sign in to comment.