11import { Page , SlidingPanel } from 'argo-ui' ;
22import * as React from 'react' ;
3+ import { useContext , useEffect , useState } from 'react' ;
34import { Link , RouteComponentProps } from 'react-router-dom' ;
45import * as models from '../../../../models' ;
56import { uiUrl } from '../../../shared/base' ;
6- import { BasePage } from '../../../shared/components/base-page' ;
77import { ErrorNotice } from '../../../shared/components/error-notice' ;
88import { ExampleManifests } from '../../../shared/components/example-manifests' ;
99import { InfoIcon } from '../../../shared/components/fa-icons' ;
1010import { Loading } from '../../../shared/components/loading' ;
1111import { Timestamp } from '../../../shared/components/timestamp' ;
1212import { ZeroState } from '../../../shared/components/zero-state' ;
13- import { Consumer } from '../../../shared/context' ;
13+ import { Context } from '../../../shared/context' ;
14+ import { useQueryParams } from '../../../shared/use-query-params' ;
15+
1416import { Footnote } from '../../../shared/footnote' ;
1517import { services } from '../../../shared/services' ;
1618import { ClusterWorkflowTemplateCreator } from '../cluster-workflow-template-creator' ;
17-
1819require ( './cluster-workflow-template-list.scss' ) ;
1920
20- interface State {
21- templates ?: models . ClusterWorkflowTemplate [ ] ;
22- error ?: Error ;
23- }
24-
25- export class ClusterWorkflowTemplateList extends BasePage < RouteComponentProps < any > , State > {
26- private get sidePanel ( ) {
27- return this . queryParam ( 'sidePanel' ) ;
28- }
21+ export const ClusterWorkflowTemplateList = ( { history, location} : RouteComponentProps < any > ) => {
22+ const { navigation} = useContext ( Context ) ;
23+ const queryParams = new URLSearchParams ( location . search ) ;
24+ const [ templates , setTemplates ] = useState < models . ClusterWorkflowTemplate [ ] > ( ) ;
25+ const [ error , setError ] = useState < Error > ( ) ;
26+ const [ sidePanel , setSidePanel ] = useState ( queryParams . get ( 'sidePanel' ) ) ;
2927
30- private set sidePanel ( sidePanel ) {
31- this . setQueryParams ( { sidePanel} ) ;
32- }
28+ const fetchClusterWorkflowTemplates = ( ) => {
29+ services . clusterWorkflowTemplate
30+ . list ( )
31+ . then ( retrievedTemplates => setTemplates ( retrievedTemplates ) )
32+ . then ( ( ) => setError ( null ) )
33+ . catch ( setError ) ;
34+ } ;
3335
34- constructor ( props : RouteComponentProps < any > , context : any ) {
35- super ( props , context ) ;
36- this . state = { } ;
37- }
36+ useEffect (
37+ useQueryParams ( history , p => {
38+ setSidePanel ( p . get ( 'sidePanel' ) ) ;
39+ } ) ,
40+ [ history ]
41+ ) ;
3842
39- public componentDidMount ( ) : void {
40- this . fetchClusterWorkflowTemplates ( ) ;
43+ useEffect ( ( ) => {
44+ fetchClusterWorkflowTemplates ( ) ;
4145 services . info . collectEvent ( 'openedClusterWorkflowTemplateList' ) . then ( ) ;
42- }
43-
44- public render ( ) {
45- return (
46- < Consumer >
47- { ctx => (
48- < Page
49- title = 'Cluster Workflow Templates'
50- toolbar = { {
51- breadcrumbs : [ { title : 'Cluster Workflow Templates' , path : uiUrl ( 'cluster-workflow-templates' ) } ] ,
52- actionMenu : {
53- items : [
54- {
55- title : 'Create New Cluster Workflow Template' ,
56- iconClassName : 'fa fa-plus' ,
57- action : ( ) => ( this . sidePanel = 'new' )
58- }
59- ]
60- }
61- } } >
62- { this . renderTemplates ( ) }
63- < SlidingPanel isShown = { this . sidePanel !== null } onClose = { ( ) => ( this . sidePanel = null ) } >
64- < ClusterWorkflowTemplateCreator onCreate = { wf => ctx . navigation . goto ( uiUrl ( `cluster-workflow-templates/${ wf . metadata . name } ` ) ) } />
65- </ SlidingPanel >
66- </ Page >
67- ) }
68- </ Consumer >
69- ) ;
70- }
71-
72- private fetchClusterWorkflowTemplates ( ) : void {
73- services . clusterWorkflowTemplate
74- . list ( )
75- . then ( templates => this . setState ( { error : null , templates} ) )
76- . catch ( error => this . setState ( { error} ) ) ;
77- }
46+ } , [ ] ) ;
7847
79- private renderTemplates ( ) {
80- if ( this . state . error ) {
81- return < ErrorNotice error = { this . state . error } /> ;
48+ const renderTemplates = ( ) => {
49+ if ( error ) {
50+ return < ErrorNotice error = { error } /> ;
8251 }
83- if ( ! this . state . templates ) {
52+ if ( ! templates ) {
8453 return < Loading /> ;
8554 }
8655 const learnMore = < a href = 'https://argoproj.github.io/argo-workflows/cluster-workflow-templates/' > Learn more</ a > ;
87- if ( this . state . templates . length === 0 ) {
56+ if ( templates . length === 0 ) {
8857 return (
8958 < ZeroState title = 'No cluster workflow templates' >
9059 < p > You can create new templates here or using the CLI.</ p >
@@ -102,7 +71,7 @@ export class ClusterWorkflowTemplateList extends BasePage<RouteComponentProps<an
10271 < div className = 'columns small-5' > NAME</ div >
10372 < div className = 'columns small-3' > CREATED</ div >
10473 </ div >
105- { this . state . templates . map ( t => (
74+ { templates . map ( t => (
10675 < Link className = 'row argo-table-list__row' key = { t . metadata . uid } to = { uiUrl ( `cluster-workflow-templates/${ t . metadata . name } ` ) } >
10776 < div className = 'columns small-1' >
10877 < i className = 'fa fa-clone' />
@@ -119,5 +88,27 @@ export class ClusterWorkflowTemplateList extends BasePage<RouteComponentProps<an
11988 </ Footnote >
12089 </ >
12190 ) ;
122- }
123- }
91+ } ;
92+
93+ return (
94+ < Page
95+ title = 'Cluster Workflow Templates'
96+ toolbar = { {
97+ breadcrumbs : [ { title : 'Cluster Workflow Templates' , path : uiUrl ( 'cluster-workflow-templates' ) } ] ,
98+ actionMenu : {
99+ items : [
100+ {
101+ title : 'Create New Cluster Workflow Template' ,
102+ iconClassName : 'fa fa-plus' ,
103+ action : ( ) => setSidePanel ( 'new' )
104+ }
105+ ]
106+ }
107+ } } >
108+ { renderTemplates ( ) }
109+ < SlidingPanel isShown = { sidePanel !== null } onClose = { ( ) => setSidePanel ( null ) } >
110+ < ClusterWorkflowTemplateCreator onCreate = { wf => navigation . goto ( uiUrl ( `cluster-workflow-templates/${ wf . metadata . name } ` ) ) } />
111+ </ SlidingPanel >
112+ </ Page >
113+ ) ;
114+ } ;
0 commit comments