Last active
September 20, 2017 04:58
-
-
Save jschr/ca335b9b158f29d6e5595f5372308eff to your computer and use it in GitHub Desktop.
state-provider api
This file contains hidden or 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
import { createStateProvider, createStateSlicer } from 'state-provider' | |
import ProfileForm from './ProfileForm' | |
const Provider = createStateProvider('app') | |
const Slicer = createStateSlicer('app') | |
async function saveUser(user) { | |
await api.saveUser(user) | |
} | |
const state = { | |
user: null | |
} | |
const reducer = (state, action) => { | |
switch (action.type) { | |
// reduce | |
default: | |
return state | |
} | |
} | |
const enhancer = next => (...args) => { | |
const store = next(...args) | |
// enhance | |
return store | |
} | |
export default function App() { | |
return ( | |
<Provider state={state} reducer={reducer} enhancer={enhancer}> | |
<Slicer slice='user' onValue={saveUser}> | |
{({ value, setValue }) => | |
<ProfileForm user={value} saveUser={setValue} /> | |
} | |
</Slicer> | |
</Provider> | |
) | |
} |
This file contains hidden or 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
import { createStateProvider, createStateSlicer } from 'state-provider' | |
import api from '../api' | |
import { Form, Input, Button } from './' | |
const Provider = createStateProvider('form') | |
const Slice = createStateSlicer('form') | |
async function isUnique(username) { | |
await api.isUniqueUsername(username) | |
} | |
export default function UserForm({ user, saveUser }) { | |
return ( | |
<Provider state={user} onCommit={saveUser}> | |
{({ commit, onCommitError }) => | |
<Form onSubmit={commit}> | |
<Slicer slice='username' onValue={isUnique}> | |
{({ value, setValue, onValueError }) => | |
<Input type='text' value={value} onChange={setValue} /> | |
} | |
</Slice> | |
<Button label='Save' /> | |
</Form> | |
} | |
</Provider> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment