Skip to content
This repository was archived by the owner on Dec 28, 2023. It is now read-only.

Commit c6f5f8a

Browse files
adbatistaAlessandro Dias Batista
authored andcommitted
add new dashboard page using hotwired
1 parent 81cc8df commit c6f5f8a

File tree

19 files changed

+290
-42
lines changed

19 files changed

+290
-42
lines changed

app/assets/stylesheets/_punches.scss

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ $header-text-color: #453b3b;
4747

4848
&.out {
4949
opacity: .3;
50+
cursor: default;
5051
}
5152

5253
&.today {
@@ -59,6 +60,8 @@ $header-text-color: #453b3b;
5960
.weekday-0, .weekday-6, .weekday-holiday {
6061
background: $flat-clouds;
6162
color: $flat-silver;
63+
cursor: default;
64+
6265
ul {
6366
color: $flat-pomegranate;
6467
}

app/controllers/punches_controller.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@ def create
3333
end
3434
end
3535

36+
def bulk_create
37+
punches_params = params.require(:punches_calendar_form).permit(:project_id, :from1, :from2, :to1, :to2, days: [])
38+
39+
@punches = Punches::CalendarForm.new(punches_params)
40+
@punches.validate
41+
@punches_of_day = current_user.punches.group_by(&:date)
42+
@current_month_by_weeks = (Date.current.beginning_of_month.beginning_of_week..Date.current.end_of_month.end_of_week).group_by do |date|
43+
date.strftime("%U")
44+
end
45+
46+
47+
render :calendar
48+
# @punch = Punch.new(punch_params)
49+
# @punch.user_id = current_user.id
50+
51+
# if @punch.save
52+
# redirect_to punches_path, notice: I18n.t(:notice, scope: "flash.actions.create", resource_name: "Punch")
53+
# else
54+
# flash_errors('create')
55+
# render :new
56+
# end
57+
end
58+
3659
def update
3760
@punch = scopped_punches.find params[:id]
3861
@punch.attributes = punch_params
@@ -45,6 +68,16 @@ def update
4568
end
4669
end
4770

71+
def calendar
72+
@selected_month = params[:month].present? && params[:year].present? ? "#{params[:year]}/#{params[:month]}/1".to_date : Date.current.beginning_of_month
73+
74+
@punches = Punches::CalendarForm.new
75+
@punches_of_day = current_user.punches.group_by(&:date)
76+
@current_month_by_weeks = (@selected_month.beginning_of_week..@selected_month.end_of_month.end_of_week).group_by do |date|
77+
date.strftime("%U")
78+
end
79+
end
80+
4881
def destroy
4982
punch = Punch.find(params[:id])
5083
punch.destroy

app/forms/punches/calendar_form.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class Punches::CalendarForm
2+
include ActiveModel::Model
3+
include ActiveModel::Attributes
4+
5+
attribute :days, :string
6+
attribute :project_id, :integer
7+
attribute :user_id, :integer
8+
attribute :from1, :string
9+
attribute :to1, :string
10+
attribute :from2, :string
11+
attribute :to2, :string
12+
13+
validates_presence_of :days, :project_id, :from1, :from2, :to1, :to2
14+
validate :all_days_valid?
15+
16+
17+
def initialize(params={})
18+
super
19+
@punches = days.to_s.split(",").flat_map do |day|
20+
[
21+
Punch.new(from_time: from1, to_time: to1, project_id: project_id, when_day: day),
22+
Punch.new(from_time: from2, to_time: to2, project_id: project_id, when_day: day)
23+
]
24+
end
25+
end
26+
27+
# def model_name
28+
# ActiveModel::Name.new("CalendarPunches")
29+
# end
30+
31+
# def save
32+
# return false unless valid?
33+
34+
# Punch.transaction do
35+
# @punches.each(&:save!)
36+
# end
37+
38+
# true
39+
# end
40+
41+
def all_days_valid?
42+
43+
end
44+
end

app/forms/punches_create_form.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@ class PunchesCreateForm
77
validate :punches_validations, :punch_on_same_day_validation
88

99
def initialize(user, punches)
10-
punches ||= []
11-
12-
@punches = punches.map do |punch_params|
10+
@punches = Array(punches).map do |punch_params|
1311
Punch.new(**punch_params, user: user)
1412
end
1513
end
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { Controller } from "@hotwired/stimulus"
2+
3+
export default class extends Controller {
4+
static classes = [ "selectDay" ]
5+
static targets = [ "selectedDays", "deactivatable" ]
6+
static values = { selectedDays: Array }
7+
8+
connect() {
9+
this.disableForm()
10+
}
11+
12+
selectDay({ currentTarget: dayElement, params: { day } }) {
13+
if (this.selectedDaysValue.some(element => element == day)) {
14+
dayElement.classList.remove(this.selectDayClass)
15+
this.selectedDaysValue = this.selectedDaysValue.filter(element => element != day)
16+
} else {
17+
dayElement.classList.add(this.selectDayClass)
18+
this.selectedDaysValue = [day, ...this.selectedDaysValue]
19+
}
20+
}
21+
22+
selectedDaysValueChanged() {
23+
this.selectedDaysTarget.value = this.selectedDaysValue
24+
25+
if (this.selectedDaysValue.length) {
26+
this.enableForm()
27+
} else {
28+
this.disableForm()
29+
}
30+
}
31+
32+
disableForm() {
33+
this.deactivatableTargets.forEach(element => element.disabled = true)
34+
}
35+
36+
enableForm() {
37+
this.deactivatableTargets.forEach(element => element.disabled = false)
38+
}
39+
}

app/javascript/packs/calendar.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as Turbo from "@hotwired/turbo"
2+
import { Application } from "@hotwired/stimulus"
3+
import { definitionsFromContext } from "@hotwired/stimulus-webpack-helpers"
4+
5+
window.Stimulus = Application.start()
6+
const context = require.context("../controllers", true, /\.js$/)
7+
Stimulus.load(definitionsFromContext(context))

app/javascript/packs/hello_react.jsx

Lines changed: 0 additions & 26 deletions
This file was deleted.

app/views/fields/boolean/_index.html.erb

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/views/fields/boolean/_show.html.erb

Lines changed: 0 additions & 1 deletion
This file was deleted.

app/views/fields/date_field/_form.html.erb

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)