Last active
July 4, 2017 06:18
-
-
Save mariusandra/8dbdd623761252039e98ccefe9dce434 to your computer and use it in GitHub Desktop.
inline kea
This file contains 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 React, { Component } from 'react' | |
import { connect, inline } from 'kea/logic' | |
import { put } from 'redux-saga/effects' | |
import someLogic from './some/logic' | |
@connect({ | |
actions: [ | |
someLogic, [ | |
'doSomething' | |
] | |
], | |
props: [ | |
someLogic, [ | |
'firstProp', | |
'secondProp', | |
] | |
] | |
}) | |
@inline({ | |
actions: () => ({ | |
updateName: name => ({ name }) | |
}), | |
reducers: ({ props, actions }) => ({ | |
name: ['Chirpy', PropTypes.string, { | |
[actions.updateName]: (state, payload) => payload.name | |
}] | |
}), | |
selectors: ({ props, selectors }) => ({ | |
capitalizedName: [ | |
() => [selectors.name], | |
(name) => name.trim().split(' ').map(k => `${k.charAt(0).toUpperCase()}${k.slice(1).toLowerCase()}`).join(' '), | |
PropTypes.string | |
] | |
}), | |
start: function * () { | |
console.log('The component was created, running base saga.') | |
const something = yield someLogic.get('something') | |
console.log(`value of something: ${something}`) | |
}, | |
stop: function * () { | |
console.log('The component was destroyed and the saga got cancelled') | |
}, | |
takeEvery: (actions, workers) => ({ | |
[actions.updateName]: workers.updateNameWorker | |
}), | |
workers: { | |
updateNameWorker: function * (action) { | |
const { doSomething } = this.actions | |
const { name } = action.payload | |
console.log(`New name: ${name}`) | |
yield put(doSomething('bla bla')) | |
const capitalizedName = yield this.get('capitalizedName') | |
console.log(`Capitalized name: ${capitalizedName}`) | |
} | |
} | |
}) | |
class Something extends Component { | |
handleClick = () => { | |
const { name } = this.props | |
const { updateName } = this.props.actions | |
const newName = window.propmpt('Enter a new name', name) | |
if (newName) { | |
updateName(newName) | |
} | |
} | |
render () { | |
const { capitalizedName, firstProp, secondProp } = this.props | |
const { doSomething } = this.actions | |
return ( | |
<div> | |
<div onClick={this.handleClick}> | |
{capitalizedName} | |
</div> | |
<div onClick={doSomething}> | |
{firstProp} {secondProp} | |
</div> | |
</div> | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment