F
Filament17mo ago
Saade

Change state before dehydration

For context, i have a DateTimePicker component for selecting the event's start date. If the user marks the all_day checkbox, i need the component to set the hour to 00:00:00 but keeping the date. eg: 28/07/2023 -> 28/07/2023 00:00:00. The thing is: I know i can do this with ->dehydrateStateUsing() but this will override the default dehydration process, which i need because it handles the timezone stuff behind the scenes.. How can i mutate the state before the dehydration process?
Solution:
Filament already have a ->beforeStateDehydrated()
Jump to solution
17 Replies
Dan Harrin
Dan Harrin17mo ago
you could do it in mutateFormDataBeforeSave(), right?
Saade
SaadeOP17mo ago
yeahh, but i then i need to copy the timezone handling to this method 1. the user selects 14:00 in the frontend 2. the data come as 11:00 (GMT -3 to UTC) 3. i mutate the data to 00:00 (GMT -3) 4. then i would need to shift the timezone again to UTC (21:00) the 4th step is already part of the component dehydration process, if i could mutate the data before the dehydration process it would be easier and DRY i need something like parent::dehydrateStateUsing($state) hahaha
awcodes
awcodes17mo ago
Wouldn’t this automatically get handled at the laravel or db level unless your server isn’t UTC.
Saade
SaadeOP17mo ago
my laravel app and db are UTC, and my frontend component is ->timezone($user->timezone)
awcodes
awcodes17mo ago
I would think it would all just work if your make the field type datetime-local. Let the browser worry about the conversion. Then you only have to worry about converting it if the display is outside on an input.
Saade
SaadeOP17mo ago
hmm, i don't get it if the user selects 15:00 in the frontend, the DateTimePicker component will consider that this time is in UTC by default, and save 15:00 in the database which is wrong.. i need the ->timezone($user>timezone) part to the Filament dehydration process to apply -3 hours to that time also, if my record is 12:00 (UTC), does the browser knows it is UTC and apply +3 to that time?
awcodes
awcodes17mo ago
I could be wrong but I’m under the impression that datetime-local basically masks the value based on the users browser settings for their timezone. Meaning anything they select would compensate the UTC offset, if im correct.
awcodes
awcodes17mo ago
👍
Saade
SaadeOP17mo ago
wait
Solution
Saade
Saade17mo ago
Filament already have a ->beforeStateDehydrated()
Saade
SaadeOP17mo ago
🤦‍♂️ 🤦‍♂️ 🤦‍♂️ 🤦‍♂️
awcodes
awcodes17mo ago
Lol.
Saade
SaadeOP17mo ago
goddammit
awcodes
awcodes17mo ago
Sorry for wasting you time. I’m new to this. 😂
Saade
SaadeOP17mo ago
well, VSCode autocompletion sucks, no surprises here...
awcodes
awcodes17mo ago
Yes it does.
Want results from more Discord servers?
Add your server