Skip to content

Instantly share code, notes, and snippets.

@mpowell-atomic
Created February 24, 2021 01:54
Show Gist options
  • Save mpowell-atomic/49ab309a3a8a05cd4317a3aa58423d3c to your computer and use it in GitHub Desktop.
Save mpowell-atomic/49ab309a3a8a05cd4317a3aa58423d3c to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const createPreview = (campaignId) => {
return {};
};
const deletePreview = (campaignId) => {
return {};
};
const previewMachine = Machine(
{
initial: 'idle',
context: {
preview: null,
previewed_at: null,
campaign_id: null,
},
states: {
idle: {
// check if has preview already, if has skip to created state
always: [
{ cond: 'hasPreviewAlready', target: 'created' },
],
on: {
CREATE: {
target: 'creating',
},
CLOSE_MODAL: {
target: 'idle',
}
}
},
creating: {
invoke: {
src: (context) => createPreview(context.campaign_id),
onDone: {
target: 'created',
actions: assign({
preview: (context, event) => event.data.data.preview,
previewed_at: (context, event) => event.data.data.previewed_at
}),
}
},
},
created: {
on: {
UPDATE: {
target: 'updating'
},
DONE: {
target: 'idle'
},
DELETE: {
target: 'deleting'
}
}
},
updating: {
invoke: {
src: (context) => createPreview(context.campaign_id),
onDone: {
target: 'updated',
actions: assign({
previewed_at: (context, event) => event.data.data.previewed_at
}),
},
}
},
updated: {
on: {
CLOSE_MODAL: {
target: 'close_and_reset',
}
}
},
close_and_reset: {
// delay state change until after modal closes
after: {
600: 'created'
}
},
deleting: {
invoke: {
src: (context) => deletePreview(context.campaign_id),
onDone: {
target: 'idle',
actions: assign({ previewed_at: null })
}
},
}
},
},
{
guards: {
hasPreviewAlready: (context) => {
return context.previewed_at;
}
}
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment