Guest User

Untitled

a guest
Jan 23rd, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. //REDUCER Exemple
  2. import {Map} from 'immutable';
  3. import {loop, Effects} from 'redux-loop-symbol-ponyfill';
  4. import {post} from '../../utils/api';
  5.  
  6. const initialState = Map({
  7. cars: [],
  8. loading: false
  9. });
  10.  
  11. const GET_CAR_REQUEST = 'ProfilState/GET_CAR_REQUEST';
  12. const GET_CAR_RESPONSE = 'ProfilState/GET_CAR_RESPONSE';
  13.  
  14. export function getCar() {
  15. return {type: GET_CAR_REQUEST};
  16. }
  17.  
  18. async function getCarResponse() {
  19. return {
  20. type: GET_CAR_RESPONSE,
  21. payload: await post('/graphql', {
  22. query: `{
  23. getVehicule {
  24. _id
  25. modele
  26. marque
  27. plaque
  28. couleur
  29. picture
  30. nb_places
  31. }
  32. }`
  33. })
  34. };
  35. }
  36.  
  37. export default function ProfilStateReducer(state = initialState, action = {}) {
  38. switch (action.type) {
  39. case GET_CAR_REQUEST:
  40. return loop(
  41. state.set('loading', true),
  42. Effects.promise(getCarResponse)
  43. );
  44.  
  45. case GET_CAR_RESPONSE:
  46. return state
  47. .set('loading', false)
  48. .set('cars', action.payload.data.getVehicule);
  49.  
  50. default:
  51. return state;
  52. }
  53. }
  54.  
  55.  
  56. // Connect Container Component
  57. import {connect} from 'react-redux';
  58. import {bindActionCreators} from 'redux';
  59. import {NavigationActions} from 'react-navigation';
  60. import ProfilView from './ProfilView';
  61. import * as ProfilStateActions from './ProfilState';
  62.  
  63. export default connect(
  64. state => ({
  65. cars: state.getIn(['profil', 'cars']),
  66. user: state.getIn(['login', 'user'])
  67. }),
  68. dispatch => {
  69. return {
  70. navigate: bindActionCreators(NavigationActions.navigate, dispatch),
  71. profilStateActions: bindActionCreators(ProfilStateActions, dispatch)
  72. };
  73. }
  74. )(ProfilView);
  75.  
  76.  
  77. //How I fetch my data from my reducer
  78. componentWillMount() {
  79. this.props.profilStateActions.getCar();
  80. }
  81.  
  82. //How I render my data, in my render
  83. renderHeader = () => {
  84. if (!this.props.cars) {
  85. return null;
  86. }
  87. else {
  88. return (<HeaderProfil {...this.props} headerHeight={300}/>);
  89. }
  90. }
  91.  
  92. render() {
  93. return (<View>{this.renderHeader()}</View>)
  94. }
Add Comment
Please, Sign In to add comment