Skip to content

Commit

Permalink
Merge pull request #183 from cerebral/proxyclasses
Browse files Browse the repository at this point in the history
Proxyclasses
  • Loading branch information
christianalfoni authored Jan 11, 2019
2 parents 8cf2d07 + 197f98e commit aeb9b92
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 52 deletions.
3 changes: 2 additions & 1 deletion packages/node_modules/overmind/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
ResolveState,
Execution,
ResolveMockActions,
NestedPartial,
} from './internalTypes'
import { proxifyEffects } from './proxyfyEffects'
import {
Expand Down Expand Up @@ -78,7 +79,7 @@ function deepCopy(obj) {

export function createMock<Config extends Configuration>(
config: Config,
mockedEffects?: Partial<Config['effects']>
mockedEffects?: NestedPartial<Config['effects']>
): {
actions: ResolveMockActions<Config['actions']>
state: ResolveState<Config['state']>
Expand Down
4 changes: 4 additions & 0 deletions packages/node_modules/overmind/src/internalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export type SubType<Base, Condition> = Pick<
{ [Key in keyof Base]: Base[Key] extends Condition ? Key : never }[keyof Base]
>

export type NestedPartial<T> = T extends object
? Partial<{ [P in keyof T]: NestedPartial<T[P]> }>
: T

export type Options = {
name?: string
devtools?: string | boolean
Expand Down
2 changes: 1 addition & 1 deletion packages/node_modules/overmind/src/proxyfyEffects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function wrapEffect(target, prop, path, cb) {
const effectId = currentEffectId++

return cb({
func: target[prop],
func: target[prop].bind(target),
effectId,
name: path,
method: prop,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ export const loadPosts: Action = async ({ state, jsonPlaceholder }) => {
view,
`
import { state } from './state'
import * as effects from './effects'
import * as actions from './actions'
const config = {
state,
effects,
actions
}
`
Expand All @@ -43,6 +45,12 @@ export const overmind = new Overmind({
isLoadingPosts: false,
posts: []
},
effects: {
getPosts() {
return fetch('https://jsonplaceholder.typicode.com/posts')
.then(response => response.json())
}
},
actions: {
loadPosts: async ({ state, jsonPlaceholder }) => {
state.isLoadingPosts = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ export const jsonPlaceholder = {
view,
`
import state from './state'
import * as actions from './actions'
import * as effects from './effects'
const config = {
state,
actions,
effects
}
`
Expand All @@ -46,13 +44,6 @@ export const overmind = new Overmind({
isLoadingPosts: false,
posts: []
},
actions: {
loadPosts: async ({ state, jsonPlaceholder }) => {
state.isLoadingPosts = true
state.posts = await jsonPlaceholder.getPosts()
state.isLoadingPosts = false
}
},
effects: {
getPosts() {
return fetch('https://jsonplaceholder.typicode.com/posts')
Expand Down
14 changes: 6 additions & 8 deletions packages/overmind-website/guides/beginner/01_getstarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,20 @@ h(Example, { name: "guide/getstarted/connectapp" })

That is it, your component will now render whenever the state accessed changes.

## Loading posts
## Effects

We want to load some posts from [jsonplaceholder](https://jsonplaceholder.typicode.com/) when the **Posts** component mounts. To run logic in Overmind you trigger **actions**. Let us define an action that is responsible for getting our application up and running.
We want to load some posts from [jsonplaceholder](https://jsonplaceholder.typicode.com/) when the **Posts** component mounts. This requires a side effect and we can expose any kind of side effects to our Overmind instance. Think of it as injecting libraries and tools. For example, it could be the [axios](https://www.npmjs.com/package/axios) library itself, some class instance we created or just a plain object as we see in the example below. Doing this injection keeps our logic pure and Overmind knows when these injected libraries are accessed, giving you a better developer experience.

```marksy
h(Example, { name: "guide/getstarted/actions" })
h(Example, { name: "guide/getstarted/effects" })
```

Let us see how we define this effect called **jsonPlaceholder**.
## Loading the posts

## Effects

We can expose any kind of side effects to our Overmind instance. Think of it as injecting libraries and tools. For example, it could be the [axios](https://www.npmjs.com/package/axios) library itself, some class instance we created or just a plain object as we see in the example below. Doing this injection keeps our actions pure and Overmind knows when these injected libraries are accessed.
To run logic in Overmind you trigger **actions**. Let us define an action that is responsible for getting our application up and running.

```marksy
h(Example, { name: "guide/getstarted/effects" })
h(Example, { name: "guide/getstarted/actions" })
```

Finally we need to trigger our action and we will do that when the component mounts.
Expand Down
2 changes: 1 addition & 1 deletion packages/overmind-website/src/components/App/styles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { css } from 'emotion'

export const pageWrapper = css`
min-height: calc(100vh - 198px);
min-height: calc(100vh);
`

export const wrapper = css`
Expand Down
1 change: 1 addition & 0 deletions packages/overmind-website/src/components/Guides/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const wrapper = css`
margin-top: 50px;
margin-left: auto;
margin-right: auto;
justify-content: center;
`

export const guide = css`
Expand Down
1 change: 1 addition & 0 deletions packages/overmind-website/src/components/Videos/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const wrapper = css`
padding: var(--padding-5);
flex-wrap: wrap;
margin-top: 50px;
justify-content: center;
`

export const video = css`
Expand Down
37 changes: 16 additions & 21 deletions packages/overmind-website/src/components/ViewSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as ReactImage from '../../images/react.png'
import * as VueImage from '../../images/vue.png'
import * as AngularImage from '../../images/angular.png'
import * as TsImage from '../../images/ts.png'
import * as TsImageGrayscale from '../../images/ts-grayscale.png'
import * as JsImage from '../../images/js.png'
import Icon from '../Icon'
import * as styles from './styles'
import { css } from 'emotion'
Expand All @@ -25,49 +25,44 @@ const ViewSelector: SFC = () => {
}

const options = {
vue: (
<div className={styles.viewOption}>
<img src={VueImage} width={25} />
Vue
<img className={styles.image} src={JsImage} width="20" height="20" />
</div>
),
react: (
<div className={styles.viewOption}>
<img src={ReactImage} width={25} />
React
<img className={styles.image} src={JsImage} width="20" height="20" />
</div>
),
vue: (
react_ts: (
<div className={styles.viewOption}>
<img src={VueImage} width={25} />
Vue
<img src={ReactImage} width={25} />
React
<img className={styles.image} src={TsImage} width="20" height="20" />
</div>
),
angular: (
angular_ts: (
<div className={styles.viewOption}>
<img src={AngularImage} width={25} />
Angular
<img className={styles.image} src={TsImage} width="20" height="20" />
</div>
),
}

return (
<div className={styles.wrapper}>
<div
className={styles.tsImageWrapper}
onClick={state.showViewHelp ? null : actions.toggleTypescript}
>
{state.typescript ? (
<img className={styles.image} src={TsImage} width="20" height="20" />
) : (
<img
className={css(styles.image, styles.grayscale)}
src={TsImageGrayscale}
width="20"
height="20"
/>
)}
</div>
<div
ref={selectorRef}
onClick={state.showViewHelp ? null : onSelectorClick}
className={css(styles.selector, isOpen && styles.selectorOpen)}
>
{options[state.theme]}
{options[state.theme + (state.typescript ? '_ts' : '')]}
<span className={styles.chevron}>
<Icon>chevron-down</Icon>
</span>
Expand Down
14 changes: 5 additions & 9 deletions packages/overmind-website/src/components/ViewSelector/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ export const wrapper = css`
export const viewOption = css`
display: flex;
align-items: center;
`

export const tsImageWrapper = css`
display: flex;
padding: 4px;
align-items: center;
border-top-left-radius: var(--border-radius-1);
border-bottom-left-radius: var(--border-radius-1);
width: 100%;
img:last-child {
margin-left: auto;
}
`

export const image = css`
Expand Down Expand Up @@ -59,7 +55,7 @@ export const selectorOpen = css`
`

export const chevron = css`
margin-left: auto;
margin-left: 5px;
font-size: var(--font-size-1);
`

Expand Down
Binary file added packages/overmind-website/src/images/js.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
9 changes: 8 additions & 1 deletion packages/overmind-website/src/overmind/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,21 @@ export const openApi: Action<RouteContext<VideoParams>> = async ({
}

export const selectTheme: Action<string> = ({
value: theme,
value: selection,
state,
css,
storage,
}) => {
const selectionArray = selection.split('_')
const theme = selectionArray[0]
const typescript = Boolean(selectionArray[1])

state.theme = theme
state.typescript = typescript

css.changePrimary(theme)
storage.set('theme', theme)
storage.set('typescript', typescript)
}

export const toggleTypescript: Action = ({ state, storage }) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/overmind-website/src/overmind/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type State = {
}

const state: State = {
page: null,
page: Page.HOME,
currentGuide: null,
currentVideo: null,
currentApi: null,
Expand Down

0 comments on commit aeb9b92

Please sign in to comment.