Last active
June 26, 2021 15:11
-
-
Save tgevaert/d884c46da5bb88ffb36a57f318b1e848 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Available variables: | |
// - Machine | |
// - interpret | |
// - assign | |
// - send | |
// - sendParent | |
// - spawn | |
// - raise | |
// - actions | |
// - XState (all XState exports) | |
const referralBadgeMachine = Machine( | |
{ | |
id: "referralBadge", | |
initial: "NOT_ACTIVE", | |
context: { | |
activeLoanVisits: 0, | |
loanApprovalBadgeTriggers: 0, | |
latestManualBadgeTriggerTimestamp: new Date("1900-01-01"), | |
}, | |
states: { | |
NOT_ACTIVE: { | |
on: { | |
UPDATE_REFERRAL_BADGE_TRIGGER_TIMESTAMP: { | |
target: "ACTIVE", | |
actions: ["fireManualAnalyticsEvent", "updateManualTimestamp"], | |
}, | |
}, | |
initial: "unknown", | |
states: { | |
unknown: { | |
always: [ | |
{ | |
target: "WAITING_FOR_LOAN_APPROVAL", | |
cond: "shouldListenForLoanApprovals", | |
}, | |
{ target: "DONE" }, | |
], | |
}, | |
WAITING_FOR_LOAN_APPROVAL: { | |
on: { | |
SET_ACTIVE_LOAN_RENDER_STATE: { | |
target: "WAITING_TO_ACTIVATE", | |
cond: "isLoanApprovedVisit", | |
}, | |
}, | |
}, | |
WAITING_TO_ACTIVATE: { | |
on: { | |
SET_ACTIVE_LOAN_RENDER_STATE: [ | |
{ | |
target: "#referralBadge.ACTIVE", | |
cond: ["isActiveLoanVisit", "isSecondVisit"], | |
actions: [ | |
"incrementLoanApprovalBadgeTriggers", | |
"fireLoanApprovalAnalyticsEvent", | |
], | |
}, | |
{ actions: "incrementActiveLoanVisits" }, | |
], | |
}, | |
}, | |
DONE: { | |
type: "final", | |
}, | |
}, | |
}, | |
ACTIVE: { | |
on: { | |
PUSH_ROUTE: { | |
target: "NOT_ACTIVE", | |
actions: ["fireReferralVisitAnalyticsEvent"], | |
cond: "isReferralRoute", | |
}, | |
}, | |
}, | |
}, | |
}, | |
{ | |
actions: { | |
fireManualAnalyticsEvent: () => | |
console.log("Referral Badge Active", { | |
triggerType: "Manual Trigger", | |
}), | |
fireLoanApprovalAnalyticsEvent: () => | |
console.log("Referral Badge Active", { | |
triggerType: "Loan Approval", | |
}), | |
fireReferralVisitAnalyticsEvent: () => | |
console.log("Referral Page Visited"), | |
updateManualTimestamp: assign({ | |
latestManualBadgeTriggerTimestamp: () => new Date(), | |
}), | |
incrementActiveLoanVisits: assign({ | |
activeLoanVisits: (context) => context.activeLoanVisits + 1, | |
}), | |
incrementLoanApprovalBadgeTriggers: assign({ | |
loanApprovalBadgeTriggers: (context) => | |
context.loanApprovalBadgeTriggers + 1, | |
}), | |
}, | |
guards: { | |
isReferralRoute: (context, event) => | |
event?.payload?.route?.key === "Referral", | |
isActiveLoanVisit: (context, event) => | |
event?.payload?.renderState === "ACTIVE_LOAN", | |
isLoanApprovedVisit: (context, event) => | |
event?.payload?.renderState === "APPROVED", | |
isSecondActiveLoanVisit: (context) => context.activeLoanVisits + 1 === 2, | |
shouldListenForLoanApprovals: (context) => | |
context.loanApprovalBadgeTriggers < 2, | |
}, | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment