Skip to content

Commit e0f962b

Browse files
apphelper: Download and Share for audio sermons on the website
The website Sermons element played audio-only sermons with a bare player. It now shows Download through the Api public download redirect and Share for the /sermons/<id> page under the player, like the Podcast element. Video sermons are unchanged. ChurchApps/ChurchAppsSupport#1709
1 parent 1e221fc commit e0f962b

4 files changed

Lines changed: 86 additions & 2 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@churchapps/apphelper": patch
3+
---
4+
5+
Sermons element: audio-only sermons get Download and Share under the player, like the Podcast element.

‎apphelper/public/locales/en.json‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,11 @@
410410
},
411411
"sermon": {
412412
"back": "Back",
413-
"playlists": "Playlists"
413+
"download": "Download",
414+
"linkCopied": "Link copied",
415+
"playlists": "Playlists",
416+
"share": "Share",
417+
"unableToShare": "Unable to share"
414418
},
415419
"stockPhotos": {
416420
"photoBy": "Photo by:",

‎apphelper/src/website/components/elementTypes/SermonElement.tsx‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"use client";
2-
import { useEffect, useState } from "react";
2+
import { useEffect, useRef, useState } from "react";
33
import { Typography, Breadcrumbs, Button, Grid, Card, CardContent, Box } from "@mui/material";
44
import FolderCopyIcon from "@mui/icons-material/FolderCopy";
55
import TopicIcon from "@mui/icons-material/Topic";
@@ -13,6 +13,7 @@ import { Locale } from "../../..";
1313
import type { PlaylistInterface, SermonInterface } from "@churchapps/helpers";
1414
import { ElementInterface } from "../../helpers";
1515
import { EnvironmentHelper } from "../../helpers/EnvironmentHelper";
16+
import { shareLink } from "../../helpers/shareLink";
1617

1718
interface Props {
1819
churchId: string;
@@ -28,6 +29,8 @@ export const SermonElement = ({ churchId, appearance, element }: Props) => {
2829
const [sermons, setSermons] = useState<SermonInterface[]>([]);
2930
const [activeSermons, setActiveSermons] = useState<SermonInterface[]>([]);
3031
const [activeVideo, setActiveVideo] = useState<SermonInterface>();
32+
const [shareStatus, setShareStatus] = useState<string>("");
33+
const shareTimer = useRef<ReturnType<typeof setTimeout> | null>(null);
3134

3235
const answers: any = element?.answers || {};
3336
const layout = (answers.layout as string) || "browse";
@@ -48,6 +51,8 @@ export const SermonElement = ({ churchId, appearance, element }: Props) => {
4851
});
4952
}, [churchId]);
5053

54+
useEffect(() => () => { if (shareTimer.current) clearTimeout(shareTimer.current); }, []);
55+
5156
const getFilteredData = (id?: string) => {
5257
if (!id) return;
5358
const filteredData = sermons.filter((item) => item.playlistId === id);
@@ -63,11 +68,29 @@ export const SermonElement = ({ churchId, appearance, element }: Props) => {
6368

6469
const thumbFor = (item: SermonInterface) => item.thumbnail || AppearanceHelper.getLogo(appearance, "/images/logo.png", "/images/logo.png", "#FFF");
6570

71+
// Uploaded sermon audio is private in S3, so downloads go through the Api redirect that signs the link.
72+
const downloadHref = (item: SermonInterface) => ApiHelper.getConfig("ContentApi")?.url + "/sermons/public/download/" + churchId + "/" + item.id;
73+
74+
const handleShare = async (item: SermonInterface) => {
75+
const url = window.location.origin + "/sermons/" + item.id;
76+
const result = await shareLink({ title: item.title || "", text: `Check out this sermon: "${item.title}" ${url}`, url });
77+
const text = result === "copied" ? Locale.label("sermon.linkCopied", "Link copied") : result === "failed" ? Locale.label("sermon.unableToShare", "Unable to share") : "";
78+
if (!text) return;
79+
setShareStatus(text);
80+
if (shareTimer.current) clearTimeout(shareTimer.current);
81+
shareTimer.current = setTimeout(() => setShareStatus(""), 3000);
82+
};
83+
6684
const renderAudio = (item: SermonInterface) => (
6785
<div className="sermonAudio">
6886
<img src={item.thumbnail || activePlaylist?.thumbnail || thumbFor(item)} alt={item.title} loading="lazy" />
6987
<h2>{item.title}</h2>
7088
<audio controls autoPlay preload="none" src={item.audioUrl} data-testid="sermon-audio" />
89+
<div className="podcastActions">
90+
<a className="podcastDownload" data-testid="sermon-download" href={downloadHref(item)} aria-label={Locale.label("sermon.download", "Download") + " " + (item.title || "")}>{Locale.label("sermon.download", "Download")}</a>
91+
<button type="button" className="podcastShare" data-testid="sermon-share" aria-label={Locale.label("sermon.share", "Share") + " " + (item.title || "")} onClick={() => handleShare(item)}>{Locale.label("sermon.share", "Share")}</button>
92+
{shareStatus && <span className="podcastShareStatus" role="status" data-testid="sermon-share-status">{shareStatus}</span>}
93+
</div>
7194
{item.description && <p>{item.description}</p>}
7295
</div>
7396
);

‎apphelper/src/website/components/elementTypes/__tests__/SermonElement.test.tsx‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,55 @@ describe("SermonElement audio-only sermons", () => {
6262
expect(el.querySelector('[data-testid="sermon-audio"]')).toBeNull();
6363
});
6464
});
65+
66+
// ChurchAppsSupport#1709: audio sermons on the website get the same Download and Share the Podcast element has.
67+
describe("SermonElement audio actions", () => {
68+
const openAudio = async () => {
69+
vi.spyOn(ApiHelper, "getConfig").mockReturnValue({ keyName: "ContentApi", url: "https://api.churchapps.org/content", jwt: "", permissions: [] } as any);
70+
const el = await mount({ layout: "list" });
71+
await click(el.querySelector(".sermonListItem"));
72+
return el;
73+
};
74+
75+
it("downloads an audio sermon through the Api redirect", async () => {
76+
const el = await openAudio();
77+
const download = el.querySelector('[data-testid="sermon-download"]');
78+
expect(download?.textContent).toBe("Download");
79+
expect(download?.getAttribute("href")).toBe("https://api.churchapps.org/content/sermons/public/download/church1/s2");
80+
});
81+
82+
it("shares the sermon page link", async () => {
83+
const share = vi.fn().mockResolvedValue(undefined);
84+
vi.stubGlobal("navigator", { ...navigator, share });
85+
try {
86+
const el = await openAudio();
87+
const button = el.querySelector<HTMLButtonElement>('[data-testid="sermon-share"]');
88+
expect(button?.textContent).toBe("Share");
89+
await act(async () => { button!.click(); });
90+
const url = window.location.origin + "/sermons/s2";
91+
expect(share).toHaveBeenCalledWith({ title: "Walking Through Ruth", text: "Check out this sermon: \"Walking Through Ruth\" " + url, url });
92+
} finally {
93+
vi.unstubAllGlobals();
94+
}
95+
});
96+
97+
it("copies the link where the browser cannot share", async () => {
98+
const writeText = vi.fn().mockResolvedValue(undefined);
99+
vi.stubGlobal("navigator", { clipboard: { writeText } });
100+
try {
101+
const el = await openAudio();
102+
await act(async () => { el.querySelector<HTMLButtonElement>('[data-testid="sermon-share"]')!.click(); });
103+
expect(writeText).toHaveBeenCalled();
104+
expect(el.querySelector('[data-testid="sermon-share-status"]')?.textContent).toBe("Link copied");
105+
} finally {
106+
vi.unstubAllGlobals();
107+
}
108+
});
109+
110+
it("leaves the video view without audio actions", async () => {
111+
const el = await mount({ layout: "grid" });
112+
await click(el.querySelectorAll(".sermonCard")[1]);
113+
expect(el.querySelector('[data-testid="sermon-download"]')).toBeNull();
114+
expect(el.querySelector('[data-testid="sermon-share"]')).toBeNull();
115+
});
116+
});

0 commit comments

Comments
 (0)