Skip to content

Commit a395c2d

Browse files
authored
fix: sort data feed articles by date descending (#106)
* feat(ui): sorted Newsletter Publications by created date desc * fix(ui): fixed bug with Article Table Keyword elements not have unique key * chore: resolved react node dep mismatch and added @cloudscape-design/chat-components * fix(ui): added missing loader to Newsletter publications table
1 parent a08c908 commit a395c2d

File tree

5 files changed

+134
-186
lines changed

5 files changed

+134
-186
lines changed

lib/user-interface/genai-newsletter-ui/src/components/data-feeds/article-table.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,16 @@ export default function DataFeedArticleTable () {
5353
return
5454
}
5555
if (result.data.listArticles?.items !== null) {
56-
setArticles(result.data.listArticles?.items as Article[])
56+
const sortedArticles = [...(result.data.listArticles?.items as Article[])]
57+
.sort((a, b) => {
58+
const dateA = new Date(a.createdAt ?? 0);
59+
const dateB = new Date(b.createdAt ?? 0);
60+
return dateB.getTime() - dateA.getTime();
61+
});
62+
setArticles(sortedArticles);
5763
}
5864

65+
5966
setLoading(false)
6067
}, [appContext, dataFeedId])
6168

@@ -101,7 +108,6 @@ export default function DataFeedArticleTable () {
101108
if (flagArticle !== null && flagArticle == 'true' && articleId !== null) {
102109
setLoading(true)
103110
try {
104-
console.log('TRIGGER')
105111
flagDataFeedArticle(articleId, true)
106112
} catch (error) {
107113
console.log(error)

lib/user-interface/genai-newsletter-ui/src/components/newsletters/definitions.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ export const ArticlesTableColumnDefinition = (
105105
{
106106
id: 'keywords',
107107
cell: (item: Article) => (
108-
<SpaceBetween size="xs" direction="horizontal">
108+
<SpaceBetween key={'keywords-'+item.keywords+'spacer'} size="xs" direction="horizontal">
109109
{item.keywords?.split(',').map((keyword: string) => {
110-
return <Badge color="blue">{keyword}</Badge>
110+
return <Badge key={'keywords-' + item.keywords + '-' + keyword} color="blue">{keyword}</Badge>
111111
})}
112112
</SpaceBetween>
113113
),

lib/user-interface/genai-newsletter-ui/src/components/newsletters/publications-table.tsx

Lines changed: 68 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,25 @@ import { AppContext } from '../../common/app-context'
88
import { useParams } from 'react-router-dom'
99
import { Publication } from '../../../../../shared/api/API'
1010
import {
11+
Box,
1112
Button,
1213
Container,
1314
ExpandableSection,
14-
SpaceBetween
15+
SpaceBetween,
1516
} from '@cloudscape-design/components'
17+
import { LoadingBar } from '@cloudscape-design/chat-components'
1618
import { listPublications } from '../../../../../shared/api/graphql/queries'
1719
import { generateAuthorizedClient } from '../../common/helpers'
1820
import Newsletter from './newsletter'
1921

20-
export default function PublicationsTable () {
22+
export default function PublicationsTable() {
2123
const appContext = useContext(AppContext)
2224
const { newsletterId } = useParams()
2325
const [publications, setPublications] = useState<Publication[]>([])
26+
const [publicationsLoading, setPublicationsLoading] = useState<boolean>(true)
2427

2528
const getPublications = useCallback(async () => {
29+
setPublicationsLoading(true)
2630
if (!appContext) {
2731
return
2832
}
@@ -37,65 +41,80 @@ export default function PublicationsTable () {
3741
id: newsletterId
3842
}
3943
}
40-
})
44+
});
4145

4246
if (result.errors) {
43-
console.error(result.errors)
47+
console.error(result.errors);
4448
} 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)
4857
}
58+
4959
}, [appContext, newsletterId])
5060

5161
useEffect(() => {
5262
getPublications()
5363
}, [getPublications])
5464
return (
5565
<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>
9773
) : (
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+
)
99118
)}
100119
</SpaceBetween>
101120
)

0 commit comments

Comments
 (0)