Skip to content

Instantly share code, notes, and snippets.

@Lucasdsk
Last active June 5, 2020 17:40
Show Gist options
  • Save Lucasdsk/ab07f5bd41c87bbf5c00cd6e9bf9cef3 to your computer and use it in GitHub Desktop.
Save Lucasdsk/ab07f5bd41c87bbf5c00cd6e9bf9cef3 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 OPENING_STATUS = {
PUBLISHED: 'active',
DRAFT: 'draft',
CLOSED: 'closed'
}
const validateTitle = ctx => !!ctx.title
const openingId = 12
const status = ''
const openingMachine = Machine({
id: 'opening',
initial: openingId ? 'editing' : 'creatingNew',
context: {
title: '',
status
},
states: {
editing: {
on: {
SET_TITLE: {
actions: assign({
title: (ctx, evt) => evt.value
})
},
SAVE: {
target: 'saved',
cond: validateTitle
},
PUBLISH: {
target: 'published',
actions: assign({
status: OPENING_STATUS.PUBLISHED
}),
cond: ctx => ctx.status !== OPENING_STATUS.PUBLISHED
},
CLOSE: {
target: 'closed',
actions: assign({
status: OPENING_STATUS.CLOSED
}),
cond: ctx => ctx.status === OPENING_STATUS.PUBLISHED
}
}
},
creatingNew: {
on: {
SET_TITLE: {
actions: assign({
title: (ctx, evt) => evt.value
})
},
SAVE: {
target: 'saved',
actions: assign({
status: OPENING_STATUS.DRAFT
}),
cond: validateTitle,
meta: {
message: 'Vaga cadastrada!'
}
},
PUBLISH: {
target: 'published',
actions: assign({
status: OPENING_STATUS.PUBLISHED
}),
cond: validateTitle
}
}
},
saved: {
meta: {
message: 'Vaga atualizada'
}
},
published: {
type: 'final',
meta: {
message: 'Vaga publicada'
}
},
closed: {
meta: {
message: 'Vaga encerrada'
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment