Skip to content

Commit b122640

Browse files
fix(#245): dynamically fetch the sponsorship message
Added one-time fetch function on backend as fetching on frontend was blocked due to CORS
1 parent e9cb60a commit b122640

File tree

7 files changed

+63
-18
lines changed

7 files changed

+63
-18
lines changed

src/main/index.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
import { setupClipboardEvents } from './clipboard'
5656
import { checkForUpdate } from 'shared/data/slices/update'
5757
import path from 'path'
58+
import { setupOneTimeFetchEvent } from './one-time-fetch'
5859

5960
makeAppWithSingleInstanceLock(async () => {
6061
app.setName(APP_CONFIG.TITLE)
@@ -90,6 +91,7 @@ makeAppWithSingleInstanceLock(async () => {
9091
setupFileSystemEvents()
9192
setupClipboardEvents()
9293
setupLogEvents()
94+
setupOneTimeFetchEvent()
9395
buildMenu()
9496

9597
store.subscribe(() => {
@@ -149,11 +151,12 @@ function buildMenu() {
149151
store.dispatch(selectJob(job))
150152
},
151153
onRunJob: async (job) => {
152-
store.dispatch(
153-
runJob({
154-
...job,
155-
})
156-
)
154+
MainWindow().then((w) => w.webContents.send('run-job'))
155+
// store.dispatch(
156+
// runJob({
157+
// ...job,
158+
// })
159+
// )
157160
},
158161
onRemoveJob: async (job) => {
159162
store.dispatch(removeJob(job))

src/main/one-time-fetch.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// fetch a resource and return the result to the renderer
2+
// this is meant as a one-off utility for special cases
3+
import { ipcMain } from 'electron'
4+
import { IPC_EVENT_oneTimeFetch } from '../shared/main-renderer-events'
5+
import fetch from 'node-fetch'
6+
7+
async function oneTimeFetch(url) {
8+
if (url.length == 0) return false
9+
10+
let res = await fetch(url)
11+
if (res) {
12+
return res.json()
13+
}
14+
else {
15+
return null
16+
}
17+
}
18+
19+
function setupOneTimeFetchEvent() {
20+
// comes from the renderer process (ipcRenderer.send())
21+
ipcMain.on(IPC_EVENT_oneTimeFetch, async (event, payload) => {
22+
let res = await oneTimeFetch(payload)
23+
event.sender.send(IPC_EVENT_oneTimeFetch, res)
24+
})
25+
}
26+
export { setupOneTimeFetchEvent, oneTimeFetch }

src/renderer/bridge/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const API = {
4444
sniffEncoding: ipcs.sniffEncoding,
4545
copyToClipboard: ipcs.copyToClipboard,
4646
log: ipcs.log,
47+
oneTimeFetch: ipcs.oneTimeFetch,
4748
// we can add on to this API and restructure it as we move more commands to the redux side
4849
store: {
4950
dispatch,

src/renderer/bridge/ipcs/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ export * from './file/save'
1515
export * from './file/unzip'
1616

1717
export * from './log'
18+
export * from './oneTimeFetch'
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { ipcRenderer } from 'electron'
2+
import * as events from 'shared/main-renderer-events'
3+
4+
// get JSON data from an arbitrary endpoint
5+
export function oneTimeFetch(url) {
6+
return new Promise<JSON>((resolve, reject) => {
7+
ipcRenderer.send(events.IPC_EVENT_oneTimeFetch, url)
8+
ipcRenderer.once(events.IPC_EVENT_oneTimeFetch, (event, res: JSON) => {
9+
resolve(res)
10+
})
11+
})
12+
}

src/renderer/utils/sponsorship.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const { App } = window
2+
13
// default message
24
let defaultSponsorshipMessage = {
35
active: true,
@@ -9,18 +11,17 @@ let defaultSponsorshipMessage = {
911

1012
async function updateSponsorshipMessage() {
1113
// fetch the latest sponsorship message
12-
// try {
13-
// let sponsorshipData = await window.fetch(
14-
// 'https://dl.daisy.org/tools/sponsorship.json'
15-
// )
16-
// if (sponsorshipData) {
17-
// return sponsorshipData['PipelineApp']['en']
18-
// } else {
19-
// return defaultSponsorshipMessage
20-
// }
21-
// } catch (err) {
22-
// return defaultSponsorshipMessage
23-
// }
24-
return defaultSponsorshipMessage
14+
try {
15+
let sponsorshipData = await App.oneTimeFetch(
16+
'https://dl.daisy.org/tools/sponsorship.json'
17+
)
18+
if (sponsorshipData) {
19+
return sponsorshipData['PipelineApp']['en']
20+
} else {
21+
return defaultSponsorshipMessage
22+
}
23+
} catch (err) {
24+
return defaultSponsorshipMessage
25+
}
2526
}
2627
export { defaultSponsorshipMessage, updateSponsorshipMessage }

src/shared/main-renderer-events.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export const IPC_EVENT_pathExists = 'IPC_EVENT_pathExists'
66
export const IPC_EVENT_openInBrowser = 'IPC_EVENT_openInBrowser'
77
export const IPC_EVENT_sniffEncoding = 'IPC_EVENT_sniffEncoding'
88
export const IPC_EVENT_log = 'IPC_EVENT_log'
9+
export const IPC_EVENT_oneTimeFetch = 'IPC_EVENT_oneTimeFetch'

0 commit comments

Comments
 (0)