-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial port to redux #11
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { createAction } from 'redux-act' | ||
|
||
import { Category, Contract, Instructions } from 'reducers' | ||
|
||
export interface FormToSend { | ||
provider: string | ||
pair: string | ||
updateAfter: string | ||
retireAfter: string | ||
} | ||
|
||
export const setConfig = createAction('set fetched config', (res: Category[]) => res) | ||
export const setResults = createAction('set contract results', (res: { contract: Contract, instructions: Instructions }) => res) | ||
export const sendForm = createAction('send form with params', (res: FormToSend) => res) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export const API_URL = "http://localhost:3091" | ||
export const API_URL = "http://localhost:8081" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It will be move to a config file |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,36 @@ | ||
import React, { PureComponent } from 'react' | ||
|
||
import { API_URL } from 'config' | ||
|
||
import ConstructForm, { State as Form } from './ConstructForm' | ||
import { connect } from 'react-redux' | ||
import { withRouter } from 'react-router' | ||
import { History } from 'history' | ||
|
||
import Page from 'components/Page' | ||
import ConstructForm from './ConstructForm' | ||
import { StoreState } from 'reducers' | ||
import { sendForm, FormToSend } from 'actions' | ||
|
||
interface Props { | ||
history: History | ||
config: StoreState['config'] | ||
sendForm: (a: FormToSend) => void | ||
} | ||
|
||
export interface Category { | ||
name: string | ||
types?: string[] // not needed | ||
providers?: { | ||
id: string | ||
name: string | ||
types: string[] | ||
}[] | ||
} | ||
|
||
interface StateNew { | ||
categories: { | ||
name: string, | ||
meta?: { | ||
providers: { | ||
id: string | ||
name: string | ||
types: string[] | ||
}[] | ||
} | ||
}[] | ||
} | ||
|
||
interface State { | ||
categories?: Category[] | ||
} | ||
|
||
// TEMP | ||
declare global { | ||
interface Window { store: any; } | ||
} | ||
|
||
class ConstructPage extends PureComponent<Props, State> { | ||
state = { | ||
categories: null | ||
} as State | ||
|
||
onFormSubmit = (form: Form) => { | ||
window.store = { form } | ||
class ConstructPage extends PureComponent<Props> { | ||
onFormSubmit = (form: FormToSend) => { | ||
this.props.sendForm(form) | ||
this.props.history.push('contractCode') | ||
} | ||
|
||
async componentWillMount() { | ||
const res = await fetch(`${API_URL}/config`) | ||
const json = await res.json() as { categories: Category[] } | ||
this.setState(json) | ||
} | ||
|
||
render() { | ||
return ( | ||
<Page title='Choose data to receive'> | ||
<ConstructForm | ||
data={this.state.categories} | ||
data={this.props.config} | ||
onSubmit={this.onFormSubmit} /> | ||
</Page> | ||
) | ||
} | ||
} | ||
|
||
export default withRouter(ConstructPage as any) | ||
const mapStateToProps = ({ config }: StoreState) => ({ config }) | ||
const mapDispatchToProps = { sendForm } | ||
export default connect(mapStateToProps, mapDispatchToProps)(withRouter(ConstructPage as any)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { take, call, put, fork } from 'redux-saga/effects' | ||
|
||
import { setConfig, sendForm, setResults } from 'actions' | ||
import { API_URL } from 'config' | ||
|
||
function* requestConfig() { | ||
const res = yield call(fetch, `${API_URL}/config`) | ||
const { categories } = yield res.json() | ||
yield put(setConfig(categories)) | ||
} | ||
|
||
function* requestResults() { | ||
while (true) { | ||
const { payload } = yield take(sendForm) | ||
const { provider, pair, updateAfter, retireAfter } = payload | ||
const res = yield call(fetch, `${API_URL}/generate/eos/crypto/${provider}/${pair}?updatefreq=${updateAfter}&lifetime=${retireAfter}`) | ||
const result = yield res.json() | ||
yield put(setResults(result)) | ||
} | ||
} | ||
|
||
export default function* constructSaga() { | ||
yield fork(requestConfig) | ||
yield fork(requestResults) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { createReducer } from 'redux-act' | ||
import { combineReducers } from 'redux' | ||
|
||
import { setConfig, setResults } from 'actions' | ||
|
||
export type Contract = string | ||
export type Instructions = string | ||
|
||
export interface Category { | ||
name: string | ||
types?: string[] // not needed | ||
providers?: { | ||
id: string | ||
name: string | ||
types: string[] | ||
}[] | ||
} | ||
|
||
export interface StoreState { | ||
config: Category[], | ||
contract: Contract, | ||
instructions: Instructions | ||
} | ||
|
||
export const config = createReducer({ | ||
[setConfig.toString()]: (_, payload: Category[]) => payload | ||
}, []) | ||
|
||
export const contract = createReducer({ | ||
[setResults.toString()]: (_, { contract }) => contract | ||
}, '') | ||
|
||
export const instructions = createReducer({ | ||
[setResults.toString()]: (_, { instructions }) => instructions | ||
}, '') | ||
|
||
export default combineReducers({ config, contract, instructions }) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { all, take, call, put, fork } from 'redux-saga/effects' | ||
|
||
import { setConfig, sendForm, setResults } from 'actions' | ||
import { API_URL } from 'config' | ||
|
||
function* requestConfig() { | ||
const res = yield call(fetch, `${API_URL}/config`) | ||
const { categories } = yield res.json() | ||
yield put(setConfig(categories)) | ||
} | ||
|
||
function* requestResults() { | ||
while (true) { | ||
try { | ||
const { payload } = yield take(sendForm) | ||
const { provider, pair, updateAfter, retireAfter } = payload | ||
const res = yield call(fetch, `${API_URL}/generate/eos/crypto/${provider}/${pair}?updatefreq=${updateAfter}&lifetime=${retireAfter}`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Parameters can contain special chars, we should |
||
const result = yield res.json() | ||
yield put(setResults(result)) | ||
} catch (err) { | ||
console.log(err) | ||
} | ||
} | ||
} | ||
|
||
export default function* constructSaga() { | ||
yield all([ | ||
fork(requestConfig), | ||
fork(requestResults) | ||
]) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