11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT license.
33
4- import React , { useEffect , useState } from 'react' ;
4+ import React , { useEffect , useRef , useState } from 'react' ;
55import { useDispatch , useSelector } from 'react-redux' ;
66import {
77 IProcessedStyleSet ,
@@ -85,7 +85,6 @@ export const ManageMembershipBase: React.FunctionComponent<IManageMembershipProp
8585
8686 const dispatch = useDispatch < AppDispatch > ( ) ;
8787 useEffect ( ( ) => {
88- dispatch ( resetManageMembership ( ) ) ;
8988 dispatch ( setPagingBarVisible ( false ) ) ;
9089 } , [ dispatch ] ) ;
9190
@@ -99,38 +98,35 @@ export const ManageMembershipBase: React.FunctionComponent<IManageMembershipProp
9998 const isJobWriter = useSelector ( selectIsJobWriter )
10099
101100 // Existing job
102- const jobDetails = useSelector ( selectSelectedJobDetails ) ;
101+ const jobDetailsRef = useRef ( useSelector ( selectSelectedJobDetails ) ) ;
103102 const isLoading = useSelector ( selectSelectedJobLoading ) ;
104103
105104 useEffect ( ( ) => {
106- let editingExistingJob = ! ! locationState . jobId ;
107- if ( editingExistingJob && isJobWriter ) {
108- editingExistingJob = true ;
109- dispatch ( setIsEditingExistingJob ( editingExistingJob ) ) ;
110- }
111- else {
112- editingExistingJob = false ;
113- dispatch ( setIsEditingExistingJob ( editingExistingJob ) ) ;
105+ let editingExistingJob = ! ! locationState ?. jobId && isJobWriter ;
106+ dispatch ( setIsEditingExistingJob ( editingExistingJob ) ) ;
107+
108+ if ( ! editingExistingJob ) {
109+ jobDetailsRef . current = undefined ;
114110 }
115111
116- if ( locationState . currentStep ) {
112+ if ( locationState ? .currentStep ) {
117113 dispatch ( setCurrentStep ( locationState . currentStep ) ) ;
118114 }
119115
120- if ( locationState . jobId ) {
116+ if ( locationState ? .jobId ) {
121117 dispatch ( fetchJobDetails ( {
122118 syncJobId : locationState . jobId
123119 } ) ) ;
124120 } else {
125121 dispatch ( resetManageMembership ( ) ) ;
126122 }
127- } , [ dispatch , locationState ] ) ;
123+ } , [ dispatch , locationState , isJobWriter ] ) ;
128124
129125 useEffect ( ( ) => {
130- if ( jobDetails ) {
131- dispatch ( setJobDetailsForExistingJob ( jobDetails ) ) ;
126+ if ( jobDetailsRef . current ) {
127+ dispatch ( setJobDetailsForExistingJob ( jobDetailsRef . current ) ) ;
132128 }
133- } , [ dispatch , jobDetails ] ) ;
129+ } , [ dispatch , jobDetailsRef . current ] ) ;
134130
135131 const isAdvancedQueryValid = useSelector ( manageMembershipisAdvancedQueryValid ) ;
136132 const allSourcePartsValid = useSelector ( areAllSourcePartsValid ) ;
0 commit comments