Skip to content

Commit f1444cf

Browse files
docs(website): add charts guide
1 parent bfcee01 commit f1444cf

File tree

7 files changed

+608
-0
lines changed

7 files changed

+608
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
export default (ts, view) =>
2+
ts
3+
? [
4+
{
5+
fileName: 'overmind/login/actions.ts',
6+
code: `
7+
import { Action, AsyncAction } from 'overmind'
8+
import { User } from './state'
9+
10+
export const changeUsername: Action<string> = ({ state }, username) => {
11+
state.login.username = username
12+
}
13+
14+
export const changePassword: Action<string> = ({ state }, password) => {
15+
state.login.password = password
16+
}
17+
18+
export const login: AsyncAction = ({ state, actions, effects }) => {
19+
try {
20+
const user = await effects.api.login(state.username, state.password)
21+
actions.login.resolveUser(user)
22+
} catch (error) {
23+
actions.login.rejectUser(error)
24+
}
25+
}
26+
27+
export const resolveUser: Action<User> = ({ state }, user) => {
28+
state.login.user = user
29+
}
30+
31+
export const rejectUser: Action<Error> = ({ state }, error) => {
32+
state.login.authenticationError = error.message
33+
}
34+
35+
export const logout: Action = ({ effects }) => {
36+
effects.api.logout()
37+
}
38+
39+
export const tryAgain: Action = () => {}
40+
`,
41+
},
42+
]
43+
: [
44+
{
45+
fileName: 'overmind/login/actions.ts',
46+
code: `
47+
export const changeUsername = ({ state }, username) => {
48+
state.login.username = username
49+
}
50+
51+
export const changePassword = ({ state }, password) => {
52+
state.login.password = password
53+
}
54+
55+
export const login = ({ state, actions, effects }) => {
56+
try {
57+
const user = await effects.api.login(state.username, state.password)
58+
actions.login.resolveUser(user)
59+
} catch (error) {
60+
actions.login.rejectUser(error)
61+
}
62+
}
63+
64+
export const resolveUser = ({ state }, user) => {
65+
state.login.user = user
66+
}
67+
68+
export const rejectUser = ({ state }, error) => {
69+
state.login.authenticationError = error.message
70+
}
71+
72+
export const logout = ({ effects }) => {
73+
effects.api.logout()
74+
}
75+
76+
export const tryAgain = () => {}
77+
`,
78+
},
79+
]
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
export default (ts, view) =>
2+
ts
3+
? [
4+
{
5+
fileName: 'overmind/login/index.ts',
6+
code: `
7+
import { Statechart, statechart } from 'overmind/config'
8+
import * as actions from './actions'
9+
import { state } from './state'
10+
11+
const config = {
12+
state,
13+
actions
14+
}
15+
16+
enum LoginState {
17+
LOGIN = 'LOGIN',
18+
AUTHENTICATING = 'AUTHENTICATING',
19+
AUTHENTICATED = 'AUTHENTICATED',
20+
ERROR = 'ERROR'
21+
}
22+
23+
const loginChart: Statechart<typeof config, LoginState> = {
24+
initial: LoginState.LOGIN,
25+
states: {
26+
[LoginState.LOGIN]: {
27+
on: {
28+
changeUsername: null,
29+
changePassword: null,
30+
login: {
31+
target: LoginState.AUTHENTICATING,
32+
condition: state => Boolean(state.username && state.password)
33+
}
34+
}
35+
},
36+
...
37+
}
38+
}
39+
40+
export default statechart(config, loginChart)
41+
`,
42+
},
43+
]
44+
: [
45+
{
46+
fileName: 'overmind/login/index.ts',
47+
code: `
48+
import { statechart } from 'overmind/config'
49+
import * as actions from './actions'
50+
import { state } from './state'
51+
52+
const config = {
53+
state,
54+
actions
55+
}
56+
57+
const loginChart = {
58+
initial: 'LOGIN',
59+
states: {
60+
LOGIN: {
61+
on: {
62+
changeUsername: null,
63+
changePassword: null,
64+
login: {
65+
target: 'AUTHENTICATING',
66+
condition: state => Boolean(state.username && state.password)
67+
}
68+
}
69+
},
70+
...
71+
}
72+
}
73+
74+
export default statechart(config, loginChart)
75+
`,
76+
},
77+
]
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
export default (ts, view) =>
2+
ts
3+
? [
4+
{
5+
fileName: 'overmind/login/index.ts',
6+
code: `
7+
import { Statechart, statechart } from 'overmind/config'
8+
import * as actions from './actions'
9+
import { state } from './state'
10+
11+
const config = {
12+
state,
13+
actions
14+
}
15+
16+
enum LoginState {
17+
LOGIN = 'LOGIN',
18+
AUTHENTICATING = 'AUTHENTICATING',
19+
AUTHENTICATED = 'AUTHENTICATED',
20+
ERROR = 'ERROR'
21+
}
22+
23+
const loginChart: Statechart<typeof config, LoginState> = {
24+
initial: LoginState.LOGIN,
25+
states: {
26+
[LoginState.LOGIN]: {
27+
on: {
28+
changeUsername: null,
29+
changePassword: null,
30+
login: LoginState.AUTHENTICATING
31+
}
32+
},
33+
[LoginState.AUTHENTICATING]: {
34+
on: {
35+
resolveUser: LoginState.AUTHENTICATED,
36+
rejectUser: LoginState.ERROR
37+
}
38+
},
39+
[LoginState.AUTHENTICATED]: {
40+
on: {
41+
logout: LoginState.LOGIN
42+
}
43+
},
44+
[LoginState.ERROR]: {
45+
on: {
46+
tryAgain: LoginState.LOGIN
47+
}
48+
}
49+
}
50+
}
51+
52+
export default statechart(config, loginChart)
53+
`,
54+
},
55+
]
56+
: [
57+
{
58+
fileName: 'overmind/login/index.ts',
59+
code: `
60+
import { statechart } from 'overmind/config'
61+
import * as actions from './actions'
62+
import { state } from './state'
63+
64+
const config = {
65+
state,
66+
actions
67+
}
68+
69+
const loginChart = {
70+
initial: 'LOGIN',
71+
states: {
72+
LOGIN: {
73+
on: {
74+
changeUsername: null,
75+
changePassword: null,
76+
login: 'AUTHENTICATING'
77+
}
78+
},
79+
AUTHENTICATING: {
80+
on: {
81+
resolveUser: 'AUTHENTICATED',
82+
rejectUser: 'ERROR'
83+
}
84+
},
85+
AUTHENTICATED: {
86+
on: {
87+
logout: 'LOGIN'
88+
}
89+
},
90+
ERROR: {
91+
on: {
92+
tryAgain: 'LOGIN'
93+
}
94+
}
95+
}
96+
}
97+
98+
export default statechart(config, loginChart)
99+
`,
100+
},
101+
]
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export default (ts, view) =>
2+
ts
3+
? [
4+
{
5+
code: `
6+
state.login.matches(LoginState.Login)
7+
`,
8+
},
9+
]
10+
: [
11+
{
12+
code: `
13+
state.login.matches('LOGIN')
14+
`,
15+
},
16+
]

0 commit comments

Comments
 (0)