Skip to content

Instantly share code, notes, and snippets.

@Colubi
Created October 14, 2019 11:59
Show Gist options
  • Select an option

  • Save Colubi/f836da412aea6cc0eb6081e6af6e8931 to your computer and use it in GitHub Desktop.

Select an option

Save Colubi/f836da412aea6cc0eb6081e6af6e8931 to your computer and use it in GitHub Desktop.

Revisions

  1. Colubi created this gist Oct 14, 2019.
    95 changes: 95 additions & 0 deletions machine.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    Machine({
    id: "optIn",
    type: "parallel",
    context: {
    optInState: "initial",
    optInRequest: {
    successState: "Success",
    },
    },
    states: {
    flow: {
    initial: "initial",
    states: {
    initial: {
    on: {
    START_OPTIN: [
    {
    target: "progress",
    in: "#optIn.session.loggedIn",
    actions: ["assignOptInRequestParams"],
    },
    {
    actions: ["assignOptInRequestParams", "displayLogin"],
    },
    ],
    },
    },
    progress: {
    entry: "setLoading",
    invoke: {
    src: "requestOptIn",
    onDone: {
    target: "success",
    actions: assign({
    optInState: (_, result) => result.data,
    }),
    },
    onError: {
    target: "error",
    actions: assign({
    optInState: (_, result) => result.data,
    }),
    },
    },
    },
    success: {
    type: "final",
    entry: "onOptInSuccess",
    },
    error: {
    type: "final",
    entry: "onOptInError",
    },
    },
    },
    session: {
    initial: "notDetermined",
    states: {
    notDetermined: {
    on: {
    LOG_IN: {
    target: "loggedIn",
    },
    LOG_OUT: {
    target: "loggedOut",
    },
    },
    },
    loggedIn: {
    on: {
    LOG_OUT: {
    target: "loggedOut",
    },
    },
    },
    loggedOut: {
    on: {
    LOG_IN: {
    target: "loggedIn",
    },
    },
    },
    },
    },
    },
    }, {
    actions: {
    assignOptInRequestParams: assign({
    optInRequest: (_, event) => event.optInRequest,
    }),
    setLoading: assign({
    optInState: "progress",
    }),
    },
    });