Skip to content

Instantly share code, notes, and snippets.

@xavxyz
Last active September 6, 2021 13:42
Show Gist options
  • Save xavxyz/a4788cfd65e0751a2d817754d789c732 to your computer and use it in GitHub Desktop.
Save xavxyz/a4788cfd65e0751a2d817754d789c732 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const fileUploadMachine = Machine({
id: 'file-upload',
initial: 'idle',
context: {
userId: 'abc-123',
file: undefined,
signedUrl: undefined,
fileId: undefined,
},
states: {
idle: {
on: {
DROP_FILE: [
{
target: 'generatingSignedUrl',
actions: assign({
file: (context, event) => event.file
}),
cond: (context, event) => isValidFile(event.file)
},
{
actions: () => pushFlashMessage('Fichier invalide ! Veuillez réessayer avec un autre fichier.'),
target: 'idle',
cond: (context, event) => !isValidFile(event.file)
}
],
}
},
generatingSignedUrl: {
invoke: {
id: 'generateSignedUrl',
src: (context, event) => generateSignedUrl(),
onDone: {
target: 'uploadingFile',
actions: assign({
signedUrl: (context, event) => event.data
})
},
onError: {
target: 'idle',
actions: () => pushFlashMessage('Erreur lors du téléchargement du fichier ! Veuillez réessayer.')
}
}
},
uploadingFile: {
invoke: {
id: 'uploadFile',
src: (context, event) => uploadFile({
signedUrl: context.signedUrl,
file: context.file
}),
onDone: {
target: 'updatingUser',
actions: assign({
fileId: (context, event) => event.data
})
},
onError: {
target: 'idle',
actions: () => pushFlashMessage('Erreur lors du téléchargement du fichier ! Veuillez réessayer.')
}
}
},
updatingUser: {
invoke: {
id: 'updateUser',
src: (context, event) => updateUser({
userId: context.userId,
fileId: context.fileId
}),
onDone: {
target: 'idle',
actions: () => pushFlashMessage('Informations mises à jour !')
},
onError: {
target: 'idle',
actions: () => pushFlashMessage('Erreur lors du téléchargement du fichier ! Veuillez réessayer.')
}
}
}
}
});
function isValidFile(file) {
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment