@@ -8,21 +8,25 @@ import { AppContext } from '../../common/app-context'
8
8
import { useParams } from 'react-router-dom'
9
9
import { Publication } from '../../../../../shared/api/API'
10
10
import {
11
+ Box ,
11
12
Button ,
12
13
Container ,
13
14
ExpandableSection ,
14
- SpaceBetween
15
+ SpaceBetween ,
15
16
} from '@cloudscape-design/components'
17
+ import { LoadingBar } from '@cloudscape-design/chat-components'
16
18
import { listPublications } from '../../../../../shared/api/graphql/queries'
17
19
import { generateAuthorizedClient } from '../../common/helpers'
18
20
import Newsletter from './newsletter'
19
21
20
- export default function PublicationsTable ( ) {
22
+ export default function PublicationsTable ( ) {
21
23
const appContext = useContext ( AppContext )
22
24
const { newsletterId } = useParams ( )
23
25
const [ publications , setPublications ] = useState < Publication [ ] > ( [ ] )
26
+ const [ publicationsLoading , setPublicationsLoading ] = useState < boolean > ( true )
24
27
25
28
const getPublications = useCallback ( async ( ) => {
29
+ setPublicationsLoading ( true )
26
30
if ( ! appContext ) {
27
31
return
28
32
}
@@ -37,65 +41,80 @@ export default function PublicationsTable () {
37
41
id : newsletterId
38
42
}
39
43
}
40
- } )
44
+ } ) ;
41
45
42
46
if ( result . errors ) {
43
- console . error ( result . errors )
47
+ console . error ( result . errors ) ;
44
48
} else {
45
- setPublications ( [
46
- ...( result . data . listPublications ?. items as Publication [ ] )
47
- ] )
49
+ const sortedPublications = [ ...( result . data . listPublications ?. items as Publication [ ] ) ]
50
+ . sort ( ( a , b ) => {
51
+ const dateA = new Date ( a . createdAt ?? 0 ) ;
52
+ const dateB = new Date ( b . createdAt ?? 0 ) ;
53
+ return dateB . getTime ( ) - dateA . getTime ( ) ;
54
+ } ) ;
55
+ setPublications ( sortedPublications ) ;
56
+ setPublicationsLoading ( false )
48
57
}
58
+
49
59
} , [ appContext , newsletterId ] )
50
60
51
61
useEffect ( ( ) => {
52
62
getPublications ( )
53
63
} , [ getPublications ] )
54
64
return (
55
65
< SpaceBetween direction = "vertical" size = "m" >
56
- { publications . length > 0 ? (
57
- publications . map ( ( publication ) => {
58
- if ( publication . filePath ) {
59
- return (
60
- < ExpandableSection
61
- key = { 'newsletterPublicationSection' + publication . id }
62
- headerText = { publication . createdAt }
63
- headerActions = {
64
- publication . filePath !== null &&
65
- publication . filePath !== undefined &&
66
- publication . filePath . length > 0 ? (
67
- < SpaceBetween size = "s" direction = "horizontal" >
68
- < Button
69
- onClick = { ( ) => {
70
- window . open (
71
- ( publication . filePath + '.html' ) as string ,
72
- '_blank'
73
- )
74
- } }
75
- >
76
- Open in a New Window
77
- </ Button >
78
- </ SpaceBetween >
79
- ) : (
80
- < > </ >
81
- )
82
- }
83
- variant = "stacked"
84
- >
85
- < Container >
86
- < Newsletter
87
- filePath = { publication . filePath }
88
- key = { `rendered-newsletter-${ publication . id } ` }
89
- />
90
- </ Container >
91
- </ ExpandableSection >
92
- )
93
- } else {
94
- return < > </ >
95
- }
96
- } )
66
+ { publicationsLoading ? (
67
+ < SpaceBetween direction = 'vertical' size = 'm' >
68
+ < Box margin = { { bottom : "xs" , left : "l" } } >
69
+ Loading Newsletter Publications
70
+ </ Box >
71
+ < LoadingBar variant = 'gen-ai' />
72
+ </ SpaceBetween >
97
73
) : (
98
- < p > No Publications Available</ p >
74
+ publications . length > 0 ? (
75
+ publications . map ( ( publication ) => {
76
+ if ( publication . filePath ) {
77
+ return (
78
+ < ExpandableSection
79
+ key = { 'newsletterPublicationSection' + publication . id }
80
+ headerText = { publication . createdAt }
81
+ headerActions = {
82
+ publication . filePath !== null &&
83
+ publication . filePath !== undefined &&
84
+ publication . filePath . length > 0 ? (
85
+ < SpaceBetween size = "s" direction = "horizontal" >
86
+ < Button
87
+ onClick = { ( ) => {
88
+ window . open (
89
+ ( publication . filePath + '.html' ) as string ,
90
+ '_blank'
91
+ )
92
+ } }
93
+ >
94
+ Open in a New Window
95
+ </ Button >
96
+ </ SpaceBetween >
97
+ ) : (
98
+ < > </ >
99
+ )
100
+ }
101
+ variant = "stacked"
102
+ >
103
+ < Container >
104
+ < Newsletter
105
+ filePath = { publication . filePath }
106
+ key = { `rendered-newsletter-${ publication . id } ` }
107
+ />
108
+ </ Container >
109
+ </ ExpandableSection >
110
+ )
111
+ } else {
112
+ return < > </ >
113
+ }
114
+ } )
115
+ ) : (
116
+ < p > No Publications Available</ p >
117
+ )
99
118
) }
100
119
</ SpaceBetween >
101
120
)
0 commit comments