This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Using a JavaScript proxy for a super low code REST client */ | |
// via https://dev.to/dipsaus9/javascript-lets-create-aproxy-19hg | |
// also see https://towardsdatascience.com/why-to-use-javascript-proxy-5cdc69d943e3 | |
const createApi = (url) => { | |
return new Proxy({}, { | |
get(target, key) { | |
return async function(id = "") { | |
const response = await fetch(`${url}/${key}/${id}`) | |
if (response.ok) { | |
return response.json(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const connectionMachine = Machine({ | |
id: 'connection', | |
initial: 'testing', | |
states: { | |
testing: { | |
invoke: { | |
id: 'testing', | |
src: 'test', | |
onDone: [{ | |
target: 'unknown', |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fetchMachine = Machine({ | |
id: 'fetch', | |
initial: 'idle', | |
context: { | |
retries: 0 | |
}, | |
states: { | |
idle: { | |
on: { | |
FETCH: 'loading' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const appStateMachine = Machine({ | |
id: 'app-state', | |
initial: 'setup', | |
states: { | |
setup: { | |
invoke: { | |
id: 'listen', | |
src: 'setup' | |
}, | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const agreementMachine = Machine({ | |
id: 'agreement', | |
initial: 'unknown', | |
context: { | |
terms: null, // Terms of use document | |
status: null, // 'accepted' or perhaps 'declined' | |
timestamp: null | |
}, | |
states: { | |
unknown: { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Cache(config) { | |
config = config || {}; | |
config.trim = config.trim || 600; | |
config.ttl = config.ttl || 3600; | |
var data = {}; | |
var self = this; | |
var now = function() { | |
return new Date().getTime() / 1000; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fileMachine = Machine({ | |
id: 'file', | |
type: 'parallel', | |
states: { | |
upload: { | |
initial: 'idle', | |
states: { | |
idle: { | |
on: { | |
INIT_UPLOAD: 'pending' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = 'https://assets.avalanche.ca/images/' | |
const iconURLs = new Map([ | |
['fall', PATH + 'early_season_icon.svg'], | |
['spring', PATH + 'spring_situation_icon.svg'], | |
['summer', PATH + 'summer_conditions_icon.svg'], | |
]) | |
const src = IconURLs.get(payload.season.value) // payload is https://api.avalanche.ca/forecasts/fr/products/chic-chocs | |
<img src={src} alt='' /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function main(lang) { | |
const name = await getSponsorName() | |
return await getSponsorData(name, lang) | |
} | |
async function getSponsorName() { | |
const date = new Date() | |
const url = API + '/static/sponsors/' + date.toISOString().substr(0, 10) | |
const sponsors = await get(url) |