Skip to content

Instantly share code, notes, and snippets.

@btnwtn
Last active January 26, 2021 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btnwtn/f86c40c1e30cfb2c81a126648e088b23 to your computer and use it in GitHub Desktop.
Save btnwtn/f86c40c1e30cfb2c81a126648e088b23 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
Machine({
id: 'Create Campaign Workflow',
initial: 'processing',
context: {
// Change this to pikachu or charizard
campaignId: 'ditto',
processingStatus: 'idle',
},
states: {
processing: {
invoke: {
id: 'fetchCampaign',
src: apiService,
},
on: {
SUCCESS: 'completed',
MALFORMED_CSV: 'malformedCSV',
TRACK_ERRORS: 'trackValidation'
},
initial: 'polling',
states: {
polling: {
after: {
INTERVAL: {
actions: send('POLL', { to: 'fetchCampaign' })
}
}
}
}
},
error: {
type: 'final'
},
trackValidation: {
on: {
PROCEED_WITH_VALID_TRACKS: {
target: 'processing',
actions: assign({
campaignId: 'pikachu'
})
},
PROCEED_WITH_NEW_CSV: {
target: 'processing',
action: assign({
campaignId: 'new-csv',
})
}
}
},
malformedCSV: {
on: {
SUBMIT_WITH_NEW_CSV: {
target: 'processing',
cond: () => true,
}
}
},
completed: {
type: 'final'
},
completedWithValidTracks: {
type: 'final'
}
}
}, {
delays: { INTERVAL: 1000 }
})
function apiService(context) {
return (callback, onReceive) => {
function poll() {
if (context.campaignId === 'new-csv') {
return callback({
type: 'SUCCESS'
})
}
return fetch(`https://pokeapi.co/api/v2/pokemon/${context.campaignId}/`)
.then(r => r.json())
.then(pokemans => {
if (pokemans.name === 'pikachu') {
callback({
type: 'SUCCESS',
})
}
if (pokemans.name === 'charizard') {
callback({
type: 'MALFORMED_CSV',
})
}
if (pokemans.name === 'ditto') {
callback({
type: 'TRACK_ERRORS',
})
}
})
.catch(console.error)
}
poll()
onReceive(event => {
if (event === 'POLL') {
poll()
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment