Skip to content

Commit

Permalink
Merge pull request #389 from bullet-train-co/jeremy/revert-334
Browse files Browse the repository at this point in the history
Revert #334 - Themes localization
  • Loading branch information
jagthedrummer authored Aug 9, 2023
2 parents bd547b2 + fe85c52 commit 4bd8ede
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 257 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def destroy
include strong_parameters_from_api

def process_params(strong_params)
assign_date_and_time(strong_params, :last_used_at)
# 🚅 super scaffolding will insert processing for new fields above this line.
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,12 @@ module Fields::DateAndTimeSupport
extend ActiveSupport::Concern

def assign_date_and_time(strong_params, attribute)
deprecator = ActiveSupport::Deprecation.new("2.0", "BulletTrain::Fields")
deprecator.deprecation_warning(
"assign_date_and_time",
"Please assign an ISO8601 datetime string as form field value instead and remove all assign_date_and_time assignments,
see https://ruby-doc.org/3.2.2/exts/date/DateTime.html"
)
attribute = attribute.to_s
time_zone_attribute = "#{attribute}_time_zone"
if strong_params.dig(attribute).present?
begin
strong_params[attribute] = DateTime.iso8601(strong_params[attribute])
rescue ArgumentError
time_zone = ActiveSupport::TimeZone.new(strong_params[time_zone_attribute] || current_team.time_zone)
strong_params.delete(time_zone_attribute)
strong_params[attribute] = time_zone.strptime(strong_params[attribute], t("global.formats.date_and_time"))
end
time_zone = ActiveSupport::TimeZone.new(strong_params[time_zone_attribute] || current_team.time_zone)
strong_params.delete(time_zone_attribute)
strong_params[attribute] = time_zone.strptime(strong_params[attribute], t("global.formats.date_and_time"))
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,11 @@ module Fields::DateSupport
extend ActiveSupport::Concern

def assign_date(strong_params, attribute)
deprecator = ActiveSupport::Deprecation.new("2.0", "BulletTrain::Fields")
deprecator.deprecation_warning(
"assign_date",
"Please assign an ISO8601 date string as field value instead and remove all assign_date assignments,
see https://ruby-doc.org/3.2.2/exts/date/Date.html"
)
attribute = attribute.to_s
if strong_params.dig(attribute).present?
begin
strong_params[attribute] = Date.iso8601(strong_params[attribute])
rescue ArgumentError
parsed_value = Chronic.parse(strong_params[attribute])
return nil unless parsed_value
strong_params[attribute] = parsed_value.to_date
end
parsed_value = Chronic.parse(strong_params[attribute])
return nil unless parsed_value
strong_params[attribute] = parsed_value.to_date
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@ require("daterangepicker/daterangepicker.css");

// requires jQuery, moment, might want to consider a vanilla JS alternative
import 'daterangepicker';
import moment from 'moment-timezone'

