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 all 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
127 changes: 73 additions & 54 deletions assets/admin/students/student-action-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
* WordPress dependencies
*/
import { DropdownMenu } from '@wordpress/components';
import { render, useState } from '@wordpress/element';
import {
render,
useEffect,
useState,
useMemo,
useCallback,
} from '@wordpress/element';
import { moreVertical } from '@wordpress/icons';
import { applyFilters } from '@wordpress/hooks';
import { __ } from '@wordpress/i18n';
Expand All @@ -27,68 +33,81 @@ export const StudentActionMenu = ( {
} ) => {
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' ),
onClick: () => addToCourse(),
},
{
title: __( 'Remove from Course', 'sensei-lms' ),
onClick: () => removeFromCourse(),
},
{
title: __( 'Reset Progress', 'sensei-lms' ),
onClick: () => resetProgress(),
},
{
title: __( 'Grading', 'sensei-lms' ),
onClick: () =>
window.open(
`admin.php?page=sensei_grading&view=ungraded&s=${ studentName }`,
'_self'
),
},
];

/**
* Filters controls for the single student action menu.
*
* @since 4.11.0
*
* @param {Array} controls Controls for the single student action menu.
* @param {Function} setAction Selected action.
* @param {Function} setModalOpen The callback to run when the modal is closed.
*
* @return {Array} Filtered controls.
*/
const controls = applyFilters(
'senseiStudentActionMenuControls',
defaultControls,
setAction,
setModalOpen
);

const addToCourse = () => {
const addToCourse = useCallback( () => {
setAction( 'add' );
setModalOpen( true );
};
}, [ setAction, setModalOpen ] );

const removeFromCourse = () => {
const removeFromCourse = useCallback( () => {
setAction( 'remove' );
setModalOpen( true );
};
}, [ setAction, setModalOpen ] );

const resetProgress = () => {
const resetProgress = useCallback( () => {
setAction( 'reset-progress' );
setModalOpen( true );
};
}, [ setAction, setModalOpen ] );

const defaultControls = useMemo(
() => [
{
title: __( 'Add to Course', 'sensei-lms' ),
onClick: () => addToCourse(),
},
{
title: __( 'Remove from Course', 'sensei-lms' ),
onClick: () => removeFromCourse(),
},
{
title: __( 'Reset Progress', 'sensei-lms' ),
onClick: () => resetProgress(),
},
{
title: __( 'Grading', 'sensei-lms' ),
onClick: () =>
window.open(
`admin.php?page=sensei_grading&view=ungraded&s=${ studentName }`,
'_self'
),
},
],
[ studentName, addToCourse, removeFromCourse, resetProgress ]
);

const [ controls, setControls ] = useState( defaultControls );

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

useEffect( () => {
/**
* Filters controls for the single student action menu.
*
* @since 4.11.0
*
* @param {Array} controls Controls for the single student action menu.
* @param {Function} setAction Selected action.
* @param {Function} setModalOpen The callback to run when the modal is closed.
*
* @return {Array} Filtered controls.
*/
applyFilters(
'senseiStudentActionMenuControls',
[ ...defaultControls ],
setAction,
setModalOpen
).then( ( response ) => {
setControls( response );
} );
}, [ defaultControls ] );

const defaultStudentModal = (
<StudentModal
Expand Down
Loading
Loading