Skip to content

Commit

Permalink
triv: fix datepicker default value - used in dashboard (#38)
Browse files Browse the repository at this point in the history
* triv: fix datepicker default value - used in dashboard

* triv: bump version
  • Loading branch information
MartinCervenkaSB authored Dec 17, 2024
1 parent e77b09e commit 928a96a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "django-smartbase-admin"
version = "0.2.89"
version = "0.2.90"
description = ""
authors = ["SmartBase <[email protected]>"]
readme = "README.md"
Expand Down
22 changes: 18 additions & 4 deletions src/django_smartbase_admin/engine/filter_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,19 @@ def get_shortcuts_dict(self):
shortcuts[key].append(self.process_shortcut(shortcut, now))
return shortcuts

def get_default_label(self):
if self.default_value_shortcut_index is not None:
return self.get_shortcuts()[self.default_value_shortcut_index]["label"]
return super().get_default_value()

def get_default_value(self):
if self.default_value_shortcut_index is not None:
selected_shortcut_value = self.get_shortcuts()[
self.default_value_shortcut_index
]["value"]
return self.get_value_from_date_or_range(selected_shortcut_value)
return SBAdminViewService.json_dumps_for_url(
self.get_value_from_date_or_range(selected_shortcut_value)
)
return super().get_default_value()

def get_data(self):
Expand Down Expand Up @@ -374,9 +381,14 @@ def get_range_from_value(cls, filter_value):
return [None, None]
date_format = cls.DATE_FORMAT
if type(filter_value) is list:
if type(filter_value[0]) is int:
return [
timezone.now() + timedelta(days=filter_value[0]),
timezone.now() + timedelta(days=filter_value[1]),
]
return [
timezone.now() + timedelta(days=filter_value[0]),
timezone.now() + timedelta(days=filter_value[1]),
datetime.strptime(filter_value[0], date_format),
datetime.strptime(filter_value[1], date_format),
]
try:
days_range = json.loads(filter_value)
Expand All @@ -397,9 +409,11 @@ def get_range_from_value(cls, filter_value):
def get_value_from_date_or_range(cls, date_or_range):
if not isinstance(date_or_range, list):
return datetime.strftime(date_or_range, cls.DATE_FORMAT)
if type(date_or_range[0]) is int:
return date_or_range
date_from = datetime.strftime(date_or_range[0], cls.DATE_FORMAT)
date_to = datetime.strftime(date_or_range[1], cls.DATE_FORMAT)
return f"{date_from}{cls.DATE_RANGE_SPLIT}{date_to}"
return [date_from, date_to]

def parse_value_from_input(self, request, filter_value):
return self.get_range_from_value(filter_value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,24 @@ export const createRadioInput = (id, name, value, label, checked, index) => {

// eslint-disable-next-line no-unused-vars
export const customActionsPlugin = (fp) => {
const setInitialValueAndLabel = (realInput, shortcuts, baseId) => {
const shortcutValue = JSON.parse(realInput.value)
const from = new Date()
from.setDate(from.getDate() + shortcutValue[0])

const to = new Date()
to.setDate(to.getDate() + shortcutValue[1])
fp.setDate([from, to], false, fp.config.dateFormat)

shortcuts.forEach((shortcut, idx) => {
if(JSON.stringify(shortcut.value) === realInput.value) {
document.getElementById(`${baseId}_range${idx}`).checked = true
realInput.dataset['label'] = shortcut.label
}
})
}


const createShortcuts = () => {
const realInput = document.getElementById(fp.element.dataset.sbadminDatepickerRealInputId)
const baseId = realInput.id
Expand Down Expand Up @@ -312,22 +330,13 @@ export const customActionsPlugin = (fp) => {
})
fp.calendarContainer.prepend(el)

if(realInput.value){
setInitialValueAndLabel(realInput, shortcuts, baseId)
}

realInput.addEventListener('SBTableFilterFormLoad', () => {
try {
const shortcutValue = JSON.parse(realInput.value)
const from = new Date()
from.setDate(from.getDate() + shortcutValue[0])

const to = new Date()
to.setDate(to.getDate() + shortcutValue[1])
fp.setDate([from, to], false, fp.config.dateFormat)

shortcuts.forEach((shortcut, idx) => {
if(JSON.stringify(shortcut.value) === realInput.value) {
document.getElementById(`${baseId}_range${idx}`).checked = true
realInput.dataset['label'] = shortcut.label
}
})
setInitialValueAndLabel(realInput, shortcuts, baseId)
}
catch {
fp.setDate(realInput.value, false, fp.config.dateFormat)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
id="{{ filter_widget.input_id }}"
name="{{ filter_widget.input_name }}"
form="{{ filter_widget.view_id }}-filter-form"
{% if filter_widget.get_default_value %}value="{{ filter_widget.get_default_value }}"{% endif %}
{% if filter_widget.get_default_value %}value="{{ filter_widget.get_default_value }}" data-label="{{ filter_widget.get_default_label }}"{% endif %}
{% if not all_filters_visible %}disabled{% endif %}
>
<input type="text"
Expand Down

0 comments on commit 928a96a

Please sign in to comment.