export default class extends Controller {
static targets = [ "field", "displayField", "clearButton", "currentTimeZoneWrapper", "timeZoneButtons", "timeZoneSelectWrapper", "timeZoneField", "timeZoneSelect" ]
static targets = [ "field", "clearButton", "currentTimeZoneWrapper", "timeZoneButtons", "timeZoneSelectWrapper", "timeZoneField" ]
static values = {
includeTime: Boolean,
defaultTimeZones: Array,
dateFormat: String,
timeFormat: String,
currentTimeZone: String,
isAmPm: Boolean,
pickerLocale: { type: Object, default: {} }
cancelButtonLabel: { type: String, default: "Cancel" },
applyButtonLabel: { type: String, default: "Apply" }
}

connect() {
Expand All @@ -30,33 +26,13 @@ export default class extends Controller {
event.preventDefault()

$(this.fieldTarget).val('')
$(this.displayFieldTarget).val('')
}

currentTimeZone(){
return (
( this.hasTimeZoneSelectWrapperTarget && $(this.timeZoneSelectWrapperTarget).is(":visible") && this.timeZoneSelectTarget.value ) ||
( this.hasTimeZoneFieldTarget && this.timeZoneFieldTarget.value ) ||
this.currentTimeZoneValue
)
}

applyDateToField(event, picker) {
const format = this.includeTimeValue ? this.timeFormatValue : this.dateFormatValue
const newTimeZone = this.currentTimeZone()
const momentVal = (
picker ?
moment(picker.startDate.toISOString()).tz(newTimeZone, true) :
moment.tz(moment(this.fieldTarget.value, "YYYY-MM-DDTHH:mm").format("YYYY-MM-DDTHH:mm"), newTimeZone)
)
const displayVal = momentVal.format(format)
const dataVal = this.includeTimeValue ? momentVal.toISOString(true) : momentVal.format('YYYY-MM-DD')
$(this.displayFieldTarget).val(displayVal)
$(this.fieldTarget).val(dataVal)
const format = this.includeTimeValue ? 'MM/DD/YYYY h:mm A' : 'MM/DD/YYYY'
$(this.fieldTarget).val(picker.startDate.format(format))
// bubble up a change event when the input is updated for other listeners
if(picker){
this.displayFieldTarget.dispatchEvent(new CustomEvent('change', { detail: { picker: picker }}))
}
this.fieldTarget.dispatchEvent(new CustomEvent('change', { detail: { picker: picker }}))
}

showTimeZoneButtons(event) {
Expand All @@ -67,18 +43,15 @@ export default class extends Controller {
$(this.timeZoneButtonsTarget).toggleClass('hidden')
}

// triggered on other click from the timezone buttons
showTimeZoneSelectWrapper(event) {
// don't follow the anchor
event.preventDefault()

$(this.timeZoneButtonsTarget).toggleClass('hidden')

if (this.hasTimeZoneSelectWrapperTarget) {
$(this.timeZoneSelectWrapperTarget).toggleClass('hidden')
}
if(!["", null].includes(this.fieldTarget.value)){
$(this.displayFieldTarget).trigger("apply.daterangepicker");
}
}

resetTimeZoneUI(e) {
Expand All @@ -92,70 +65,39 @@ export default class extends Controller {
}
}

// triggered on selecting a new timezone using the buttons
setTimeZone(event) {
// don't follow the anchor
event.preventDefault()

const currentTimeZoneEl = this.currentTimeZoneWrapperTarget.querySelector('a')
$(this.timeZoneFieldTarget).val(event.target.dataset.value)
$(currentTimeZoneEl).text(event.target.dataset.label)
const {value} = event.target.dataset

$(this.timeZoneFieldTarget).val(value)
$(currentTimeZoneEl).text(value)

$('.time-zone-button').removeClass('button').addClass('button-alternative')
$(event.target).removeClass('button-alternative').addClass('button')
this.resetTimeZoneUI()
if(!["", null].includes(this.fieldTarget.value)){
$(this.displayFieldTarget).trigger("apply.daterangepicker");
}
}

// triggered on selecting a new timezone from the timezone picker
selectTimeZoneChange(event) {
if(!["", null].includes(this.fieldTarget.value)){
$(this.displayFieldTarget).trigger("apply.daterangepicker");
}
}

// triggered on cancel click from the timezone picker
cancelSelect(event) {
event.preventDefault()
this.resetTimeZoneUI()
if(!["", null].includes(this.fieldTarget.value)){
$(this.displayFieldTarget).trigger("apply.daterangepicker")
}
}

displayFieldChange(event) {
const newTimeZone = this.currentTimeZone()
const format = this.includeTimeValue ? this.timeFormatValue : this.dateFormatValue
const momentParsed = moment(this.displayFieldTarget.value, format, false)
if(momentParsed.isValid()){
const momentVal = moment.tz(momentParsed.format("YYYY-MM-DDTHH:mm"), newTimeZone)
const dataVal = this.includeTimeValue ? momentVal.toISOString(true) : momentVal.format('YYYY-MM-DD')
$(this.fieldTarget).val(dataVal)
} else {
// nullify field value when the display format is wrong
$(this.fieldTarget).val("")
}
}

initPluginInstance() {
const localeValues = this.pickerLocaleValue
const isAmPm = this.isAmPmValue
localeValues['format'] = this.includeTimeValue ? this.timeFormatValue : this.dateFormatValue

$(this.displayFieldTarget).daterangepicker({
$(this.fieldTarget).daterangepicker({
singleDatePicker: true,
timePicker: this.includeTimeValue,
timePickerIncrement: 5,
autoUpdateInput: false,
locale: localeValues,
timePicker24Hour: !isAmPm,
locale: {
cancelLabel: this.cancelButtonLabelValue,
applyLabel: this.applyButtonLabelValue,
format: this.includeTimeValue ? 'MM/DD/YYYY h:mm A' : 'MM/DD/YYYY'
}
})

$(this.displayFieldTarget).on('apply.daterangepicker', this.applyDateToField.bind(this))
$(this.displayFieldTarget).on('cancel.daterangepicker', this.clearDate.bind(this))
$(this.displayFieldTarget).on('input', this,this.displayFieldChange.bind(this));
$(this.fieldTarget).on('apply.daterangepicker', this.applyDateToField.bind(this))
$(this.fieldTarget).on('cancel.daterangepicker', this.clearDate.bind(this))

this.pluginMainEl = this.displayFieldTarget
this.pluginMainEl = this.fieldTarget
this.plugin = $(this.pluginMainEl).data('daterangepicker') // weird

// Init time zone select
Expand All @@ -171,6 +113,7 @@ export default class extends Controller {
$(this.timeZoneSelect).on('change.select2', function(event) {
const currentTimeZoneEl = self.currentTimeZoneWrapperTarget.querySelector('a')
const {value} = event.target

$(self.timeZoneFieldTarget).val(value)
$(currentTimeZoneEl).text(value)

Expand All @@ -183,6 +126,7 @@ export default class extends Controller {
} else {
// deselect any selected button
$('.time-zone-button').removeClass('button').addClass('button-alternative')

selectedOptionTimeZoneButton.text(value)
selectedOptionTimeZoneButton.attr('data-value', value).removeAttr('hidden')
selectedOptionTimeZoneButton.removeClass(['hidden', 'button-alternative']).addClass('button')
Expand All @@ -195,8 +139,10 @@ export default class extends Controller {

teardownPluginInstance() {
if (this.plugin === undefined) { return }

$(this.pluginMainEl).off('apply.daterangepicker')
$(this.pluginMainEl).off('cancel.daterangepicker')

// revert to original markup, remove any event listeners
this.plugin.remove()

Expand Down
1 change: 0 additions & 1 deletion bullet_train-fields/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
"cp-cli": "^2.0.0",
"emoji-mart": "^5.1.0",
"microbundle": "^0.13.0",
"moment-timezone": "^0.5.43",
"np": "^7.6.0",
"npm-watch": "^0.11.0",
"rimraf": "^3.0.2",
Expand Down
12 changes: 0 additions & 12 deletions bullet_train-fields/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3445,18 +3445,6 @@ minimist@^1.2.0, minimist@^1.2.5:
resolved "https://registry.npmjs.org/minimist/-/minimist-1.2.5.tgz"
integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==

moment-timezone@^0.5.43:
version "0.5.43"
resolved "https://registry.yarnpkg.com/moment-timezone/-/moment-timezone-0.5.43.tgz#3dd7f3d0c67f78c23cd1906b9b2137a09b3c4790"
integrity sha512-72j3aNyuIsDxdF1i7CEgV2FfxM1r6aaqJyLB2vwb33mXYyoyLly+F1zbWqhA3/bVIoJ4szlUoMbUnVdid32NUQ==
dependencies:
moment "^2.29.4"

moment@^2.29.4:
version "2.29.4"
resolved "https://registry.yarnpkg.com/moment/-/moment-2.29.4.tgz#3dbe052889fe7c1b2ed966fcb3a77328964ef108"
integrity sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==

moment@^2.9.0:
version "2.29.1"
resolved "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,17 @@ def destroy

# Never trust parameters from the scary internet, only allow the white list through.
def delivery_params
params.require(:webhooks_outgoing_delivery).permit(
strong_params = params.require(:webhooks_outgoing_delivery).permit(
:event_id,
:endpoint_url,
:delivered_at,
# 🚅 super scaffolding will insert new fields above this line.
# 🚅 super scaffolding will insert new arrays above this line.
)

assign_date_and_time(strong_params, :delivered_at)
# 🚅 super scaffolding will insert processing for new fields above this line.

strong_params
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def destroy
def process_params(strong_params)
# 🚅 skip this section when scaffolding.
assign_boolean(strong_params, :boolean_button_value)
assign_date_and_time(strong_params, :date_and_time_field_value)
assign_checkboxes(strong_params, :multiple_button_values)
assign_checkboxes(strong_params, :multiple_option_values)
assign_select_options(strong_params, :multiple_super_select_values)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def tangible_thing_params
:cloudinary_image_value,
:date_field_value,
:date_and_time_field_value,
:date_and_time_field_value_time_zone,
:email_field_value,
:file_field_value,
:file_field_value_removal,
Expand Down
4 changes: 4 additions & 0 deletions bullet_train-super_scaffolding/lib/scaffolding/transformer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,10 @@ def valid_#{collection_name}
end

special_processing = case type
when "date_field"
"assign_date(strong_params, :#{name})"
when "date_and_time_field"
"assign_date_and_time(strong_params, :#{name})"
when "buttons"
if boolean_buttons
"assign_boolean(strong_params, :#{name})"
Expand Down
Loading

0 comments on commit 4bd8ede

Please sign in to comment.