Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chrome: Adding the post date picker to schedule posts #841

Merged
merged 8 commits into from
May 25, 2017

Conversation

youknowriad
Copy link
Contributor

@youknowriad youknowriad commented May 19, 2017

This PR adds the post data picker to schedule a post.

screen shot 2017-05-19 at 12 47 50

  • Using react-datepicker. Are we ok with that? should we create our own date picker
  • Using moment
  • I'm using the post date GMT field to ease working with the dates. I know there are some discussions around this in Core. Any information I should know about here?

Not done yet

  • I'm using the default styling from react-datepicker. This needs to be tweaked (@joen make me pretty 😄)
  • We need to set the locale to the moment object globally moment.locale( locale )(cc @aduth). Should we do this in the i18n module assuming moment is used globally in WordPress?
  • The time picker is not localized too, it always uses a AM/PM selector.

@youknowriad youknowriad added the General Interface Parts of the UI which don't fall neatly under other labels. label May 19, 2017
@youknowriad youknowriad self-assigned this May 19, 2017
@youknowriad youknowriad requested review from jasmussen and aduth May 19, 2017 11:53
@jasmussen
Copy link
Contributor

This seems good to me, and I'm okay with having to do a little CSS'ing to get it up to snuff ;)

However we will encounter some internationalization things. This could already be adressed and I haven't checked so forgive me if I'm wrong. But I immediately notice the AM/PM toggle, which as a european I find mostly useless. So I'm just checking to make sure, have we a plan for hooking into the standards that WordPress' options table gives us here, including date format?

Also, can we ease the workload by using some of the code from Calypso? There's no reason to reinvent the wheel since they are both GPL.

@aduth
Copy link
Member

aduth commented May 19, 2017

We should pay some mind to this ticket: https://core.trac.wordpress.org/ticket/39222

Specifically on the proposed abstraction that doesn't tie us to Moment specifically. Dunno how we'd want to interop with that patch though.

@youknowriad
Copy link
Contributor Author

youknowriad commented May 19, 2017

If we want to move forward with this branch, maybe we should copy this patch temporarily?

@aduth
Copy link
Member

aduth commented May 19, 2017

I'd be fine with that, but the specifics of copying are a bit fuzzy to me.

@youknowriad
Copy link
Contributor Author

  • I've copied and adapted the patch from https://core.trac.wordpress.org/ticket/39222 to match Gutenberg "guidelines".
  • I also had to update the code, especially the calls to utcOffset to set the second parameter to true. It wasn't working as I expected it to work if I didn't do that.
  • I've also exposed the date settings (timezone, formats) to be able to use them in the PostSchedule component
  • I show/hide the AP/PM selector depending on the timezone.

Thoughts on these updates?

@jasmussen
Copy link
Contributor

I like it!

Can we pre-fill the time with the current time, so it isn't empty until you click a date?

@youknowriad
Copy link
Contributor Author

@aduth do you have an idea how to fix this unit test failure?

@aduth
Copy link
Member

aduth commented May 22, 2017

088fd8f should clear it up. I don't think we'd intended to allow importing .css files, though the Webpack configuration is set up to match them. From the error it appeared the CSS was being interpreted as JavaScript. Instead of trying to accommodate CSS imports from the component, I moved the import into the component's SASS stylesheet.

@youknowriad youknowriad force-pushed the add/post-datepicker branch from 088fd8f to b394b86 Compare May 23, 2017 09:36
@youknowriad
Copy link
Contributor Author

I'd use some technical 👀 on this. Thanks :)

.wp-core-ui .editor-post-schedule__clock-am-button.is-toggled,
.wp-core-ui .editor-post-schedule__clock-pm-button.is-toggled {
background: $light-gray-300;
border-color: $dark-gray-100;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mixed spacing (needs tab).

const handleChange = ( newDate ) => {
onUpdateDate( newDate.format( 'YYYY-MM-DDTHH:mm:ss' ) );
};
const is12HourTime = settings.formats.time.indexOf( 'a' ) !== -1 || settings.formats.time.indexOf( 'A' ) !== -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be inaccurate because it's possible to escape characters in the format. In other words, 'H:i:s \a\w\e\s\o\m\e' is not twelve hour time, but this logic would treat it as though it were.

Also, it could be simpler to force to lower/upper and test index of the one instance of 'a'/'A'.

Accounting for escaping could be tricky, particularly since JavaScript doesn't support lookbehind regular expression patterns. Dunno if there's a better way, but "flipping" the string and doing a look ahead is one option:

/a(?!\\)/i.test( 'h:i:s a'.split( '' ).reverse().join( '' ) )
// true
/a(?!\\)/i.test( 'h:i:s A'.split( '' ).reverse().join( '' ) )
// true
/a(?!\\)/i.test( 'H:i:s \\a\\w\\e\\s\\o\\m\\e'.split( '' ).reverse().join( '' ) )
// false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even this solution is not perfect because this \\\\\a is a 12 hour time but the test returns false

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is the right test /a(?!\\)/i.test( '\\\\\\\\a'.replace(/\\\\/g).split( '' ).reverse().join( '' ) )

@youknowriad youknowriad force-pushed the add/post-datepicker branch from 995bb12 to b7e0954 Compare May 24, 2017 15:57
@youknowriad
Copy link
Contributor Author

Rebased this PR. I'm planning to merge this tomorrow if no more raised concerns.

Comment on lines +52 to +59
// To know if the current timezone is a 12 hour time with look for "a" in the time format
// We also make sure this a is not escaped by a "/"
const is12HourTime = /a(?!\\)/i.test(
settings.formats.time
.toLowerCase() // Test only the lower case a
.replace( /\\\\/g, '' ) // Replace "//" with empty strings
.split( '' ).reverse().join( '' ) // Reverse the string and test for "a" not followed by a slash
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know how old this PR is :), but today we have multiple occurrences of this snippet in the codebase, so it's worth asking:

  • Why reverse the string and not use a non-capturing group like /(?:[^\\])a/?
  • If the RegExp already has the i flag, why pipe through .toLowerCase?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
General Interface Parts of the UI which don't fall neatly under other labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants