Adds filter to customize Default DurationOptions.#882
Adds filter to customize Default DurationOptions.#882mauteri merged 7 commits intoGatherPress:developfrom
Conversation
The filter works, but something in the DateTimeRange useEffect is reverting back to the default values.
mauteri
left a comment
There was a problem hiding this comment.
Thanks for contributing, Anthony! Take a look at my comments and let me know what you think.
src/components/DurationOptions.js
Outdated
| }, | ||
| ]; | ||
|
|
||
| export default applyFilters('gatherpress.durationOptions', DurationOptionsDefaults ); |
There was a problem hiding this comment.
Let's put this back in helpers/datetime.js, but adjust it a bit. This area is for React components, this is just returning an object.
src/helpers/datetime.js
Outdated
| export function getDateTimeOffset() { | ||
| return ( | ||
| durationOptions.find( | ||
| DurationOptions.find( |
There was a problem hiding this comment.
After changes above this would become durationOptions().find(
src/components/Duration.js
Outdated
| label={__('Duration', 'gatherpress')} | ||
| value={ | ||
| durationOptions.some((option) => option.value === duration) | ||
| DurationOptions.some((option) => option.value === duration) |
There was a problem hiding this comment.
this will become durationOpions().some... after changes below.
src/components/Duration.js
Outdated
| : false | ||
| } | ||
| options={durationOptions} | ||
| options={DurationOptions} |
There was a problem hiding this comment.
options={durationOptions()}
* Moves the object back into the datetime helpers file * Converted to a function that returns the filtered values * Updated usage of durationOptions
|
The comments made a lot of sense and I pushed up a new commit with the updates. With those in place the filter from a theme was working great, but there is one bug that I was able to drill down. When the page loads the Do you think it would be better to save the duration value to the post meta along with the start and end dates so this calculation doesn't need to happen? |
Hey @ajskelton, can you try to call this function to get the duration value? This should serve the same purpose you are looking for with storing meta. Basically, it takes the start/end datetime and returns the duration or false (same as you'd get from the meta) https://github.com/GatherPress/gatherpress/blob/main/src/helpers/datetime.js#L127 Let me know if that works. Thx! |
|
@mauteri This is the function that doesn't use the filtered values when the default state is being set. I tested by adding some console.logs in the .find(). When you refresh the page and select the block you see the default duration values logging to the console instead of the filtered duration. So it sets the duration to false before checking against the filtered options. When it runs here: gatherpress/src/stores/datetime.js Line 23 in 4b89474 |
Oh gotcha, let me give it some thought. |
|
Hello @ajskelton & @mauteri, are you aware of WordPress/gutenberg#60971, which seems slightly related? |
Sets the initial duration value to null instead of using the getDateTimeOffset function. This function is moved to the getDuration selector. This selector is run in the Duration component which renders after any theme filters have been registered. We do need to check if the value is false when the user selects the value to custom pick the end time.
|
@mauteri I found a fix for the issue. Instead of running the getDateTimeOffset() function in the DEFAULT_STATE, i changed that initial duration value to be null. Then I moved that function to the getDuration selector. When the Duration Component renders it always runs the getDuration selector to dynamically get the value based on the start and end times. And when this runs all of the theme filters have already been registered. We do need to check if the current state is false here in the selector. I first tried just using the getDateTimeOffset but when the value was set to false the state wasn't saving the false value and the DateTimeEnd was never rendering. I don't have a ton of experience working with custom stores so let me know if this is the wrong way to go about this. @carstingaxion That new Date/Time Range component does look nice. I think it's still just design right now, I couldn't see any code commits to check out. |
src/stores/datetime.js
Outdated
| getDateTimeStart: (state) => state.dateTimeStart, | ||
| getDateTimeEnd: (state) => state.dateTimeEnd, | ||
| getDuration: (state) => state.duration, | ||
| getDuration: (state) => state.duration === false ? false : getDateTimeOffset(), |
There was a problem hiding this comment.
getDuration: (state) => false === state.duration ? false : getDateTimeOffset(),
Preview changes with PlaygroundYou can preview the recent changes for PR#882 with the following PHP versions: PHP Version 8.3
PHP Version 7.4
Download Made with 💙 from GatherPress & a little bit of WordPress Playground. Changes will not persist between sessions. |
|
This can be tested with this gist. https://gist.github.com/mauteri/5b8402d29de94502fff1e0da9e68f2d8 |
The filter works, but something in the DateTimeRange useEffect is reverting back to the default values. I am not seeing this behavior now with my testing.
Description of the Change
Closes #876
How to test the Change
Changelog Entry
Credits
Props @ajskelton, @mauteri
Checklist: