diff --git a/app/assets/javascripts/components/settings/views/add_special_user_form.jsx b/app/assets/javascripts/components/settings/views/add_special_user_form.jsx index 4e39466591..932eabc979 100644 --- a/app/assets/javascripts/components/settings/views/add_special_user_form.jsx +++ b/app/assets/javascripts/components/settings/views/add_special_user_form.jsx @@ -1,4 +1,4 @@ -import React, { useState, useEffect } from 'react'; +import React, { useCallback, useState, useEffect, useMemo } from 'react'; import PropTypes from 'prop-types'; import CreatableInput from '../../common/creatable_input.jsx'; import { connect } from 'react-redux'; @@ -22,25 +22,25 @@ const AddSpecialUserForm = ({ submittingNewSpecialUser, upgradeSpecialUser: upgr } }, [submittingNewSpecialUser]); - const reset = () => { + const reset = useCallback(() => { setState(prevState => ({ ...prevState, username: '', confirming: false })); - }; + }, []); - const handleUsernameChange = (_key, value) => { + const handleUsernameChange = useCallback((_key, value) => { setState(prevState => ({ ...prevState, username: value })); - }; + }, []); - const handlePositionChange = (e) => { + const handlePositionChange = useCallback((e) => { const enabled = !!e.value; setState(prevState => ({ ...prevState, position: e.value, enabled, selectedOption: e.value })); - }; + }, []); - const handleConfirm = (e) => { + const handleConfirm = useCallback((e) => { upgradeUser(state.username, state.position); handlePopoverClose(e); - }; + }, [upgradeUser, state.username, state.position, handlePopoverClose]); - const handleSubmit = (e) => { + const handleSubmit = useCallback((e) => { e.preventDefault(); const { username } = state; setState(prevState => ({ @@ -48,22 +48,44 @@ const AddSpecialUserForm = ({ submittingNewSpecialUser, upgradeSpecialUser: upgr confirming: true, confirmMessage: `${I18n.t('settings.special_users.new.confirm_add_special_user')} ${username}?` })); - }; + }, [state.username]); - const renderForm = () => { - const buttonClass = state.enabled ? 'button border' : 'button border disabled'; - const options = [ - { value: 'communications_manager', label: 'communications_manager' }, - { value: 'classroom_program_manager', label: 'classroom_program_manager' }, - { value: 'outreach_manager', label: 'outreach_manager' }, - { value: 'wikipedia_experts', label: 'wikipedia_experts' }, - { value: 'technical_help_staff', label: 'technical_help_staff' }, - { value: 'survey_alerts_recipient', label: 'survey_alerts_recipient' }, - { value: 'backup_account_creator', label: 'backup_account_creator' }, - ]; - return ( - - + const options = useMemo(() => [ + { value: 'communications_manager', label: 'communications_manager' }, + { value: 'classroom_program_manager', label: 'classroom_program_manager' }, + { value: 'outreach_manager', label: 'outreach_manager' }, + { value: 'wikipedia_experts', label: 'wikipedia_experts' }, + { value: 'technical_help_staff', label: 'technical_help_staff' }, + { value: 'survey_alerts_recipient', label: 'survey_alerts_recipient' }, + { value: 'backup_account_creator', label: 'backup_account_creator' }, + ], []); + return ( + + + {state.confirming ? ( + <> + + {submittingNewSpecialUser ? ( +
+ ) : ( + + )} + + ) : (
- + - - - ); - }; - - const renderConfirm = () => { - let buttonContent; - if (submittingNewSpecialUser) { - buttonContent = (
); - } else { - buttonContent = ( - - ); - } - return ( - - - - {buttonContent} - - - ); - }; - - return state.confirming ? renderConfirm() : renderForm(); + )} + + + ); }; AddSpecialUserForm.propTypes = {