Skip to content

Commit

Permalink
work week updating is sorta functional
Browse files Browse the repository at this point in the history
  • Loading branch information
fermion committed Dec 19, 2023
1 parent 3352f7a commit 75a0fe4
Show file tree
Hide file tree
Showing 14 changed files with 100 additions and 16 deletions.
14 changes: 10 additions & 4 deletions app/components/work_week_component.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<td data-controller="work-week" class="whitespace-nowrap px-2 py-2 text-sm text-gray-500">
<%= work_week_form do |form| %>
<%= form.hidden_field :assignment_id %>
<% if render_td? %>
<td data-controller="work-week" class="whitespace-nowrap px-2 py-2 text-sm text-gray-500">
<% end %>
<%= tag.turbo_frame id: turbo_frame_id do %>
<%= form_for @work_week, url: form_path, data: { work_week_target: "form" } do |form| %>
<%= hidden_field_tag :assignment_id, @assignment.id %>
<%= form.hidden_field :cweek %>
<%= form.hidden_field :year %>
<%= form.hidden_field :beginning_of_week %>
Expand All @@ -13,4 +16,7 @@
style: "#{render_actual_hours? ? '' : 'visibility: hidden;'}",
data: { action: "blur->work-week#submit", work_week_target: "actualInput" } %>
<% end %>
</td>
<% end %>
<% if render_td? %>
</td>
<% end %>
28 changes: 25 additions & 3 deletions app/components/work_week_component.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
# frozen_string_literal: true

class WorkWeekComponent < ViewComponent::Base
def initialize(work_week: new_work_week, assignment:, work_week_beginning_of_week:, beginning_of_week:)
include Turbo::FramesHelper

def initialize(work_week: nil, assignment:, work_week_beginning_of_week:, beginning_of_week:, render_td: true)
@assignment = assignment
@work_week_beginning_of_week = work_week_beginning_of_week
@beginning_of_week = beginning_of_week
@work_week = work_week
@render_td = render_td
@work_week = work_week || new_work_week
end

def render_td?
@render_td
end

def actual_hours
Expand All @@ -17,7 +24,7 @@ def estimated_hours
end

def work_week_form(&block)
form_for @work_week, data: { work_week_target: "form" } do |form|
form_for @work_week, url: form_path, data: { work_week_target: "form" } do |form|
yield(form)
end
end
Expand All @@ -26,12 +33,27 @@ def render_actual_hours?
@work_week_beginning_of_week <= @beginning_of_week
end

def turbo_frame_id
@work_week.turbo_frame_id
end

def form_path
if @work_week.persisted?
staff_plans_work_week_path(@work_week)
else
staff_plans_work_weeks_path
end
end

private



def new_work_week
date = Time.at(@work_week_beginning_of_week.to_i).to_date

@assignment.work_weeks.new(
assignment: @assignment,
beginning_of_week: @work_week_beginning_of_week,
cweek: date.cweek,
year: date.year
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class StaffPlan::UsersController < ApplicationController
class StaffPlans::UsersController < ApplicationController

def show
@viewing_user = params[:id].blank? ? current_user : current_company.users.find(params[:id])
Expand Down
24 changes: 24 additions & 0 deletions app/controllers/staff_plans/work_weeks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class StaffPlans::WorkWeeksController < ApplicationController
def create
@work_week = assignment.work_weeks.create(create_work_week_params)
end

def update
@work_week = assignment.work_weeks.find(params[:id])
@work_week.update(create_work_week_params)
end

private

def create_work_week_params
params.require(:work_week).permit(:estimated_hours, :actual_hours, :cweek, :year, :beginning_of_week)
end

def edit_work_week_params
params.require(:work_week).permit(:estimated_hours, :actual_hours, :cweek, :year, :beginning_of_week)
end

def assignment
current_user.assignments.find(params[:assignment_id])
end
end
2 changes: 2 additions & 0 deletions app/helpers/staff_plan/work_weeks_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module StaffPlan::WorkWeeksHelper
end
2 changes: 1 addition & 1 deletion app/javascript/controllers/work_week_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ export default class extends Controller {
}

submit() {
this.formTarget.submit()
this.formTarget.requestSubmit()
}
}
4 changes: 2 additions & 2 deletions app/lib/queries/user_staff_plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(user:, company:, beginning_of_week: Date.today.beginning_of_week)
@assignments = user.
assignments.
where(project: company.projects.active).
includes(:work_weeks, project: :client).
includes(work_weeks: :assignment, project: :client).
order('clients.name')
end

