@@ -1644,7 +1644,107 @@ function SidebarComponent(props) {
16441644}
16451645
16461646function EditFlow ( props ) {
1647+ const history = useHistory ( )
1648+ const params = useParams ( )
1649+ const location = useLocation ( )
1650+
1651+ const { item, updateItem, isLoading } = useDraftOfAPI ( )
1652+
1653+ const [ flow , setFlow ] = useState ( )
1654+
1655+ useEffect ( ( ) => {
1656+ if ( item && ! flow ) {
1657+ const currentFlow = item . flows . find ( flow => flow . id === params . flowId )
1658+
1659+ if ( currentFlow ) {
1660+ props . setTitle ( {
1661+ value : `Update ${ currentFlow . name } ` ,
1662+ noThumbtack : true ,
1663+ children : < VersionBadge />
1664+ } )
1665+
1666+ setFlow ( {
1667+ ...currentFlow ,
1668+ consumers : item . consumers . reduce ( ( acc , item ) => ( {
1669+ ...acc ,
1670+ [ item . id ] : currentFlow . consumers . includes ( item . id )
1671+ } ) , { } )
1672+ } )
1673+ }
1674+ }
1675+ return ( ) => props . setTitle ( undefined )
1676+ } , [ item ] )
1677+
1678+ const updateFlow = ( ) => {
1679+ return updateItem ( {
1680+ ...item ,
1681+ flows : item . flows . map ( ite => {
1682+ if ( ite . id === params . flowId ) {
1683+ return {
1684+ ...flow ,
1685+ consumers : Object . entries ( flow . consumers ) . filter ( f => f [ 1 ] ) . map ( f => f [ 0 ] )
1686+ }
1687+ } else {
1688+ return ite
1689+ }
1690+ } )
1691+ } )
1692+ . then ( ( ) => historyPush ( history , location , `/apis/${ params . apiId } /flows` ) ) ;
1693+ }
1694+
1695+ if ( isLoading || ! item )
1696+ return < SimpleLoader />
1697+
1698+ return < div style = { {
1699+ maxWidth : MAX_WIDTH ,
1700+ margin : 'auto'
1701+ } } >
1702+ < NgForm
1703+ schema = { FLOW_FORM_SETTINGS . schema ( item ) }
1704+ flow = { FLOW_FORM_SETTINGS . flow }
1705+ value = { flow }
1706+ onChange = { setFlow }
1707+ />
1708+ < Button
1709+ type = "success"
1710+ className = "btn-sm ms-auto d-flex align-items-center"
1711+ onClick = { updateFlow }
1712+ >
1713+ Update < VersionBadge size = "xs" className = "ms-2" />
1714+ </ Button >
1715+ </ div >
1716+ }
16471717
1718+ const FLOW_FORM_SETTINGS = {
1719+ schema : item => ( {
1720+ name : {
1721+ type : 'string' ,
1722+ props : { label : 'Name' } ,
1723+ } ,
1724+ consumers : {
1725+ type : 'form' ,
1726+ label : 'Enabled consumers' ,
1727+ schema : item . consumers . reduce ( ( acc , item ) => ( {
1728+ ...acc ,
1729+ [ item . id ] : {
1730+ type : 'box-bool' ,
1731+ label : item . name ,
1732+ props : {
1733+ description : item . description || item . consumer_kind
1734+ }
1735+ }
1736+ } ) , { } ) ,
1737+ }
1738+ } ) ,
1739+ flow : [
1740+ {
1741+ type : 'group' ,
1742+ name : 'Informations' ,
1743+ collapsable : false ,
1744+ fields : [ "name" ]
1745+ } ,
1746+ 'consumers'
1747+ ]
16481748}
16491749
16501750function NewFlow ( props ) {
@@ -1665,42 +1765,33 @@ function NewFlow(props) {
16651765 const [ flow , setFlow ] = useState ( {
16661766 id : v4 ( ) ,
16671767 name : 'New flow name' ,
1668- plugins : [ ]
1768+ plugins : [ ] ,
1769+ consumers : [ ]
16691770 } )
16701771
16711772 const { item, updateItem, isLoading } = useDraftOfAPI ( )
16721773
16731774 const createFlow = ( ) => {
16741775 return updateItem ( {
16751776 ...item ,
1676- flows : [ ...item . flows , flow ]
1777+ flows : [ ...item . flows , {
1778+ ...flow ,
1779+ consumers : Object . entries ( flow . consumers ) . filter ( f => f [ 1 ] ) . map ( f => f [ 0 ] )
1780+ } ]
16771781 } )
16781782 . then ( ( ) => historyPush ( history , location , `/apis/${ params . apiId } /flows/${ flow . id } ` ) ) ;
16791783 }
16801784
1681- if ( isLoading )
1785+ if ( isLoading || ! item )
16821786 return < SimpleLoader />
16831787
1684- return < >
1685- < Form
1686- schema = { {
1687- name : {
1688- type : 'string' ,
1689- props : { label : 'Name' } ,
1690- } ,
1691- consumers : {
1692- type : 'select' ,
1693- props : {
1694- label : 'Consumers' ,
1695- } ,
1696- options : item . consumers ,
1697- optionsTransformer : {
1698- label : 'name' ,
1699- value : 'id'
1700- }
1701- }
1702- } }
1703- flow = { [ "name" , "consumers" ] }
1788+ return < div style = { {
1789+ maxWidth : MAX_WIDTH ,
1790+ margin : 'auto'
1791+ } } >
1792+ < NgForm
1793+ schema = { FLOW_FORM_SETTINGS . schema ( item ) }
1794+ flow = { FLOW_FORM_SETTINGS . flow }
17041795 value = { flow }
17051796 onChange = { setFlow }
17061797 />
@@ -1711,7 +1802,7 @@ function NewFlow(props) {
17111802 >
17121803 Create < VersionBadge size = "xs" className = "ms-2" />
17131804 </ Button >
1714- </ >
1805+ </ div >
17151806}
17161807
17171808function NewAPI ( props ) {
0 commit comments