From 2017979a1049b72d04c75d35b3931eedae885f9f Mon Sep 17 00:00:00 2001 From: Eric P Date: Fri, 4 Nov 2022 00:10:14 -0500 Subject: [PATCH] Added live episodes to the front page --- server/index.js | 21 +++++++++++++ ui/src/components/RecentPodcasts/index.tsx | 5 ++-- ui/src/pages/landing.tsx | 34 ++++++++++++++++++---- 3 files changed, 52 insertions(+), 8 deletions(-) diff --git a/server/index.js b/server/index.js index bc6f5f1f..acc5b14a 100644 --- a/server/index.js +++ b/server/index.js @@ -112,6 +112,27 @@ app.use('/api/episodes/byfeedid', async (req, res) => { res.send(response) }) +app.use('/api/episodes/live', async (req, res) => { + let max = req.query.max + const response = await api.custom('episodes/live', {pretty: true, max: max}) + const minPublished = Math.floor(new Date().getTime() / 1000) - 3600 + + // assume live episodes posted within the past hour are live + // since the api doesn't provide start and end times + + response.items = response.items.filter(item => { + return item.datePublished >= minPublished + }) + + response.count = response.items.length; + + if (response.count === 0) { + response.description = 'No matching items' + } + + res.send(response) +}) + app.use('/api/add/byfeedurl', async (req, res) => { let feedUrl = req.query.url const response = await apiAdd.addByFeedUrl(feedUrl) diff --git a/ui/src/components/RecentPodcasts/index.tsx b/ui/src/components/RecentPodcasts/index.tsx index 20b37848..d6734575 100644 --- a/ui/src/components/RecentPodcasts/index.tsx +++ b/ui/src/components/RecentPodcasts/index.tsx @@ -11,6 +11,7 @@ import {Link} from "react-router-dom"; interface IProps { title?: string + liveTitle?: string podcasts?: Array loading?: boolean } @@ -60,7 +61,7 @@ export default class RecentPodcasts extends React.Component { } render() { - const {loading, title, podcasts} = this.props + const {loading, title, liveTitle, podcasts} = this.props const {index} = this.state const selectedPodcast = podcasts[index] return ( @@ -87,7 +88,7 @@ export default class RecentPodcasts extends React.Component { {title && (
- {title} + {selectedPodcast.live ? liveTitle : title}
)}
diff --git a/ui/src/pages/landing.tsx b/ui/src/pages/landing.tsx index ae520457..743100a3 100644 --- a/ui/src/pages/landing.tsx +++ b/ui/src/pages/landing.tsx @@ -11,14 +11,14 @@ import './styles.scss' interface IProps {} interface IState { loading?: boolean - recentPodcasts?: Array + podcasts?: Array stats?: {} } export default class Landing extends React.Component { state = { loading: true, - recentPodcasts: [], + podcasts: [], stats: { feedCountTotal: '1,318,328', feedCount3days: '81,919', @@ -36,13 +36,13 @@ export default class Landing extends React.Component { async componentDidMount(): Promise { this._isMounted = true - const recentPodcasts = (await this.getRecentEpisodes()).items + const podcasts = await this.getEpisodes() const stats = await this.getStats() if (this._isMounted) { this.setState({ loading: false, - recentPodcasts, + podcasts, stats, }) } @@ -68,8 +68,29 @@ export default class Landing extends React.Component { return await response.json() } + async getLiveEpisodes() { + let response = await fetch(`/api/episodes/live?max=3`, { + credentials: 'same-origin', + method: 'GET', + }) + return await response.json() + } + + async getEpisodes() { + let liveResponse = await this.getLiveEpisodes() + let episodes = liveResponse.items.map(ep => { + ep.live = true + return ep + }) + + let recentResponse = await this.getRecentEpisodes() + episodes = episodes.concat(recentResponse.items) + + return episodes; + } + render() { - const { loading, recentPodcasts, stats } = this.state + const { loading, podcasts, stats } = this.state updateTitle('Home') return (
@@ -108,8 +129,9 @@ export default class Landing extends React.Component {