avo 2.9.1.pre2 → 2.9.1.pre3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of avo might be problematic. Click here for more details.

@@ -1,15 +1,18 @@
1
1
  <%= edit_field_wrapper field: @field, index: @index, form: @form, resource: @resource, displayed_in_modal: @displayed_in_modal do %>
2
- <div data-controller="date-field">
2
+ <%= content_tag :div, data: {
3
+ controller: "date-field",
4
+ date_field_view_value: @view,
5
+ date_field_enable_time_value: true,
6
+ date_field_picker_format_value: @field.picker_format,
7
+ date_field_first_day_of_week_value: @field.first_day_of_week,
8
+ date_field_time24_hr_value: @field.time_24hr,
9
+ date_field_timezone_value: @field.timezone,
10
+ } do %>
3
11
  <%= @form.datetime_field @field.id,
12
+ value: @field.edit_formatted_value,
4
13
  class: classes("w-full"),
5
14
  data: {
6
15
  'date-field-target': 'input',
7
- 'first-day-of-week': @field.first_day_of_week,
8
- 'picker-format': @field.picker_format,
9
- 'enable-time': true,
10
- time24hr: @field.time_24hr,
11
- timezone: @field.timezone,
12
- format: @field.format,
13
16
  placeholder: @field.placeholder,
14
17
  relative: @field.relative,
15
18
  **@field.get_html(:data, view: view, element: :input)
@@ -18,5 +21,5 @@
18
21
  placeholder: @field.placeholder,
19
22
  style: @field.get_html(:style, view: view, element: :input)
20
23
  %>
21
- </div>
24
+ <% end %>
22
25
  <% end %>
@@ -1,3 +1,11 @@
1
1
  <%= index_field_wrapper field: @field, resource: @resource do %>
2
- <%= @field.formatted_value %>
2
+ <%= content_tag :div, data: {
3
+ controller: "date-field",
4
+ date_field_view_value: @view,
5
+ date_field_format_value: @field.format,
6
+ date_field_timezone_value: @field.timezone,
7
+ date_field_picker_format_value: @field.picker_format,
8
+ } do %>
9
+ <%= @field.formatted_value %>
10
+ <% end %>
3
11
  <% end %>
@@ -1,3 +1,11 @@
1
1
  <%= show_field_wrapper field: @field, resource: @resource, index: @index do %>
2
- <%= @field.formatted_value %>
2
+ <%= content_tag :div, data: {
3
+ controller: "date-field",
4
+ date_field_view_value: @view,
5
+ date_field_format_value: @field.format,
6
+ date_field_timezone_value: @field.timezone,
7
+ date_field_picker_format_value: @field.picker_format,
8
+ } do %>
9
+ <%= @field.formatted_value %>
10
+ <% end %>
3
11
  <% end %>
@@ -32,7 +32,7 @@ class Avo::Views::ResourceEditComponent < Avo::ResourceComponent
32
32
  # The save button is dependent on the edit? policy method.
33
33
  # The update? method should be called only when the user clicks the Save button so the developer gets access to the params from the form.
34
34
  def can_see_the_save_button?
35
- @resource.authorization.authorize_action @view, raise_exception: false
35
+ @resource.authorization.authorize_action :edit, raise_exception: false
36
36
  end
37
37
 
38
38
  private
@@ -2,8 +2,6 @@ import { Controller } from '@hotwired/stimulus'
2
2
  import { DateTime } from 'luxon'
3
3
  import flatpickr from 'flatpickr'
4
4
 
5
- import { castBoolean } from '../../helpers/cast_boolean'
6
-
7
5
  // Get the DateTime with the TZ offset applied.
8
6
  function universalTimestamp(timestampStr) {
9
7
  return new Date(new Date(timestampStr).getTime() + (new Date(timestampStr).getTimezoneOffset() * 60 * 1000))
@@ -12,50 +10,99 @@ function universalTimestamp(timestampStr) {
12
10
  export default class extends Controller {
13
11
  static targets = ['input']
14
12
 
13
+ static values = {
14
+ view: String,
15
+ timezone: String,
16
+ format: String,
17
+ enableTime: Boolean,
18
+ pickerFormat: String,
19
+ firstDayOfWeek: Number,
20
+ time24Hr: Boolean,
21
+ }
22
+
23
+ get browserZone() {
24
+ const time = DateTime.local()
25
+
26
+ return time.zoneName
27
+ }
28
+
29
+ get initialValue() {
30
+ if (this.isOnShow || this.isOnIndex) {
31
+ return this.context.element.innerText
32
+ } if (this.isOnEdit) {
33
+ return this.inputTarget.value
34
+ }
35
+
36
+ return null
37
+ }
38
+
39
+ get isOnIndex() {
40
+ return this.viewValue === 'index'
41
+ }
42
+
43
+ get isOnEdit() {
44
+ return this.viewValue === 'edit'
45
+ }
46
+
47
+ get isOnShow() {
48
+ return this.viewValue === 'show'
49
+ }
50
+
51
+ // Parse the time as if it were UTC
52
+ get parsedValue() {
53
+ return DateTime.fromISO(this.initialValue, { zone: 'UTC' })
54
+ }
55
+
56
+ get displayTimezone() {
57
+ return this.timezoneValue || this.browserZone
58
+ }
59
+
15
60
  connect() {
61
+ if (this.isOnShow || this.isOnIndex) {
62
+ this.initShow()
63
+ } else if (this.isOnEdit) {
64
+ this.initEdit()
65
+ }
66
+ }
67
+
68
+ // Turns the value in the controller wrapper into the timezone of the browser
69
+ initShow() {
70
+ this.context.element.innerText = this.parsedValue.setZone(this.displayTimezone).toFormat(this.formatValue)
71
+ }
72
+
73
+ initEdit() {
16
74
  const options = {
17
75
  enableTime: false,
18
76
  enableSeconds: false,
19
77
  // eslint-disable-next-line camelcase
20
- time_24hr: false,
78
+ time_24hr: this.time24HrValue,
21
79
  locale: {
22
80
  firstDayOfWeek: 0,
23
81
  },
24
82
  altInput: true,
25
83
  }
26
- const enableTime = castBoolean(this.inputTarget.dataset.enableTime)
27
84
 
28
85
  // Set the format of the displayed input field.
29
- options.altFormat = this.inputTarget.dataset.pickerFormat
86
+ options.altFormat = this.pickerFormatValue
30
87
 
31
88
  // Set first day of the week.
32
- options.locale.firstDayOfWeek = this.inputTarget.dataset.firstDayOfWeek
89
+ options.locale.firstDayOfWeek = this.firstDayOfWeekValue
33
90
 
34
91
  // Enable time if needed.
35
- options.enableTime = enableTime
36
- options.enableSeconds = enableTime
37
-
38
- let currentValue
92
+ options.enableTime = this.enableTimeValue
93
+ options.enableSeconds = this.enableTimeValue
39
94
 
40
95
  // enable timezone display
41
- if (enableTime) {
42
- currentValue = DateTime.fromISO(this.inputTarget.value, { zone: window.Avo.configuration.timezone })
43
- currentValue = currentValue.setZone(this.inputTarget.dataset.timezone)
44
- currentValue = currentValue.toISO()
96
+ if (this.enableTimeValue) {
97
+ options.defaultDate = this.parsedValue.setZone(this.displayTimezone).toISO()
45
98
 
46
99
  options.dateFormat = 'Y-m-d H:i:S'
47
- // eslint-disable-next-line camelcase
48
- options.time_24hr = castBoolean(this.inputTarget.dataset.time24hr)
49
- // this.timezone = Intl.DateTimeFormat().resolvedOptions().timeZone
50
- options.appTimezone = this.inputTarget.dataset.timezone
51
100
  } else {
52
101
  // Because the browser treats the date like a timestamp and updates it ot 00:00 hour, when on a western timezone the date will be converted with one day offset.
53
102
  // Ex: 2022-01-30 will render as 2022-01-29 on an American timezone
54
- currentValue = universalTimestamp(this.inputTarget.value)
103
+ options.defaultDate = universalTimestamp(this.initialValue)
55
104
  }
56
105
 
57
- options.defaultDate = currentValue
58
-
59
106
  flatpickr(this.inputTarget, options)
60
107
  }
61
108
  }
@@ -8,7 +8,6 @@ module Avo
8
8
  include Avo::Concerns::HasModel
9
9
  include Avo::Concerns::HasFields
10
10
  include Avo::Concerns::HasStimulusControllers
11
- include Avo::Concerns::ModelClassConstantized
12
11
 
13
12
  delegate :view_context, to: ::Avo::App
14
13
  delegate :simple_format, :content_tag, to: :view_context
@@ -30,6 +29,7 @@ module Avo
30
29
  class_attribute :search_query, default: nil
31
30
  class_attribute :search_query_help, default: ""
32
31
  class_attribute :includes, default: []
32
+ class_attribute :model_class
33
33
  class_attribute :translation_key
34
34
  class_attribute :default_view_type, default: :table
35
35
  class_attribute :devise_password_optional, default: false
@@ -28,7 +28,7 @@ module Avo
28
28
  add_prop_from_args args, name: name, default: default, type: :array
29
29
  end
30
30
 
31
- def add_string_prop(args, name, default = [])
31
+ def add_string_prop(args, name, default = nil)
32
32
  add_prop_from_args args, name: name, default: default, type: :string
33
33
  end
34
34
  end
@@ -2,25 +2,29 @@ module Avo
2
2
  module Fields
3
3
  class DateTimeField < DateField
4
4
  attr_reader :format
5
+ attr_reader :picker_format
5
6
  attr_reader :time_24hr
6
7
  attr_reader :timezone
7
8
 
8
9
  def initialize(id, **args, &block)
9
10
  super(id, **args, &block)
10
11
 
11
- @picker_format = args[:picker_format].present? ? args[:picker_format] : "Y-m-d H:i:S"
12
- @time_24hr = args[:time_24hr].present? ? args[:time_24hr] : false
13
- @timezone = args[:timezone].present? ? args[:timezone] : Rails.application.config.time_zone
12
+ add_boolean_prop args, :time_24hr
13
+ add_string_prop args, :picker_format, "Y-m-d H:i:S"
14
+ add_string_prop args, :format, "yyyy-LL-dd TT"
15
+ add_string_prop args, :timezone
14
16
  end
15
17
 
16
18
  def formatted_value
17
19
  return nil if value.nil?
18
20
 
19
- if @format.is_a?(Symbol)
20
- value.to_time.in_time_zone(timezone).to_formatted_s(@format)
21
- else
22
- value.to_time.in_time_zone(timezone).strftime(@format)
23
- end
21
+ value.utc.to_time.iso8601
22
+ end
23
+
24
+ def edit_formatted_value
25
+ return nil if value.nil?
26
+
27
+ value.utc.to_formatted_s(:db)
24
28
  end
25
29
 
26
30
  def fill_field(model, key, value, params)
@@ -32,7 +36,7 @@ module Avo
32
36
 
33
37
  return model if value.blank?
34
38
 
35
- model[id] = value.to_time.in_time_zone(Rails.application.config.time_zone)
39
+ model[id] = value.in_time_zone(timezone)
36
40
 
37
41
  model
38
42
  end
data/lib/avo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Avo
2
- VERSION = "2.9.1.pre2" unless const_defined?(:VERSION)
2
+ VERSION = "2.9.1.pre3" unless const_defined?(:VERSION)
3
3
  end