Skip to content

Commit 5cb9d82

Browse files
docs(website): add medium article and side effects guide
1 parent d3f167c commit 5cb9d82

File tree

7 files changed

+209
-1
lines changed

7 files changed

+209
-1
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import tsAppIndex from '../../tsAppIndex'
2+
3+
function createJsCode(view) {
4+
return [
5+
{
6+
fileName: 'app/effects.js',
7+
code: `
8+
export { default as http } from 'axios'
9+
`,
10+
},
11+
{
12+
fileName: 'app/index.js',
13+
code: `
14+
import App from '${view}'
15+
import * as state from './state'
16+
import * as actions from './actions'
17+
import * as effects from './effects'
18+
19+
const app = new App({
20+
state,
21+
actions,
22+
effects
23+
})
24+
25+
export default app
26+
`,
27+
},
28+
]
29+
}
30+
31+
function createTsCode(view) {
32+
return [
33+
{
34+
fileName: 'app/effects.ts',
35+
code: `
36+
export { default as http } from 'axios'
37+
`,
38+
},
39+
{
40+
fileName: 'app/index.ts',
41+
code: tsAppIndex(
42+
view,
43+
`
44+
import * as state from './state'
45+
import * as actions from './actions'
46+
import * as effects from './effects'
47+
48+
const config = {
49+
state,
50+
actions,
51+
effects
52+
}
53+
`
54+
),
55+
},
56+
]
57+
}
58+
59+
export const react = createJsCode('react-overmind')
60+
61+
export const reactTs = createTsCode('react-overmind')
62+
63+
export const vue = createJsCode('vue-overmind')
64+
65+
export const vueTs = createTsCode('vue-overmind')
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
export const js = [
2+
{
3+
fileName: 'app/effects.js',
4+
code: `
5+
import axios from 'axios'
6+
7+
class Http {
8+
constructor (baseUrl, request) {
9+
this.baseUrl = baseUrl
10+
this.request = request
11+
}
12+
getUser() {
13+
return this.request.get(\`\${this.baseUrl}/user\`)
14+
}
15+
}
16+
17+
export const http =
18+
new Http(IS_PRODUCTION ? '/api/v1' : 'http://localhost:4321', axios)
19+
`,
20+
},
21+
]
22+
23+
export const ts = [
24+
{
25+
fileName: 'app/effects.ts',
26+
code: `
27+
import axios from 'axios'
28+
import { User } from './state'
29+
30+
interface IHttp {
31+
get<T>(url: string): Promise<T>
32+
}
33+
34+
class Http {
35+
private baseUrl: string
36+
private request: IHttp
37+
constructor (baseUrl: string, request: IHttp) {
38+
this.baseUrl = baseUrl
39+
this.request = request
40+
}
41+
getUser(): Promise<User> {
42+
return this.request.get(\`\${this.baseUrl}/user\`)
43+
}
44+
}
45+
46+
export const http =
47+
new Http(IS_PRODUCTION ? '/api/v1' : 'http://localhost:4321', axios)
48+
`,
49+
},
50+
]
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export const js = [
2+
{
3+
fileName: 'app/operations.js',
4+
code: `
5+
export const getUser = ({ http }) =>
6+
http.get('/user')
7+
`,
8+
},
9+
]
10+
11+
export const ts = [
12+
{
13+
fileName: 'app/operations.ts',
14+
code: `
15+
import { Map } from './'
16+
import { User } from './state'
17+
18+
export const getUser: Map<any, Promise<User>> = ({ http }) =>
19+
http.get<User>('/user')
20+
`,
21+
},
22+
]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export const js = [
2+
{
3+
fileName: 'app/effects.js',
4+
code: `
5+
import axios from 'axios'
6+
7+
export const http = {
8+
getUser() {
9+
return axios.get('/user')
10+
}
11+
}
12+
`,
13+
},
14+
]
15+
16+
export const ts = [
17+
{
18+
fileName: 'app/effects.ts',
19+
code: `
20+
import * as axios from 'axios'
21+
import { User } from './state'
22+
23+
export const http = {
24+
getUser() {
25+
return axios.get<User>('/user')
26+
}
27+
}
28+
`,
29+
},
30+
]

packages/overmind-website/guides/beginner/01_getstarted.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Get started with Overmind
22

3-
Before you fire up your first Overmind application be sure to check out the [Why Overmind?]() guide.
3+
Before you fire up your first Overmind application you might want to read the release article [There is more to state management than state](https://medium.com/@christianalfoni/there-is-more-to-state-management-than-state-60ad75e24ea6).
44

55
To get started with Overmind you have to set up a project. You can do this with [webpack](https://webpack.js.org/) or [parceljs](https://parceljs.org/) on your local machine, or go to [codesandbox.io](https://codesandbox.io/) to play around with Overmind directly in the browser.
66

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Running side effects
2+
3+
Developing applications is not only about managing state, but also managing side effects. A side effect is typically exampled with an http request or talking to local storage. In Overmind we just call this **effects**. Actually the state is also a type of effect. Unlike state all other effects has to be defined.
4+
5+
Let us start with a simple example.
6+
7+
```marksy
8+
<Example name="guide/runningsideeffects/axios" view />
9+
```
10+
11+
We are just exporting an existing library from our effects file and include it in the application config. Now Overmind is aware of an **http** effect. It can track it for debugging and all operations will have it injected.
12+
13+
Let us put it to use in an operation that grabs the user of the application.
14+
15+
```marksy
16+
<Example name="guide/runningsideeffects/getuser" />
17+
```
18+
19+
That was basically it. We can take this a step further though. Maybe you want to create a more explicit API effect.
20+
21+
```marksy
22+
<Example name="guide/runningsideeffects/object" />
23+
```
24+
25+
Or maybe you need it to be configurable. Improving testability and environment variables.
26+
27+
```marksy
28+
<Example name="guide/runningsideeffects/class" />
29+
```
30+
31+
## Summary
32+
Adding side effects is easy, but it is worth taking notice that you also get a chance to map an existing tool to something more domain specific. As we did here converting the general **get** method to a **getUser** method. If you think about it from an application standpoint it is kinda weird that it makes arbitrary requests to a string url. It is better to create an abstraction around it to keep things more maintainable and predictable.

packages/overmind-website/src/utils.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,13 @@ export const compile = marksy({
146146
<img src={`/images/${src}`} style={{ width: '100%' }} />
147147
),
148148
},
149+
elements: {
150+
a({ href, children }) {
151+
return (
152+
<a href={href} target={href.indexOf('http') === 0 ? '_blank' : null}>
153+
{children}
154+
</a>
155+
)
156+
},
157+
},
149158
})

0 commit comments

Comments
 (0)