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

Store controls in state #7706

Draft
wants to merge 7 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions assets/admin/students/student-action-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* WordPress dependencies
*/
import { DropdownMenu } from '@wordpress/components';
import { render, useState } from '@wordpress/element';
import { render, useEffect, useState } from '@wordpress/element';
import { moreVertical } from '@wordpress/icons';
import { applyFilters } from '@wordpress/hooks';
import { applyFilters, applyFiltersAsync } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';

/**
Expand All @@ -25,15 +25,6 @@ export const StudentActionMenu = ( {
studentName,
studentDisplayName,
} ) => {
const [ action, setAction ] = useState( '' );
const [ isModalOpen, setModalOpen ] = useState( false );
const closeModal = ( needsReload ) => {
if ( needsReload ) {
window.location.reload();
}
setModalOpen( false );
};

const defaultControls = [
{
title: __( 'Add to Course', 'sensei-lms' ),
Expand All @@ -57,6 +48,17 @@ export const StudentActionMenu = ( {
},
];

const [ action, setAction ] = useState( '' );
const [ controls, setControls ] = useState( defaultControls );
const [ isModalOpen, setModalOpen ] = useState( false );

const closeModal = ( needsReload ) => {
if ( needsReload ) {
window.location.reload();
}
setModalOpen( false );
};

/**
* Filters controls for the single student action menu.
*
Expand All @@ -68,12 +70,20 @@ export const StudentActionMenu = ( {
*
* @return {Array} Filtered controls.
*/
const controls = applyFilters(
'senseiStudentActionMenuControls',
defaultControls,
setAction,
setModalOpen
);
useEffect( () => {
async function getMenuControls() {
const response = await applyFiltersAsync(
Copy link
Collaborator Author

@donnapep donnapep Nov 6, 2024

Choose a reason for hiding this comment

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

Although this code works, I have no idea why or how. 😅 It looks good in theory, but when setting breakpoints or adding log statements in the code, I noticed that when the fetch in Sensei Pro returns and setControls is called below, the component does not re-render as expected. Despite this, the "Add to Group" and "Remove from Group" options are there in the UI. 🤯

Given I'm not clear on what is happening here, I'm somewhat averse to shipping this change.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe I'm missing something, but I didn't find where the applyFiltersAsync comes from. Checking the Gutenberg package, it doesn't seem to be there.

I'm also seeing this error in my browser console: Uncaught (in promise) TypeError: (0 , _wordpress_hooks__WEBPACK_IMPORTED_MODULE_2__.applyFiltersAsync) is not a function.

So I don't understand how the buttons are displayed for you. For me it's not being displayed. Maybe there is some other code adding them for you given a specific condition?

Copy link
Collaborator Author

@donnapep donnapep Nov 6, 2024

Choose a reason for hiding this comment

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

It's in @wordpress/hooks, so you'll need do reinstall npm packages as I updated the version.

Updated the testing instructions.

Copy link
Contributor

Choose a reason for hiding this comment

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

Even with that it's not finding the function for me. 🤔
Are you using a WP beta version?

The @wordpress/hooks is not bundled in our scripts. It's imported directly from WP because of the dependency extraction plugin. I checked that the function was added 1 month ago, so we probably shouldn't use it.

BTW, the link I used in my previous comment was the Gutenberg repo in the Automattic organization instead of the WordPress organization. That's the reason it wasn't there. :P

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Are you using a WP beta version?

Oof, yes I am. I wasn't able to downgrade in the Beta Tester plugin so had to stay on 6.7. Back to square one then I guess. 😄 Thanks for taking a look!

'senseiStudentActionMenuControls',
defaultControls,
setAction,
setModalOpen
);

setControls( response );
}

getMenuControls();
}, [ defaultControls ] );

const addToCourse = () => {
setAction( 'add' );
Expand Down
Loading
Loading