Skip to content

Instantly share code, notes, and snippets.

@gustavocd
Last active December 17, 2020 04:13
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 gustavocd/4ed9b4d3458596c96e5f299870b06b86 to your computer and use it in GitHub Desktop.
Save gustavocd/4ed9b4d3458596c96e5f299870b06b86 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
// - XState (all XState exports)
const fetchMachine = Machine({
id: 'agreementSummary',
initial: 'idle',
context: {
text: '',
error: null
},
states: {
idle: {
on: {
EDIT: { target: 'editing' },
CREATE: { target: 'creating' }
}
},
editing: {
on: {
UPDATE_TEXT: {
actions: assign({
text: (_, event) => event.text
})
},
SUBMIT: {
target: 'loading',
cond: 'hasText'
},
CANCEL: {
target: 'idle',
actions: assign({
text: ''
})
}
}
},
creating: {
on: {
UPDATE_TEXT: {
actions: assign({
text: (_, event) => event.text
})
},
SUBMIT: {
target: 'loading',
cond: 'hasText'
},
CANCEL: {
target: 'idle',
actions: assign({
text: ''
})
}
}
},
loading: {
invoke: {
id: 'saveItem',
src: (context, event) =>
fetch('https://dog.ceo/api/breeds/image/random').then((data) =>
data.json()
),
onDone: {
target: 'success',
actions: assign({
text: 'new item'
})
},
onError: {
target: 'failure',
actions: assign({
error: 'error message'
})
}
},
},
success: {
type: 'final'
},
failure: {
on: {
RETRY: {
target: 'loading',
}
}
}
}
}, {
guards: {
hasText: ({ text }) => text.length > 0
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment