1- import React , { Reducer , createContext , useContext , useEffect , useReducer , useState } from 'react'
1+ import React , { Reducer , createContext , useContext , useEffect , useReducer , useRef , useState } from 'react'
22import { useHistory , useParams } from 'react-router'
33import { toast } from 'react-toastify'
44import {
@@ -53,11 +53,13 @@ import { SaveConfirmationDialog, SuccessToastBody } from './DeploymentTemplateVi
5353import { deploymentConfigReducer , initDeploymentConfigState } from './DeploymentConfigReducer'
5454import DeploymentTemplateReadOnlyEditorView from './DeploymentTemplateView/DeploymentTemplateReadOnlyEditorView'
5555import CodeEditor from '../CodeEditor/CodeEditor'
56+ import * as jsonpatch from 'fast-json-patch'
5657const DeploymentTemplateLockedDiff = importComponentFromFELibrary ( 'DeploymentTemplateLockedDiff' )
5758const ConfigToolbar = importComponentFromFELibrary ( 'ConfigToolbar' , DeploymentConfigToolbar )
5859const SaveChangesModal = importComponentFromFELibrary ( 'SaveChangesModal' )
5960const DraftComments = importComponentFromFELibrary ( 'DraftComments' )
6061const getDraftByResourceName = importComponentFromFELibrary ( 'getDraftByResourceName' , null , 'function' )
62+ const applyPatches = importComponentFromFELibrary ( 'applyPatches' , null , 'function' )
6163
6264export const DeploymentConfigContext = createContext < DeploymentConfigContextType > ( null )
6365
@@ -87,8 +89,12 @@ export default function DeploymentConfig({
8789 )
8890 const [ obj , , , error ] = useJsonYaml ( state . tempFormData , 4 , 'yaml' , true )
8991 const [ , grafanaModuleStatus ] = useAsync ( ( ) => getModuleInfo ( ModuleNameMap . GRAFANA ) , [ appId ] )
92+ const [ hideLockedKeys , setHideLockedKeys ] = useState ( false )
93+ const hideLockKeysToggled = useRef ( false )
94+
9095 const readOnlyPublishedMode = state . selectedTabIndex === 1 && isProtected && ! ! state . latestDraft
9196 const baseDeploymentAbortController = new AbortController ( )
97+ const removedPatches = useRef < Array < jsonpatch . Operation > > ( [ ] )
9298
9399 const setIsValues = ( value : boolean ) => {
94100 dispatch ( {
@@ -208,6 +214,12 @@ export default function DeploymentConfig({
208214 payload : false ,
209215 } )
210216 } )
217+ . finally ( ( ) => {
218+ dispatch ( {
219+ type : DeploymentConfigStateActionTypes . loading ,
220+ payload : false ,
221+ } )
222+ } )
211223 }
212224
213225 const fetchAllDrafts = ( chartRefsData ) => {
@@ -359,6 +371,16 @@ export default function DeploymentConfig({
359371 } )
360372 }
361373 }
374+ const reload = ( ) => {
375+ dispatch ( {
376+ type : DeploymentConfigStateActionTypes . loading ,
377+ payload : {
378+ loading : true ,
379+ } ,
380+ } )
381+ setHideLockedKeys ( false )
382+ initialise ( )
383+ }
362384
363385 async function fetchDeploymentTemplate ( ) {
364386 dispatch ( {
@@ -575,6 +597,7 @@ export default function DeploymentConfig({
575597 } )
576598 saveEligibleChangesCb && closeLockedDiffDrawerWithChildModal ( )
577599 state . showConfirmation && handleConfirmationDialog ( false )
600+ setHideLockedKeys ( false )
578601 }
579602 }
580603
@@ -638,6 +661,7 @@ export default function DeploymentConfig({
638661 openComparison : state . showReadme && state . selectedTabIndex === 2 ,
639662 } ,
640663 } )
664+ hideLockKeysToggled . current = true
641665 }
642666
643667 const handleComparisonClick = ( ) => {
@@ -684,6 +708,8 @@ export default function DeploymentConfig({
684708
685709 const handleTabSelection = ( index : number ) => {
686710 if ( state . unableToParseYaml ) return
711+ //setting true to update codeditor values with current locked keys checkbox value
712+ hideLockKeysToggled . current = true
687713
688714 dispatch ( {
689715 type : DeploymentConfigStateActionTypes . selectedTabIndex ,
@@ -760,16 +786,19 @@ export default function DeploymentConfig({
760786 const prepareDataToSave = ( skipReadmeAndSchema ?: boolean ) => {
761787 let valuesOverride = obj
762788
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
767797 } else {
768798 // if allowed keys
769799 valuesOverride = getLockedJSON ( lockedOverride , lockedConfigKeysWithLockType . config )
770800 }
771801 }
772-
773802 const requestData = {
774803 ...( state . chartConfig . chartRefId === state . selectedChart . id ? state . chartConfig : { } ) ,
775804 appId : + appId ,
@@ -835,7 +864,11 @@ export default function DeploymentConfig({
835864 if ( isCompareAndApprovalState ) {
836865 result = await fetchManifestData ( state . draftValues )
837866 } 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 )
839872 }
840873 return result
841874 }
@@ -844,7 +877,14 @@ export default function DeploymentConfig({
844877
845878 const renderEditorComponent = ( ) => {
846879 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+ )
848888 }
849889
850890 if ( state . loadingManifest ) {
@@ -868,6 +908,10 @@ export default function DeploymentConfig({
868908 convertVariables = { state . convertVariables }
869909 setConvertVariables = { setConvertVariables }
870910 groupedData = { state . groupedOptionsData }
911+ hideLockedKeys = { hideLockedKeys }
912+ lockedConfigKeysWithLockType = { lockedConfigKeysWithLockType }
913+ hideLockKeysToggled = { hideLockKeysToggled }
914+ removedPatches = { removedPatches }
871915 />
872916 )
873917 }
@@ -907,7 +951,7 @@ export default function DeploymentConfig({
907951 isCiPipeline = { isCiPipeline }
908952 toggleAppMetrics = { toggleAppMetrics }
909953 isPublishedMode = { readOnlyPublishedMode }
910- reload = { initialise }
954+ reload = { reload }
911955 isValues = { state . isValues }
912956 convertVariables = { state . convertVariables }
913957 isSuperAdmin = { isSuperAdmin }
@@ -953,13 +997,19 @@ export default function DeploymentConfig({
953997 isApprovalPending = { state . latestDraft ?. draftState === 4 }
954998 approvalUsers = { state . latestDraft ?. approvers }
955999 showValuesPostfix = { true }
956- reload = { initialise }
1000+ reload = { reload }
9571001 isValues = { state . isValues }
9581002 setIsValues = { setIsValues }
9591003 convertVariables = { state . convertVariables }
9601004 setConvertVariables = { setConvertVariables }
9611005 componentType = { 3 }
9621006 setShowLockedDiffForApproval = { setShowLockedDiffForApproval }
1007+ setHideLockedKeys = { setHideLockedKeys }
1008+ hideLockedKeys = { hideLockedKeys }
1009+ setLockedConfigKeysWithLockType = { setLockedConfigKeysWithLockType }
1010+ lockedConfigKeysWithLockType = { lockedConfigKeysWithLockType }
1011+ hideLockKeysToggled = { hideLockKeysToggled }
1012+ inValidYaml = { state . unableToParseYaml }
9631013 />
9641014 { renderValuesView ( ) }
9651015 { state . showConfirmation && (
@@ -992,7 +1042,7 @@ export default function DeploymentConfig({
9921042 prepareDataToSave = { prepareDataToSave }
9931043 toggleModal = { toggleSaveChangesModal }
9941044 latestDraft = { state . latestDraft }
995- reload = { initialise }
1045+ reload = { reload }
9961046 closeLockedDiffDrawerWithChildModal = { closeLockedDiffDrawerWithChildModal }
9971047 showAsModal = { ! state . showLockedTemplateDiff }
9981048 saveEligibleChangesCb = { saveEligibleChangesCb }
0 commit comments