@typeonce/effect-machine-devtools 0.26.0 → 0.26.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,145 @@
1
+ import { Machine } from "@typeonce/effect-machine"
2
+ import { Effect, Schema } from "effect"
3
+
4
+ const Mode = Schema.Literals(["login", "signup"])
5
+ const LoginMethod = Schema.Literals(["code", "password"])
6
+
7
+ const AuthState = Schema.TaggedUnion({
8
+ Editing: {
9
+ mode: Mode,
10
+ email: Schema.String,
11
+ loginMethod: LoginMethod,
12
+ password: Schema.String
13
+ },
14
+ EditingFailed: { message: Schema.String },
15
+ Verification: {
16
+ mode: Mode,
17
+ email: Schema.String,
18
+ code: Schema.String
19
+ },
20
+ Navigating: { href: Schema.String }
21
+ })
22
+
23
+ const AuthStates = Machine.states({
24
+ Editing: {
25
+ schema: AuthState.cases.Editing,
26
+ initial: "Form",
27
+ states: {
28
+ Form: {
29
+ initial: "Ready",
30
+ states: {
31
+ Ready: {},
32
+ Failed: AuthState.cases.EditingFailed
33
+ }
34
+ },
35
+ SubmittingLogin: {},
36
+ RequestingVerification: {}
37
+ }
38
+ },
39
+ Verification: {
40
+ schema: AuthState.cases.Verification,
41
+ initial: "CodeEntry",
42
+ states: {
43
+ CodeEntry: {}
44
+ }
45
+ },
46
+ Navigating: AuthState.cases.Navigating
47
+ })
48
+
49
+ const AuthEvents = Machine.events(
50
+ Schema.TaggedUnion({
51
+ EmailChanged: { value: Schema.String },
52
+ LoginMethodChanged: { value: LoginMethod },
53
+ PasswordChanged: { value: Schema.String },
54
+ Submit: { route: Schema.Literals(["verification", "login", "invalid"]) }
55
+ })
56
+ )
57
+
58
+ export const layoutResilienceMachine = Machine.make({
59
+ id: "layout-resilience",
60
+ states: AuthStates.states,
61
+ events: AuthEvents,
62
+ initial: (to) =>
63
+ to.Editing.initial.resolve(({ target }) =>
64
+ target.from({
65
+ mode: "login",
66
+ email: "",
67
+ loginMethod: "code",
68
+ password: ""
69
+ }, (editing) => editing.Form.from((form) => form.Ready.from()))
70
+ )
71
+ }).handle({
72
+ Editing: {
73
+ states: {
74
+ Form: {
75
+ on: {
76
+ EmailChanged: (to) =>
77
+ to.branch.Editing.update(({ current, event, owner }) => owner.from({ ...current, email: event.value })),
78
+ LoginMethodChanged: (to) =>
79
+ to.branch.Editing.update(({ current, event, owner }) =>
80
+ owner.from({ ...current, loginMethod: event.value })
81
+ ),
82
+ PasswordChanged: (to) =>
83
+ to.branch.Editing.update(({ current, event, owner }) => owner.from({ ...current, password: event.value })),
84
+ Submit: (to) =>
85
+ to.branches({
86
+ requestVerification: {
87
+ title: "Request a verification code",
88
+ target: to.branch.Editing.RequestingVerification()
89
+ },
90
+ login: {
91
+ title: "Submit password login",
92
+ target: to.branch.Editing.SubmittingLogin()
93
+ },
94
+ invalid: {
95
+ title: "Show validation failure",
96
+ target: to.local.Failed()
97
+ }
98
+ }).resolve(({ event, select }) => {
99
+ if (event.route === "login") return select.login.from()
100
+ if (event.route === "verification") return select.requestVerification.from()
101
+ return select.invalid.from({ message: "Enter valid authentication details." })
102
+ })
103
+ },
104
+ states: {
105
+ Ready: {},
106
+ Failed: {}
107
+ }
108
+ },
109
+ SubmittingLogin: {
110
+ invoke: (from) =>
111
+ from.effect("submit-login", ({ containingState }) =>
112
+ containingState.email === "fail"
113
+ ? Effect.fail("invalid credentials")
114
+ : Effect.succeed("/creator")).onDone((to) =>
115
+ to.full.Navigating().resolve(({ output, target }) => target.from({ href: output }))).onFailure((to) =>
116
+ to.branch.Editing.Form.Failed().resolve(({ target }) =>
117
+ target.from({ message: "Email or password is incorrect." })
118
+ ))
119
+ },
120
+ RequestingVerification: {
121
+ invoke: (from) =>
122
+ from.effect("request-verification", ({ containingState }) =>
123
+ containingState.email === "fail"
124
+ ? Effect.fail("verification unavailable")
125
+ : Effect.succeed(undefined)).onDone((to) =>
126
+ to.full.Verification.initial.resolve(({ containingState, target }) =>
127
+ target.from({
128
+ mode: containingState.mode,
129
+ email: containingState.email,
130
+ code: ""
131
+ })
132
+ )).onFailure((to) =>
133
+ to.branch.Editing.Form.Failed().resolve(({ target }) =>
134
+ target.from({ message: "The verification code could not be sent." })
135
+ ))
136
+ }
137
+ }
138
+ },
139
+ Verification: {
140
+ states: {
141
+ CodeEntry: {}
142
+ }
143
+ },
144
+ Navigating: {}
145
+ })