Skip to content

Commit

Permalink
fix(#245): dynamically fetch the sponsorship message
Browse files Browse the repository at this point in the history
Added one-time fetch function on backend as fetching on frontend was blocked due to CORS
  • Loading branch information
marisademeglio committed Oct 8, 2024
1 parent e9cb60a commit b122640
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 18 deletions.
13 changes: 8 additions & 5 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
import { setupClipboardEvents } from './clipboard'
import { checkForUpdate } from 'shared/data/slices/update'
import path from 'path'
import { setupOneTimeFetchEvent } from './one-time-fetch'

makeAppWithSingleInstanceLock(async () => {
app.setName(APP_CONFIG.TITLE)
Expand Down Expand Up @@ -90,6 +91,7 @@ makeAppWithSingleInstanceLock(async () => {
setupFileSystemEvents()
setupClipboardEvents()
setupLogEvents()
setupOneTimeFetchEvent()
buildMenu()

store.subscribe(() => {
Expand Down Expand Up @@ -149,11 +151,12 @@ function buildMenu() {
store.dispatch(selectJob(job))
},
onRunJob: async (job) => {
store.dispatch(
runJob({
...job,
})
)
MainWindow().then((w) => w.webContents.send('run-job'))
// store.dispatch(
// runJob({
// ...job,
// })
// )
},
onRemoveJob: async (job) => {
store.dispatch(removeJob(job))
Expand Down
26 changes: 26 additions & 0 deletions src/main/one-time-fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// fetch a resource and return the result to the renderer
// this is meant as a one-off utility for special cases
import { ipcMain } from 'electron'
import { IPC_EVENT_oneTimeFetch } from '../shared/main-renderer-events'
import fetch from 'node-fetch'

async function oneTimeFetch(url) {
if (url.length == 0) return false

let res = await fetch(url)
if (res) {
return res.json()
}
else {
return null
}
}

function setupOneTimeFetchEvent() {
// comes from the renderer process (ipcRenderer.send())
ipcMain.on(IPC_EVENT_oneTimeFetch, async (event, payload) => {
let res = await oneTimeFetch(payload)
event.sender.send(IPC_EVENT_oneTimeFetch, res)
})
}
export { setupOneTimeFetchEvent, oneTimeFetch }
1 change: 1 addition & 0 deletions src/renderer/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const API = {
sniffEncoding: ipcs.sniffEncoding,
copyToClipboard: ipcs.copyToClipboard,
log: ipcs.log,
oneTimeFetch: ipcs.oneTimeFetch,
// we can add on to this API and restructure it as we move more commands to the redux side
store: {
dispatch,
Expand Down
1 change: 1 addition & 0 deletions src/renderer/bridge/ipcs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ export * from './file/save'
export * from './file/unzip'

export * from './log'
export * from './oneTimeFetch'
12 changes: 12 additions & 0 deletions src/renderer/bridge/ipcs/oneTimeFetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { ipcRenderer } from 'electron'
import * as events from 'shared/main-renderer-events'

// get JSON data from an arbitrary endpoint
export function oneTimeFetch(url) {
return new Promise<JSON>((resolve, reject) => {
ipcRenderer.send(events.IPC_EVENT_oneTimeFetch, url)
ipcRenderer.once(events.IPC_EVENT_oneTimeFetch, (event, res: JSON) => {
resolve(res)
})
})
}
27 changes: 14 additions & 13 deletions src/renderer/utils/sponsorship.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const { App } = window

// default message
let defaultSponsorshipMessage = {
active: true,
Expand All @@ -9,18 +11,17 @@ let defaultSponsorshipMessage = {

async function updateSponsorshipMessage() {
// fetch the latest sponsorship message
// try {
// let sponsorshipData = await window.fetch(
// 'https://dl.daisy.org/tools/sponsorship.json'
// )
// if (sponsorshipData) {
// return sponsorshipData['PipelineApp']['en']
// } else {
// return defaultSponsorshipMessage
// }
// } catch (err) {
// return defaultSponsorshipMessage
// }
return defaultSponsorshipMessage
try {
let sponsorshipData = await App.oneTimeFetch(
'https://dl.daisy.org/tools/sponsorship.json'
)
if (sponsorshipData) {
return sponsorshipData['PipelineApp']['en']
} else {
return defaultSponsorshipMessage
}
} catch (err) {
return defaultSponsorshipMessage
}
}
export { defaultSponsorshipMessage, updateSponsorshipMessage }
1 change: 1 addition & 0 deletions src/shared/main-renderer-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export const IPC_EVENT_pathExists = 'IPC_EVENT_pathExists'
export const IPC_EVENT_openInBrowser = 'IPC_EVENT_openInBrowser'
export const IPC_EVENT_sniffEncoding = 'IPC_EVENT_sniffEncoding'
export const IPC_EVENT_log = 'IPC_EVENT_log'
export const IPC_EVENT_oneTimeFetch = 'IPC_EVENT_oneTimeFetch'

0 comments on commit b122640

Please sign in to comment.