1
1
import * as React from 'react' ;
2
+ import { useState } from 'react' ;
2
3
import { useSession } from 'next-auth/react' ;
3
4
import { Label } from '@patternfly/react-core/dist/dynamic/components/Label' ;
4
5
import { PageBreadcrumb } from '@patternfly/react-core/dist/dynamic/components/Page' ;
@@ -27,16 +28,17 @@ import { TextContent } from '@patternfly/react-core/dist/esm/components/Text/Tex
27
28
import OutlinedQuestionCircleIcon from '@patternfly/react-icons/dist/esm/icons/outlined-question-circle-icon' ;
28
29
import { Popover } from '@patternfly/react-core/dist/esm/components/Popover' ;
29
30
import ExternalLinkAltIcon from '@patternfly/react-icons/dist/esm/icons/external-link-alt-icon' ;
31
+ import { Gallery } from '@patternfly/react-core' ;
30
32
import { Modal , ModalVariant } from '@patternfly/react-core/dist/esm/components/Modal/Modal' ;
31
- import { useState } from 'react' ;
32
33
import { Spinner } from '@patternfly/react-core/dist/esm/components/Spinner' ;
33
34
34
35
const Index : React . FunctionComponent = ( ) => {
35
36
const { data : session } = useSession ( ) ;
36
37
const [ pullRequests , setPullRequests ] = React . useState < PullRequest [ ] > ( [ ] ) ;
38
+ const [ error , setError ] = React . useState < string | null > ( null ) ;
39
+ const [ isGridView , setIsGridView ] = React . useState ( false ) ;
37
40
const [ isFirstPullDone , setIsFirstPullDone ] = React . useState < boolean > ( false ) ;
38
41
const [ isLoading , setIsLoading ] = useState < boolean > ( true ) ;
39
- //const [error, setError] = React.useState<string | null>(null);
40
42
const router = useRouter ( ) ;
41
43
42
44
const fetchAndSetPullRequests = React . useCallback ( async ( ) => {
@@ -57,6 +59,7 @@ const Index: React.FunctionComponent = () => {
57
59
58
60
setPullRequests ( sortedPRs ) ;
59
61
} catch ( error ) {
62
+ setError ( 'Failed to fetch pull requests.' ) ;
60
63
console . log ( 'Failed to fetch pull requests.' + error ) ;
61
64
}
62
65
setIsFirstPullDone ( true ) ;
@@ -81,6 +84,14 @@ const Index: React.FunctionComponent = () => {
81
84
}
82
85
} ;
83
86
87
+ const handleListViewClick = ( ) => {
88
+ setIsGridView ( false ) ;
89
+ } ;
90
+
91
+ const handleGridViewClick = ( ) => {
92
+ setIsGridView ( true ) ;
93
+ } ;
94
+
84
95
const handleOnClose = ( ) => {
85
96
setIsLoading ( false ) ;
86
97
} ;
@@ -119,7 +130,20 @@ const Index: React.FunctionComponent = () => {
119
130
</ TextContent >
120
131
</ PageSection >
121
132
< PageSection >
122
- < div style = { { marginBottom : '20px' } } />
133
+ { /* Only show toggle buttons if there are pull requests */ }
134
+ { pullRequests . length > 0 && (
135
+ < div style = { { marginBottom : '20px' } } >
136
+ < Button variant = { isGridView ? 'secondary' : 'primary' } onClick = { handleListViewClick } style = { { marginRight : '10px' } } >
137
+ List View
138
+ </ Button >
139
+ < Button variant = { isGridView ? 'primary' : 'secondary' } onClick = { handleGridViewClick } >
140
+ Grid View
141
+ </ Button >
142
+ </ div >
143
+ ) }
144
+ { /* Error message */ }
145
+ { error && < div > { error } </ div > }
146
+ { /* Loading modal */ }
123
147
{ ! isFirstPullDone && (
124
148
< Modal variant = { ModalVariant . small } title = "Retrieving your submissions" isOpen = { isLoading } onClose = { ( ) => handleOnClose ( ) } >
125
149
< div >
@@ -128,6 +152,7 @@ const Index: React.FunctionComponent = () => {
128
152
</ div >
129
153
</ Modal >
130
154
) }
155
+ { /* Content when no pull requests */ }
131
156
{ isFirstPullDone && pullRequests . length === 0 ? (
132
157
< EmptyState >
133
158
< EmptyStateHeader
@@ -172,7 +197,43 @@ const Index: React.FunctionComponent = () => {
172
197
</ EmptyStateActions >
173
198
</ EmptyStateFooter >
174
199
</ EmptyState >
200
+ ) : isGridView ? (
201
+ // Grid view
202
+ < Gallery hasGutter >
203
+ { pullRequests . map ( ( pr ) => (
204
+ < Card key = { pr . number } >
205
+ < CardTitle > { pr . title } </ CardTitle >
206
+ < CardBody >
207
+ < Flex justifyContent = { { default : 'justifyContentSpaceBetween' } } >
208
+ < FlexItem > State: { pr . state } </ FlexItem >
209
+ < FlexItem > Created At: { new Date ( pr . created_at ) . toLocaleString ( ) } </ FlexItem >
210
+ < FlexItem > Updated At: { new Date ( pr . updated_at ) . toLocaleString ( ) } </ FlexItem >
211
+ < FlexItem >
212
+ { pr . labels . map ( ( label ) => (
213
+ < Label key = { label . name } color = "blue" style = { { marginRight : '5px' } } >
214
+ { label . name }
215
+ </ Label >
216
+ ) ) }
217
+ </ FlexItem >
218
+ < FlexItem alignSelf = { { default : 'alignSelfFlexEnd' } } flex = { { default : 'flexNone' } } >
219
+ < Button variant = "secondary" component = "a" href = { pr . html_url } target = "_blank" rel = "noopener noreferrer" >
220
+ View PR
221
+ </ Button >
222
+ </ FlexItem >
223
+ < FlexItem alignSelf = { { default : 'alignSelfFlexEnd' } } flex = { { default : 'flexNone' } } >
224
+ { pr . state === 'open' && (
225
+ < Button variant = "primary" onClick = { ( ) => handleEditClick ( pr ) } >
226
+ Edit
227
+ </ Button >
228
+ ) }
229
+ </ FlexItem >
230
+ </ Flex >
231
+ </ CardBody >
232
+ </ Card >
233
+ ) ) }
234
+ </ Gallery >
175
235
) : (
236
+ // List view (Stack)
176
237
< Stack hasGutter >
177
238
{ pullRequests . map ( ( pr ) => (
178
239
< StackItem key = { pr . number } >
0 commit comments