11import  type  {  FC  }  from  'react' 
22import  {  useTranslation  }  from  'react-i18next' 
33import  {  useNavigate  }  from  'react-router-dom' 
4+ import  type  {  Node  }  from  '@xyflow/react' 
45import  type  {  UseMutateAsyncFunction  }  from  '@tanstack/react-query' 
6+ import  type  {  UseToastOptions  }  from  '@chakra-ui/react' 
57import  {  Button ,  Icon ,  useToast  }  from  '@chakra-ui/react' 
68import  {  MdPublishedWithChanges  }  from  'react-icons/md' 
79
@@ -19,7 +21,7 @@ import { usePolicyChecksStore } from '@datahub/hooks/usePolicyChecksStore.ts'
1921import  {  usePolicyGuards  }  from  '@datahub/hooks/usePolicyGuards.ts' 
2022
2123import  {  dataHubToastOption  }  from  '@datahub/utils/toast.utils.ts' 
22- import  type  {  DryRunResults ,  ResourceState  }  from  '@datahub/types.ts' 
24+ import  type  {  DryRunResults ,  ResourceState ,   SchemaData  }  from  '@datahub/types.ts' 
2325import  {  DataHubNodeType ,  ResourceWorkingVersion ,  DesignerStatus  }  from  '@datahub/types.ts' 
2426
2527// Should be PolicySchema | Script | DataPolicy | BehaviorPolicy 
@@ -50,7 +52,7 @@ const resourceReducer =
5052
5153export  const  ToolbarPublish : FC  =  ( )  =>  { 
5254  const  {  t }  =  useTranslation ( 'datahub' ) 
53-   const  {  status }  =  useDataHubDraftStore ( ) 
55+   const  {  status,  nodes ,  onUpdateNodes  }  =  useDataHubDraftStore ( ) 
5456  const  {  report,  node : selectedNode ,  setNode,  reset }  =  usePolicyChecksStore ( ) 
5557  const  createSchema  =  useCreateSchema ( ) 
5658  const  createScript  =  useCreateScript ( ) 
@@ -64,31 +66,62 @@ export const ToolbarPublish: FC = () => {
6466
6567  const  isValid  =  ! ! report  &&  report . length  >=  1  &&  report ?. every ( ( e )  =>  ! e . error ) 
6668
69+   // TODO[NVL] The routine doesn't change title/description based on the number of resources published 
70+   const  manageToast  =  ( id : string ,  conf : UseToastOptions )  =>  { 
71+     const  {  id : _ ,  ...cleanConfig  }  =  conf 
72+     if  ( ! toast . isActive ( id ) )  toast ( {  ...cleanConfig ,  id } ) 
73+     else  toast . update ( id ,  cleanConfig ) 
74+   } 
75+ 
6776  const  toastInternalError  =  ( message : string )  =>  { 
68-     toast ( { 
77+     manageToast ( `publish-internal- ${ selectedNode ?. type } ` ,   { 
6978      ...dataHubToastOption , 
7079      title : t ( 'publish.internal.title' ,  {  source : selectedNode ?. type  } ) , 
7180      description : message , 
7281      status : 'error' , 
73-       id : 'publish-internal' , 
7482    } ) 
7583  } 
7684
7785  const  reportMutation  =  ( promise : Promise < unknown > ,  type ?: string )  =>  { 
7886    promise 
7987      . then ( ( )  =>  { 
80-         toast ( { 
88+         manageToast ( `publish-success- ${ type   ||   selectedNode ?. type } ` ,   { 
8189          ...dataHubToastOption , 
8290          title : t ( 'publish.success.title' ,  {  source : type  ||  selectedNode ?. type  } ) , 
8391          description : t ( 'publish.success.description' ,  {  source : type  ||  selectedNode ?. type ,  context : status  } ) , 
8492          status : 'success' , 
85-           id : 'publish-success' , 
8693        } ) 
8794      } ) 
8895      . catch ( ( )  =>  { } ) 
8996    return  promise 
9097  } 
9198
99+   /** 
100+    * This routine is called on successful publish of a resource node (script or schema), in order to update a draft node to its published version. 
101+    * In case the main policy fails, the graph will be holding the newly published resource nodes, but the main policy will not be updated. 
102+    * TODO[NVL] This is so much a hack, but we need to update the draft resource nodes and we lost the link between nodes and mutation requests 
103+    *  The whole publish process needs to be refactored 
104+    */ 
105+   const  updateDraftResourceNodes  =  ( request : Mutate < PolicySchema >  |  Mutate < Script > )  =>  ( value : unknown )  =>  { 
106+     if  ( request . type  ===  DataHubNodeType . SCHEMA )  { 
107+       const  draftSchemaNodes  =  nodes . filter ( 
108+         ( node ) : node  is Node < SchemaData >  => 
109+           node . type  ===  DataHubNodeType . SCHEMA  && 
110+           node . data . name  ===  request . payload . id  && 
111+           // if this is a draft, versions MUST be 1 AND ResourceWorkingVersion.DRAFT 
112+           node . data . version  ===  ResourceWorkingVersion . DRAFT  && 
113+           ( value  as  PolicySchema ) . version  ===  1 
114+       ) 
115+ 
116+       draftSchemaNodes . forEach ( ( node )  =>  { 
117+         onUpdateNodes < SchemaData > ( node . id ,  { 
118+           ...node . data , 
119+           version : 1 , 
120+         } ) 
121+       } ) 
122+     } 
123+   } 
124+ 
92125  const  publishResources  =  ( resources ?: DryRunResults < never > [ ] )  =>  { 
93126    const  allSchemas  = 
94127      resources 
@@ -107,7 +140,7 @@ export const ToolbarPublish: FC = () => {
107140      } ) )  ||  [ ] 
108141
109142    return  [ ...allSchemas ,  ...allScripts ] . map ( ( request )  => 
110-       reportMutation ( request . mutation ( request . payload ) ,  request . type ) 
143+       reportMutation ( request . mutation ( request . payload ) ,  request . type ) . then ( updateDraftResourceNodes ( request ) ) 
111144    ) 
112145  } 
113146
@@ -184,12 +217,12 @@ export const ToolbarPublish: FC = () => {
184217        let  message 
185218        if  ( error  instanceof  Error )  message  =  error . message 
186219        else  message  =  String ( error ) 
187-         return  toast ( { 
220+ 
221+         manageToast ( `publish-error-${ selectedNode ?. type }  ` ,  { 
188222          ...dataHubToastOption , 
189223          title : t ( 'publish.error.title' ,  {  source : selectedNode ?. type  } ) , 
190224          description : message . toString ( ) , 
191225          status : 'error' , 
192-           id : 'publish-error' , 
193226        } ) 
194227      } ) 
195228  } 
0 commit comments