11import * as React from 'react' ;
22import { Content , ContentVariants } from '@patternfly/react-core' ;
3+ import type { FormikHelpers , FormikValues } from 'formik' ;
34import { Formik } from 'formik' ;
4- import * as _ from 'lodash' ;
55import { useTranslation , Trans } from 'react-i18next' ;
66import { Link } from 'react-router-dom-v5-compat' ;
77import {
@@ -24,8 +24,10 @@ import {
2424 getRolesWithMultipleSubjects ,
2525 getRolesToUpdate ,
2626} from './project-access-form-submit-utils' ;
27- import { getUserRoleBindings , Roles } from './project-access-form-utils' ;
28- import { Verb , UserRoleBinding } from './project-access-form-utils-types' ;
27+ import type { Roles } from './project-access-form-utils' ;
28+ import { getUserRoleBindings } from './project-access-form-utils' ;
29+ import type { UserRoleBinding } from './project-access-form-utils-types' ;
30+ import { Verb } from './project-access-form-utils-types' ;
2931import { validationSchema } from './project-access-form-validation-utils' ;
3032import ProjectAccessForm from './ProjectAccessForm' ;
3133
@@ -43,21 +45,31 @@ const ProjectAccess: React.FC<ProjectAccessProps> = ({
4345 fullFormView,
4446} ) => {
4547 const { t } = useTranslation ( ) ;
46- if ( ( ! roleBindings . loaded && _ . isEmpty ( roleBindings . loadError ) ) || ! roles . loaded ) {
47- return < LoadingBox /> ;
48- }
4948
50- const userRoleBindings : UserRoleBinding [ ] = getUserRoleBindings (
51- roleBindings . data ,
52- Object . keys ( roles . data ) ,
53- namespace ,
49+ const userRoleBindings : UserRoleBinding [ ] = React . useMemo (
50+ ( ) =>
51+ roleBindings ?. loaded
52+ ? getUserRoleBindings ( roleBindings . data , Object . keys ( roles . data ) , namespace )
53+ : [ ] ,
54+ [ roleBindings , roles . data , namespace ] ,
5455 ) ;
5556
57+ const memoizedRoleBindings = React . useMemo ( ( ) => ( { projectAccess : userRoleBindings } ) , [
58+ userRoleBindings ,
59+ ] ) ;
60+
5661 const rbacURL = getDocumentationURL ( documentationURLs . usingRBAC ) ;
5762
58- const initialValues = {
59- projectAccess : roleBindings . loaded && userRoleBindings ,
60- } ;
63+ const initialValues = React . useMemo (
64+ ( ) => ( {
65+ projectAccess : roleBindings ?. loaded && userRoleBindings ,
66+ } ) ,
67+ [ roleBindings ?. loaded , userRoleBindings ] ,
68+ ) ;
69+
70+ if ( ( ! roleBindings ?. loaded && ! roleBindings ?. loadError ) || ! roles . loaded ) {
71+ return < LoadingBox /> ;
72+ }
6173
6274 const handleSubmit = ( values , actions ) => {
6375 let newRoles = getNewRoles ( initialValues . projectAccess , values . projectAccess ) ;
@@ -110,8 +122,9 @@ const ProjectAccess: React.FC<ProjectAccessProps> = ({
110122 } ) ;
111123 } ;
112124
113- const handleReset = ( values , actions ) => {
114- actions . resetForm ( { status : { success : null } , values : initialValues } ) ;
125+ const handleReset = ( _values : FormikValues , actions : FormikHelpers < FormikValues > ) => {
126+ actions . setStatus ( { success : null } ) ;
127+ actions . setValues ( initialValues ) ;
115128 } ;
116129
117130 const projectAccessForm = (
@@ -156,7 +169,7 @@ const ProjectAccess: React.FC<ProjectAccessProps> = ({
156169 < ProjectAccessForm
157170 { ...formikProps }
158171 roles = { roles . data }
159- roleBindings = { initialValues }
172+ roleBindings = { memoizedRoleBindings }
160173 onCancel = { fullFormView ? history . goBack : null }
161174 />
162175 ) }
0 commit comments