Expand All @@ -25,7 +25,7 @@ def work_weeks
end

def work_weeks_for(project:)
work_weeks.select { |work_week| work_week.project == project }
work_weeks.select { |work_week| work_week.assignment.project_id == project.id }
end

def clients
Expand Down
5 changes: 4 additions & 1 deletion app/models/work_week.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ class WorkWeek < ApplicationRecord

validates :assignment_id, presence: true, uniqueness: { scope: :beginning_of_week }
validates :cweek, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 1, less_than_or_equal_to: 53 }
validates :year, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 2021, less_than_or_equal_to: 2023 }
validates :year, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 2000, less_than_or_equal_to: 2200 }
validates :beginning_of_week, presence: true, numericality: { only_integer: true }
validates :estimated_hours, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 24 }
validates :actual_hours, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0, less_than_or_equal_to: 24 }

def turbo_frame_id
"work_week|a:#{assignment.id}|u:#{assignment.user.id}|bow:#{beginning_of_week}|cweek:#{cweek}|year:#{year}"
end
end
4 changes: 2 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<div class="hidden md:block">
<div class="ml-10 flex items-baseline space-x-4">
<!-- Current: "bg-gray-900 text-white", Default: "text-gray-300 hover:bg-gray-700 hover:text-white" -->
<%= header_link_to "My StaffPlan", staff_plan_path(current_user) %>
<%= header_link_to "My StaffPlan", staff_plans_path %>
<a href="#" class="text-gray-300 hover:bg-gray-700 hover:text-white rounded-md px-3 py-2 text-sm font-medium">Clients</a>
<a href="#" class="text-gray-300 hover:bg-gray-700 hover:text-white rounded-md px-3 py-2 text-sm font-medium">Projects</a>
<a href="#" class="text-gray-300 hover:bg-gray-700 hover:text-white rounded-md px-3 py-2 text-sm font-medium">People</a>
Expand Down Expand Up @@ -99,7 +99,7 @@
<!-- Mobile menu, show/hide based on menu state. -->
<div class="border-b border-gray-700 md:hidden hidden" id="mobile-menu">
<div class="space-y-1 px-2 py-3 sm:px-3">
<%= mobile_header_link_to "My StaffPlan", staff_plan_path(current_user) %>
<%= mobile_header_link_to "My StaffPlan", staff_plans_path %>
<a href="#" class="text-gray-300 hover:bg-gray-700 hover:text-white block rounded-md px-3 py-2 text-base font-medium">Clients</a>
<a href="#" class="text-gray-300 hover:bg-gray-700 hover:text-white block rounded-md px-3 py-2 text-base font-medium">Projects</a>
<a href="#" class="text-gray-300 hover:bg-gray-700 hover:text-white block rounded-md px-3 py-2 text-base font-medium">People</a>
Expand Down
File renamed without changes.
11 changes: 11 additions & 0 deletions app/views/staff_plans/work_weeks/create.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<% # currently assuming happy path. Need to handle/render validation errors. %>
<%= turbo_stream.update(
@work_week.turbo_frame_id,
render(WorkWeekComponent.new(
work_week: @work_week,
assignment: @work_week.assignment,
work_week_beginning_of_week: @work_week.beginning_of_week,
beginning_of_week: @work_week.beginning_of_week,
render_td: false
))
) %>
11 changes: 11 additions & 0 deletions app/views/staff_plans/work_weeks/update.turbo_stream.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<% # currently assuming happy path. Need to handle/render validation errors. %>
<%= turbo_stream.update(
@work_week.turbo_frame_id,
render(WorkWeekComponent.new(
work_week: @work_week,
assignment: @work_week.assignment,
work_week_beginning_of_week: @work_week.beginning_of_week,
beginning_of_week: @work_week.beginning_of_week,
render_td: false
))
) %>
1 change: 1 addition & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
end

# Settings specified here will take precedence over those in config/application.rb.
config.view_component.capture_compatibility_patch_enabled = true

# In the development environment your application's code is reloaded any time
# it changes. This slows down response time but is perfect for development
Expand Down
8 changes: 6 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@

resource :dashboard, only: [:show], controller: "dashboard"

resources :staff_plans, only: [:show], controller: "staff_plan/users"
get '/staff_plan', to: "staff_plan/users#show"
namespace :staff_plans do
resources :users, only: [:show]
resources :work_weeks, only: [:create, :update]
end

get '/staff_plans', to: "staff_plans/users#show"

root "dashboard#show"
end

0 comments on commit 75a0fe4

Please sign in to comment.