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

Add Editor Preview and move data fetching to its own context #97

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

senadir
Copy link

@senadir senadir commented Apr 12, 2020

Hi!

First, thank you for this wonderful project, I always wanted to build one for a side project and to get to use one is wonderful.
Following my talk with Kelly in slack, she suggested I can contribute.

So this PR does some things, notably:
1- It introduces an Editor preview by simply rendering the App component in the editor.
for this to happen, I needed to create a new Context that fetches the data and pass it to EventProvider, I put it in a separate context to avoid calling the API each time the context updates, but I believe this can be mitigated if we save the fetch value in a ref and only fetch if that ref is null.
2- To keep compatibility, the context also accepts an optional data prop that has the event data, this allows us to hydrate the data from the server to avoid the initial request, using the same logic that was there.
3- This optional data will also allow us to pass a different kind of data to the editor, currently, I'm using real data, this can be swapped by a preview Data that can be passed the same way we pass the hydrated data

const EditView = () => (
	<Disabled>
		<Calendar data={ previewData } />
	</Disabled>
);

4- we move wporg-calendar-style registration to outside admin as well, so that it can render on the editor.

I'm also tagging @nerrad for a confidence check since I discussed some of those points with him.

image

@senadir senadir added the enhancement New feature or request label Apr 12, 2020
@senadir senadir requested review from StevenDufresne and ryelle and removed request for StevenDufresne April 12, 2020 20:08
export function EventDataProvider( { children, data } ) {
const [ eventData, setEventData ] = useState( [] );
// we bail out early if data is hydrated from the server.
if ( data ) {
Copy link
Author

Choose a reason for hiding this comment

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

I think we should keep this in a ref to save the reference between retenders, but I don’t think we will need it unless we move this logic to EventProvider

Comment on lines +30 to +31
const currentDate = new Date();
const fromDate = new Date( currentDate.getFullYear(), currentDate.getMonth(), 1 );
Copy link
Author

Choose a reason for hiding this comment

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

This can probably benefit from something inside @wordpress/date

const fromDate = new Date( currentDate.getFullYear(), currentDate.getMonth(), 1 );

apiFetch( {
path: `/wp/v2/meetings/from/${ format( 'Y-m-d', fromDate ) }?per_page=12`,
Copy link
Author

Choose a reason for hiding this comment

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

the value 12 is what I brought from the logic within the PHP code.

Copy link
Author

Choose a reason for hiding this comment

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

also depending on the size of data 12 per page could mean several months in advance or simply this week, so the API might be changed to some sort of from-to, and we can fetch more months as long the user is changing the page.

icon="calendar"
label={ __( 'Go to "Meetings" to add or edit your meetings', 'wporg' ) }
/>
<Disabled>
Copy link
Author

Choose a reason for hiding this comment

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

disabling interactive blocks within the editor is kinda a force of habit now 😅since some interaction depends on different contexts, I don’t think this is the case in this block so I might remove the disabled wrap, however, it might be confusing to the user if they think the changes they make in the editor will persist on the frontend (changing default month or changing display style).

Comment on lines +21 to +28
// we bail out early if data is hydrated from the server.
if ( data ) {
return (
<EventDataContext.Provider value={ data }>
{ children }
</EventDataContext.Provider>
);
}
Copy link

Choose a reason for hiding this comment

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

You're breaking a rule of hooks here by bailing early. Instead you should put your condition inside the effect and bail early from the effect instead.

Copy link
Author

Choose a reason for hiding this comment

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

Wouldn’t be right if I was running the hook inside an else clause? I do agree that having everything inside the useEffect would result in the same result but I guess I'm missing the hook rule part.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agree with @nerrad, hooks rules and we can dry up the code by not having to repeat the EventDataContext twice. As he mentioned, just check for data on the first line of the useEffect and return if its hydrated.

Copy link

@nerrad nerrad left a comment

Choose a reason for hiding this comment

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

It's unclear you me why you need both a EventsDataProvider and EventsProvider. Seems to me this could be condensed to one provider.

@StevenDufresne
Copy link
Contributor

StevenDufresne commented Apr 13, 2020

I understand why your instinct led you to add a new provider but I agree with @nerrad -- we probably don't need it. You can add it to the current EventProvider. If anything, we should make the team filtering logic more generic and move it downstream.

@StevenDufresne
Copy link
Contributor

Lastly, Thanks for jumping in on this block! Its always great having more contributors. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants