Skip to content

Instantly share code, notes, and snippets.

@edvinerikson
Created July 30, 2020 12:34
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 edvinerikson/5c5090ed8d0abe6a8f30d156d5ea4522 to your computer and use it in GitHub Desktop.
Save edvinerikson/5c5090ed8d0abe6a8f30d156d5ea4522 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const duplicateAccountCheckMachine = Machine({
id: "duplicateAccountCheck",
initial: "idle",
states: {
idle: {
on: {
OPEN: "started"
}
},
started: {
on: {
CLOSE: "finishing"
}
},
finishing: {
on: {
ABORT: "idle",
PERSIST: "persisting",
BACK: "started",
TOGGLE_RESULT: {
target: "finishing",
actions: assign({
result: context => {
return context.result === "SUCCESS" ? "FAIL" : "SUCCESS";
}
})
},
TOGGLE_DEVICE_CHECK: {
target: "finishing",
actions: assign({
deviceCheckCompleted: context => {
return !context.deviceCheckCompleted;
},
comment: () => {
return null;
}
})
},
CHANGE_COMMENT: {
target: "finishing",
actions: assign({
comment: (context, event) => {
return event.comment;
}
})
}
}
},
persisting: {
on: {
REJECT: "failure",
RESOLVE: "finished"
},
invoke: {
id: "persist",
src: context => callback => {
const {
environment,
playerId,
result,
deviceCheckCompleted,
comment
} = context;
if (playerId == null) {
callback("REJECT");
return;
}
const disposable = commitMutation<
DobMobCheckMachineCreateCheckMutation
>(environment, {
mutation: graphql`
mutation DobMobCheckMachineCreateCheckMutation(
$input: CreatePlayerDuplicateAccountCheckInput!
) {
createPlayerDuplicateAccountCheck(input: $input) {
__typename
}
}
`,
variables: {
input: {
playerId,
result,
deviceCheckCompleted,
comment
}
},
onCompleted: (response, errors) => {
if (
(errors != null && errors.length > 0) ||
response.createPlayerDuplicateAccountCheck.__typename ===
"CreatePlayerDuplicateAccountCheckErrorPayload"
) {
callback("REJECT");
} else {
callback("RESOLVE");
}
},
onError: () => {
callback("REJECT");
}
});
return () => disposable.dispose();
}
}
},
finished: {
initial: "closed",
states: {
open: {
on: {
CLOSE: "closed"
}
},
closed: {
on: {
OPEN: "open"
}
}
}
},
failure: {
on: {
RETRY: "persisting"
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment