Skip to content

Instantly share code, notes, and snippets.

@ketemartinsrufino
Created February 6, 2018 00:32
Show Gist options
  • Save ketemartinsrufino/64e0dc71a041a523087f094256b144a9 to your computer and use it in GitHub Desktop.
Save ketemartinsrufino/64e0dc71a041a523087f094256b144a9 to your computer and use it in GitHub Desktop.
export const listar = lista => {
console.log('action listar')
return {
type: 'LISTAR',
lista,
}
};
import React, { Component } from 'react';
import 'material-components-web/dist/material-components-web.css';
import MeasureListItem from './measure-list-item';
import { connect } from 'react-redux';
import { listar } from './redux/actions';
class MeasureList extends Component {
componentDidMount() {
this.props.dispatch({type: 'LISTAR'})
}
itemClick(payload) {
this.props.dispatch({type: 'EDITAR', payload});
}
render() {
this.props.lista = this.props.lista || [];
const items = this.props.lista.map(
item => (<MeasureListItem item={item} key= {'aaa'} onClick={this.itemClick.bind(this, item)}/> )
);
return (
<div className="mdc-list mdc-layout-grid__cell">
<ul>
{items}
</ul>
</div>
);
}
}
const mapStateToProps = (state) => ({lista: state.lista});
const mapDispatchToProps = dispatch => ({
listar: lista => dispatch(lista => listar(lista))
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(MeasureList);
import {actionTypes} from './actions';
const defaultItem = {
key: '',
datacriacao: new Date().getTime(),
peso: '',
altura: '',
cintura: '',
abdomen: '',
quadril: '',
imc: '',
};
export const lista = (state = [], action) => {
console.log('reducer listar. action: ', action);
if(action.type === 'successfull' ) {
return action.payload;
}
return state;
};
export const item = (state = defaultItem, action) => {
switch (action.type) {
case 'EDIT':
return action.payload;
case 'UPDATE_IMC':
return action.payload;
case 'SAVE':
return action.payload;
case 'DELETE':
return {};
default:
return state;
}
}
import { listar } from 'modules/measure/redux/actions';
function* listSuccessfull() {
const measures = yield window.firebase.database().ref("measures/");
const payload = yield call(fetchList, measures);
let list = Object.keys(payload).map((k) => ({...payload[k], key: k}));
yield put(listar(list));
}
const fetchList = (ref) => {
return new Promise((resolve, reject) => {
ref.on('value', data => {
resolve(data.val())
});
});
};
function* watchList() {
yield takeLatest('LISTAR' , listSuccessfull);
};
function* rootSaga() {
yield fork(watchList);
}
export default rootSaga;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment