1
- import React , { Reducer , createContext , useContext , useEffect , useReducer , useState } from 'react'
1
+ import React , { Reducer , createContext , useContext , useEffect , useReducer , useRef , useState } from 'react'
2
2
import { useHistory , useParams } from 'react-router'
3
3
import { toast } from 'react-toastify'
4
4
import {
@@ -53,11 +53,13 @@ import { SaveConfirmationDialog, SuccessToastBody } from './DeploymentTemplateVi
53
53
import { deploymentConfigReducer , initDeploymentConfigState } from './DeploymentConfigReducer'
54
54
import DeploymentTemplateReadOnlyEditorView from './DeploymentTemplateView/DeploymentTemplateReadOnlyEditorView'
55
55
import CodeEditor from '../CodeEditor/CodeEditor'
56
+ import * as jsonpatch from 'fast-json-patch'
56
57
const DeploymentTemplateLockedDiff = importComponentFromFELibrary ( 'DeploymentTemplateLockedDiff' )
57
58
const ConfigToolbar = importComponentFromFELibrary ( 'ConfigToolbar' , DeploymentConfigToolbar )
58
59
const SaveChangesModal = importComponentFromFELibrary ( 'SaveChangesModal' )
59
60
const DraftComments = importComponentFromFELibrary ( 'DraftComments' )
60
61
const getDraftByResourceName = importComponentFromFELibrary ( 'getDraftByResourceName' , null , 'function' )
62
+ const applyPatches = importComponentFromFELibrary ( 'applyPatches' , null , 'function' )
61
63
62
64
export const DeploymentConfigContext = createContext < DeploymentConfigContextType > ( null )
63
65
@@ -87,8 +89,12 @@ export default function DeploymentConfig({
87
89
)
88
90
const [ obj , , , error ] = useJsonYaml ( state . tempFormData , 4 , 'yaml' , true )
89
91
const [ , grafanaModuleStatus ] = useAsync ( ( ) => getModuleInfo ( ModuleNameMap . GRAFANA ) , [ appId ] )
92
+ const [ hideLockedKeys , setHideLockedKeys ] = useState ( false )
93
+ const hideLockKeysToggled = useRef ( false )
94
+
90
95
const readOnlyPublishedMode = state . selectedTabIndex === 1 && isProtected && ! ! state . latestDraft
91
96
const baseDeploymentAbortController = new AbortController ( )
97
+ const removedPatches = useRef < Array < jsonpatch . Operation > > ( [ ] )
92
98
93
99
const setIsValues = ( value : boolean ) => {
94
100
dispatch ( {
@@ -208,6 +214,12 @@ export default function DeploymentConfig({
208
214
payload : false ,
209
215
} )
210
216
} )
217
+ . finally ( ( ) => {
218
+ dispatch ( {
219
+ type : DeploymentConfigStateActionTypes . loading ,
220
+ payload : false ,
221
+ } )
222
+ } )
211
223
}
212
224
213
225
const fetchAllDrafts = ( chartRefsData ) => {
@@ -359,6 +371,16 @@ export default function DeploymentConfig({
359
371
} )
360
372
}
361
373
}
374
+ const reload = ( ) => {
375
+ dispatch ( {
376
+ type : DeploymentConfigStateActionTypes . loading ,
377
+ payload : {
378
+ loading : true ,
379
+ } ,
380
+ } )
381
+ setHideLockedKeys ( false )
382
+ initialise ( )
383
+ }
362
384
363
385
async function fetchDeploymentTemplate ( ) {
364
386
dispatch ( {
@@ -575,6 +597,7 @@ export default function DeploymentConfig({
575
597
} )
576
598
saveEligibleChangesCb && closeLockedDiffDrawerWithChildModal ( )
577
599
state . showConfirmation && handleConfirmationDialog ( false )
600
+ setHideLockedKeys ( false )
578
601
}
579
602
}
580
603
@@ -638,6 +661,7 @@ export default function DeploymentConfig({
638
661
openComparison : state . showReadme && state . selectedTabIndex === 2 ,
639
662
} ,
640
663
} )
664
+ hideLockKeysToggled . current = true
641
665
}
642
666
643
667
const handleComparisonClick = ( ) => {
@@ -684,6 +708,8 @@ export default function DeploymentConfig({
684
708
685
709
const handleTabSelection = ( index : number ) => {
686
710
if ( state . unableToParseYaml ) return
711
+ //setting true to update codeditor values with current locked keys checkbox value
712
+ hideLockKeysToggled . current = true
687
713
688
714
dispatch ( {
689
715
type : DeploymentConfigStateActionTypes . selectedTabIndex ,
@@ -760,16 +786,19 @@ export default function DeploymentConfig({
760
786
const prepareDataToSave = ( skipReadmeAndSchema ?: boolean ) => {
761
787
let valuesOverride = obj
762
788
763
- if ( state . showLockedTemplateDiff ) {
764
- // if locked keys
765
- if ( ! lockedConfigKeysWithLockType . allowed ) {
766
- valuesOverride = getUnlockedJSON ( lockedOverride , lockedConfigKeysWithLockType . config )
789
+ if ( applyPatches && hideLockedKeys ) {
790
+ valuesOverride = applyPatches ( valuesOverride , removedPatches . current )
791
+ }
792
+
793
+ if ( state . showLockedTemplateDiff ) {
794
+ // if locked keys
795
+ if ( ! lockedConfigKeysWithLockType . allowed ) {
796
+ valuesOverride = getUnlockedJSON ( lockedOverride , lockedConfigKeysWithLockType . config , true ) . newDocument
767
797
} else {
768
798
// if allowed keys
769
799
valuesOverride = getLockedJSON ( lockedOverride , lockedConfigKeysWithLockType . config )
770
800
}
771
801
}
772
-
773
802
const requestData = {
774
803
...( state . chartConfig . chartRefId === state . selectedChart . id ? state . chartConfig : { } ) ,
775
804
appId : + appId ,
@@ -835,7 +864,11 @@ export default function DeploymentConfig({
835
864
if ( isCompareAndApprovalState ) {
836
865
result = await fetchManifestData ( state . draftValues )
837
866
} else {
838
- result = await fetchManifestData ( state . tempFormData )
867
+ if ( applyPatches && hideLockedKeys ) {
868
+ result = fetchManifestData (
869
+ YAML . stringify ( applyPatches ( YAML . parse ( state . tempFormData ) , removedPatches . current ) ) ,
870
+ )
871
+ } else result = await fetchManifestData ( state . tempFormData )
839
872
}
840
873
return result
841
874
}
@@ -844,7 +877,14 @@ export default function DeploymentConfig({
844
877
845
878
const renderEditorComponent = ( ) => {
846
879
if ( readOnlyPublishedMode && ! state . showReadme ) {
847
- return < DeploymentTemplateReadOnlyEditorView value = { state . publishedState ?. tempFormData } />
880
+ return (
881
+ < DeploymentTemplateReadOnlyEditorView
882
+ value = { state . publishedState ?. tempFormData }
883
+ lockedConfigKeysWithLockType = { lockedConfigKeysWithLockType }
884
+ hideLockedKeys = { hideLockedKeys }
885
+ removedPatches = { removedPatches }
886
+ />
887
+ )
848
888
}
849
889
850
890
if ( state . loadingManifest ) {
@@ -868,6 +908,10 @@ export default function DeploymentConfig({
868
908
convertVariables = { state . convertVariables }
869
909
setConvertVariables = { setConvertVariables }
870
910
groupedData = { state . groupedOptionsData }
911
+ hideLockedKeys = { hideLockedKeys }
912
+ lockedConfigKeysWithLockType = { lockedConfigKeysWithLockType }
913
+ hideLockKeysToggled = { hideLockKeysToggled }
914
+ removedPatches = { removedPatches }
871
915
/>
872
916
)
873
917
}
@@ -907,7 +951,7 @@ export default function DeploymentConfig({
907
951
isCiPipeline = { isCiPipeline }
908
952
toggleAppMetrics = { toggleAppMetrics }
909
953
isPublishedMode = { readOnlyPublishedMode }
910
- reload = { initialise }
954
+ reload = { reload }
911
955
isValues = { state . isValues }
912
956
convertVariables = { state . convertVariables }
913
957
isSuperAdmin = { isSuperAdmin }
@@ -953,13 +997,19 @@ export default function DeploymentConfig({
953
997
isApprovalPending = { state . latestDraft ?. draftState === 4 }
954
998
approvalUsers = { state . latestDraft ?. approvers }
955
999
showValuesPostfix = { true }
956
- reload = { initialise }
1000
+ reload = { reload }
957
1001
isValues = { state . isValues }
958
1002
setIsValues = { setIsValues }
959
1003
convertVariables = { state . convertVariables }
960
1004
setConvertVariables = { setConvertVariables }
961
1005
componentType = { 3 }
962
1006
setShowLockedDiffForApproval = { setShowLockedDiffForApproval }
1007
+ setHideLockedKeys = { setHideLockedKeys }
1008
+ hideLockedKeys = { hideLockedKeys }
1009
+ setLockedConfigKeysWithLockType = { setLockedConfigKeysWithLockType }
1010
+ lockedConfigKeysWithLockType = { lockedConfigKeysWithLockType }
1011
+ hideLockKeysToggled = { hideLockKeysToggled }
1012
+ inValidYaml = { state . unableToParseYaml }
963
1013
/>
964
1014
{ renderValuesView ( ) }
965
1015
{ state . showConfirmation && (
@@ -992,7 +1042,7 @@ export default function DeploymentConfig({
992
1042
prepareDataToSave = { prepareDataToSave }
993
1043
toggleModal = { toggleSaveChangesModal }
994
1044
latestDraft = { state . latestDraft }
995
- reload = { initialise }
1045
+ reload = { reload }
996
1046
closeLockedDiffDrawerWithChildModal = { closeLockedDiffDrawerWithChildModal }
997
1047
showAsModal = { ! state . showLockedTemplateDiff }
998
1048
saveEligibleChangesCb = { saveEligibleChangesCb }
0 commit comments