I am building a plugin to handle dates, there are a lot but none that fit what I need, I am having an issue getting the initial start date and initial end date from bubble to the plugin. Instead its falling to my fall back date I have hard coded. I tried adding a console longs to see if the dates are being parsed but there is nothing coming through, please help me out.
Fields:
Initialize:
initialize: function(instance, context) {
let start = instance.data.initialStartDate ? moment(instance.data.initialStartDate) : moment().startOf('month');
let end = instance.data.initialEndDate ? moment(instance.data.initialEndDate) : moment().endOf('month');
instance.publishState('start_Datetime', start.toDate());
instance.publishState('end_Datetime', end.toDate());
instance.data.datePicker = null; // Placeholder for picker instance
}
Update:
Update: function(instance, properties, context) {
let ID = properties.id_field;
let range = {};
let time = !properties.date_only;
// Define date ranges based on properties
if (properties.today) range['Today'] = [moment(), moment()];
if (properties.yesterday) range['Yesterday'] = [moment().subtract(1, 'days'), moment().subtract(1, 'days')];
if (properties.tomorrow) range['Tomorrow'] = [moment().add(1, 'days'), moment().add(1, 'days')];
if (properties.last_7) range['Last 7 Days'] = [moment().subtract(6, 'days'), moment()];
if (properties.last_30) range['Last 30 Days'] = [moment().subtract(29, 'days'), moment()];
if (properties.next_7) range['Next 7 Days'] = [moment(), moment().add(7, 'days')];
if (properties.next_30) range['Next 30 Days'] = [moment(), moment().add(29, 'days')];
if (properties.this_month) range['This Month'] = [moment().startOf('month'), moment().endOf('month')];
if (properties.next_month) range['Next Month'] = [moment().add(1, 'month').startOf('month'), moment().add(1, 'month').endOf('month')];
if (properties.last_month) range['Last Month'] = [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')];
let pickerElement = $('#' + ID);
// Normalize and validate mode
const normalizedMode = properties.mode ? properties.mode.trim().toLowerCase() : null;
// Ensure valid start and end dates from Bubble
let startDate = properties.initial_start_date && moment(properties.initial_start_date, moment.ISO_8601).isValid()
? moment(properties.initial_start_date, moment.ISO_8601)
: moment().startOf('day');
let endDate = properties.initial_end_date && moment(properties.initial_end_date, moment.ISO_8601).isValid()
? moment(properties.initial_end_date, moment.ISO_8601)
: moment().endOf('day');
// Debugging logs
console.log("Initial Start Date (from Bubble):", properties.initial_start_date);
console.log("Initial End Date (from Bubble):", properties.initial_end_date);
// Fallback if undefined
startDate = startDate && startDate.isValid() ? startDate : moment().startOf('day');
endDate = endDate && endDate.isValid() ? endDate : moment().endOf('day');
console.log("Final Start Date Used:", startDate.format());
console.log("Final End Date Used:", endDate.format());
// Avoid publishing redundant states
if (!instance.data.prevStartDate || !instance.data.prevEndDate ||
!instance.data.prevStartDate.isSame(startDate) || !instance.data.prevEndDate.isSame(endDate)) {
instance.publishState('start_Datetime', startDate.toDate());
instance.publishState('end_Datetime', endDate.toDate());
instance.data.prevStartDate = startDate;
instance.data.prevEndDate = endDate;
}
// Initialize the date picker
if (normalizedMode === "multi") {
console.log("Mode: Multi");
instance.data.datePicker = pickerElement.daterangepicker({
singleDatePicker: false,
showDropdowns: true,
autoApply: true,
ranges: range,
timePicker: time,
alwaysShowCalendars: true,
opens: 'left',
startDate: startDate,
endDate: endDate
}, function(start, end) {
console.log("User changed range:", start.format(), end.format());
if (!start.isSame(instance.data.prevStartDate) || !end.isSame(instance.data.prevEndDate)) {
instance.publishState('start_Datetime', start.toDate());
instance.publishState('end_Datetime', end.toDate());
requestAnimationFrame(() => instance.triggerEvent("date_range_updated"));
instance.data.prevStartDate = start;
instance.data.prevEndDate = end;
}
});
} else if (normalizedMode === "single") {
console.log("Mode: Single");
instance.data.datePicker = pickerElement.daterangepicker({
singleDatePicker: true,
showDropdowns: true,
autoApply: true,
ranges: range,
timePicker: time,
alwaysShowCalendars: true,
opens: 'left',
startDate: startDate,
endDate: startDate // Keep single mode consistent
}, function(start) {
console.log("User changed date:", start.format());
if (!start.isSame(instance.data.prevStartDate)) {
instance.publishState('start_Datetime', start.toDate());
instance.publishState('end_Datetime', start.toDate());
requestAnimationFrame(() => instance.triggerEvent("date_range_updated"));
instance.data.prevStartDate = start;
}
});
} else {
console.error("Invalid mode selected:", properties.mode);
}
}
Console: