eventespresso / barista

Javascript modules & tools for Event Espresso development
GNU General Public License v3.0
5 stars 1 forks source link

Decimal Values Ignored when Shifting Datetimes via Bulk Editing #1190

Closed tn3rb closed 1 year ago

tn3rb commented 1 year ago

steps to reproduce

alexkuc commented 1 year ago

Issue successfully reproduced locally, will proceed to work on the bug fix

alexkuc commented 1 year ago

@tn3rb In the process of this bug fix, I will make this field required

image

Because currently, if you omit this field, but specify time, e.g. 1.5 hours, nothing will happen i.e. Submit button works without asking for that earlier/later but nothing actually happens to time. Not great UX, imho.

alexkuc commented 1 year ago

Summary of my understanding of GraphQL implementation on EE:

alexkuc commented 1 year ago

It looks like bug is caused by this utility function

https://github.com/eventespresso/barista/blob/aeba6e81028b49d12c94032164406a637454481f/packages/dates/src/utils/misc.ts#L32-L44

alexkuc commented 1 year ago

@tn3rb bug happens for both operations, earlier and later so I'll be fixing both

alexkuc commented 1 year ago

Further inspection of the source code reveals the following

https://github.com/eventespresso/barista/blob/aeba6e81028b49d12c94032164406a637454481f/packages/dates/src/utils/addSub.ts#L1-L13

https://github.com/eventespresso/barista/blob/aeba6e81028b49d12c94032164406a637454481f/packages/dates/src/utils/addSub.ts#L40-L43

https://github.com/eventespresso/barista/blob/aeba6e81028b49d12c94032164406a637454481f/packages/dates/src/utils/addSub.ts#L45-L52

The functions are not isomorphic even though date-fns supports subtraction function, e.g. subHours. Perhaps it is worthwhile to make it so.

Both functions use the same rounding logic which causes the bug:

Positive decimals will be rounded using Math.floor, decimals less than zero will be rounded using Math.ceil.

alexkuc commented 1 year ago

Due to non-accurate representation of floating numbers in JS, I'll be using the following logic

alexkuc commented 1 year ago

Function add is used only by misc.ts so scope for change is minimal but adjustment of unit tests might be necessary

image

alexkuc commented 1 year ago

Same goes for sub function which is only used by misc.ts

image

alexkuc commented 1 year ago

Inspecting unit tests suggest that ability to support fractions was never intended

alexkuc commented 1 year ago

Apparently, the value of 0.5 is not supported

image

Edit: when trying to set value of 0.5 the warning above is shown and the value is changed to 1

alexkuc commented 1 year ago

TODOs

Edit: TODO for field validation is now tracked via #1196

tn3rb commented 1 year ago

thank you for the thorough description of the issue 🙇🏻

tn3rb commented 1 year ago

re: earlier/later input

I will make this field required

is/was there a default set in the submit handler for that functionality?

ie: if the default was later then maybe we can simply set that as the default for the input, then remove the null option, so that the input will always have a value

alexkuc commented 1 year ago

There is no default currently. When editor loads, it looks like this:

image

We could set it to later by default but that might lead to some users overlooking it and changing event to a wrong time.

alexkuc commented 1 year ago

Using simple console.log I can see that function receives a Date object for its argument date

https://github.com/eventespresso/barista/blob/c9ddddde495eb7d6d3d851547f378c2e94a8542b/packages/dates/src/utils/misc.ts#L32-L44

alexkuc commented 1 year ago

After quick chat with @tn3rb on Slack, it became obvious that support for floats leads to not-so-trivial rounding rules. For example, if shifting date by 5.23 days, how do you round that? Do you round it as 5 days 6 hours or as 5 days, 5 hours, 31 minutes, 12 seconds? While technically feasible, it will require time to write this logic, write unit tests, do the actual testing, etc. From a business point of view, it is not entirely clear how valuable it is to support ability to shift date by 5.23 so instead a different approach will be taken. Support for floats will be removed and each unit will be presented as a standalone field. Wireframe follows:

image

alexkuc commented 1 year ago

After further code review, I have decided to finish the feature since it was actually 80% complete. The logic for calculating time is as follows: