Skip to content

Commit

Permalink
fix: sort data feed articles by date descending (#106)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
flamingquaks authored Jul 17, 2024
1 parent a08c908 commit a395c2d
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,16 @@ export default function DataFeedArticleTable () {
return
}
if (result.data.listArticles?.items !== null) {
setArticles(result.data.listArticles?.items as Article[])
const sortedArticles = [...(result.data.listArticles?.items as Article[])]
.sort((a, b) => {
const dateA = new Date(a.createdAt ?? 0);
const dateB = new Date(b.createdAt ?? 0);
return dateB.getTime() - dateA.getTime();
});
setArticles(sortedArticles);
}


setLoading(false)
}, [appContext, dataFeedId])

Expand Down Expand Up @@ -101,7 +108,6 @@ export default function DataFeedArticleTable () {
if (flagArticle !== null && flagArticle == 'true' && articleId !== null) {
setLoading(true)
try {
console.log('TRIGGER')
flagDataFeedArticle(articleId, true)
} catch (error) {
console.log(error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ export const ArticlesTableColumnDefinition = (
{
id: 'keywords',
cell: (item: Article) => (
<SpaceBetween size="xs" direction="horizontal">
<SpaceBetween key={'keywords-'+item.keywords+'spacer'} size="xs" direction="horizontal">
{item.keywords?.split(',').map((keyword: string) => {
return <Badge color="blue">{keyword}</Badge>
return <Badge key={'keywords-' + item.keywords + '-' + keyword} color="blue">{keyword}</Badge>
})}
</SpaceBetween>
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,25 @@ import { AppContext } from '../../common/app-context'
import { useParams } from 'react-router-dom'
import { Publication } from '../../../../../shared/api/API'
import {
Box,
Button,
Container,
ExpandableSection,
SpaceBetween
SpaceBetween,
} from '@cloudscape-design/components'
import { LoadingBar } from '@cloudscape-design/chat-components'
import { listPublications } from '../../../../../shared/api/graphql/queries'
import { generateAuthorizedClient } from '../../common/helpers'
import Newsletter from './newsletter'

export default function PublicationsTable () {
export default function PublicationsTable() {
const appContext = useContext(AppContext)
const { newsletterId } = useParams()
const [publications, setPublications] = useState<Publication[]>([])
const [publicationsLoading, setPublicationsLoading] = useState<boolean>(true)

const getPublications = useCallback(async () => {
setPublicationsLoading(true)
if (!appContext) {
return
}
Expand All @@ -37,65 +41,80 @@ export default function PublicationsTable () {
id: newsletterId
}
}
})
});

if (result.errors) {
console.error(result.errors)
console.error(result.errors);
} else {
setPublications([
...(result.data.listPublications?.items as Publication[])
])
const sortedPublications = [...(result.data.listPublications?.items as Publication[])]
.sort((a, b) => {
const dateA = new Date(a.createdAt ?? 0);
const dateB = new Date(b.createdAt ?? 0);
return dateB.getTime() - dateA.getTime();
});
setPublications(sortedPublications);
setPublicationsLoading(false)
}

}, [appContext, newsletterId])

useEffect(() => {
getPublications()
}, [getPublications])
return (
<SpaceBetween direction="vertical" size="m">
{publications.length > 0 ? (
publications.map((publication) => {
if (publication.filePath) {
return (
<ExpandableSection
key={'newsletterPublicationSection' + publication.id}
headerText={publication.createdAt}
headerActions={
publication.filePath !== null &&
publication.filePath !== undefined &&
publication.filePath.length > 0 ? (
<SpaceBetween size="s" direction="horizontal">
<Button
onClick={() => {
window.open(
(publication.filePath + '.html') as string,
'_blank'
)
}}
>
Open in a New Window
</Button>
</SpaceBetween>
) : (
<></>
)
}
variant="stacked"
>
<Container>
<Newsletter
filePath={publication.filePath}
key={`rendered-newsletter-${publication.id}`}
/>
</Container>
</ExpandableSection>
)
} else {
return <></>
}
})
{publicationsLoading ? (
<SpaceBetween direction='vertical' size='m'>
<Box margin={{ bottom: "xs", left: "l"}}>
Loading Newsletter Publications
</Box>
<LoadingBar variant='gen-ai' />
</SpaceBetween>
) : (
<p>No Publications Available</p>
publications.length > 0 ? (
publications.map((publication) => {
if (publication.filePath) {
return (
<ExpandableSection
key={'newsletterPublicationSection' + publication.id}
headerText={publication.createdAt}
headerActions={
publication.filePath !== null &&
publication.filePath !== undefined &&
publication.filePath.length > 0 ? (
<SpaceBetween size="s" direction="horizontal">
<Button
onClick={() => {
window.open(
(publication.filePath + '.html') as string,
'_blank'
)
}}
>
Open in a New Window
</Button>
</SpaceBetween>
) : (
<></>
)
}
variant="stacked"
>
<Container>
<Newsletter
filePath={publication.filePath}
key={`rendered-newsletter-${publication.id}`}
/>
</Container>
</ExpandableSection>
)
} else {
return <></>
}
})
) : (
<p>No Publications Available</p>
)
)}
</SpaceBetween>
)
Expand Down
Loading

0 comments on commit a395c2d

Please sign in to comment.