Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. import { combineReducers } from "redux";
  2. import NotificationReducer from "./NotificationReducer";
  3.  
  4. export default function getRootReducer(navReducer) {
  5. return combineReducers({
  6. nav: navReducer,
  7. notificationReducer: NotificationReducer
  8. });
  9. }
  10.  
  11. const initialState = {
  12. NotificationCount: 0
  13. };
  14.  
  15. export default function notifications(state = initialState, action = {}) {
  16. switch (action.type) {
  17. case 'SET_COUNT' :
  18. console.log('REDUCER NOTIFICATION SET_COUNT',state)
  19. return {
  20. ...state,
  21. NotificationCount: action.payload
  22. };
  23.  
  24. default:
  25. return state;
  26. }
  27. };
  28.  
  29. export function setNotificationCount(count) {
  30. return function (dispatch, getState) {
  31. console.log('Action - setNotificationCount: '+count)
  32. dispatch( {
  33. type: 'SET_COUNT',
  34. payload: count,
  35. });
  36. };
  37. };
  38.  
  39. import React, { Component } from 'react';
  40. import { View, Text, StyleSheet, ScrollView, Dimensions, TouchableOpacity, SectionList, Alert } from 'react-native';
  41. import Icon from 'react-native-vector-icons/Ionicons';
  42. import { Notification } from '@Components';
  43. import { ORANGE } from '@Theme/colors';
  44. import { NotificationService } from '@Services';
  45. import Style from './style';
  46.  
  47. import { connect } from 'react-redux';
  48. import { bindActionCreators } from 'redux';
  49. import * as Actions from '@Redux/Actions';
  50.  
  51. const width = Dimensions.get('window').width
  52. const height = Dimensions.get('window').height
  53.  
  54. export class NotificationsClass extends Component {
  55.  
  56. constructor(props) {
  57. super(props);
  58. this.state = {
  59. dataSource: [],
  60. NotificationCount: undefined
  61. };
  62. }
  63.  
  64. async componentWillMount() {
  65. this.updateNotifications();
  66. }
  67.  
  68. componentWillReceiveProps(nextProps){
  69. console.log('receive new props',nextProps);
  70. }
  71.  
  72. async updateNotifications() {
  73. this.props.setNotificationCount(10); <---
  74. let data = await NotificationService.get();
  75. if (data && data.data.length > 0) {
  76. this.setState({ dataSource: data });
  77. console.log(this.props) <-- NotificationCount is undefined
  78. }
  79. }
  80.  
  81.  
  82. render() {
  83. if (this.state.dataSource.length > 0) {
  84. return (
  85. <SectionList
  86. stickySectionHeadersEnabled
  87. refreshing
  88. keyExtractor={(item, index) => item.notificationId}
  89. style={Style.container}
  90. sections={this.state.dataSource}
  91. renderItem={({ item }) => this.renderRow(item)}
  92. renderSectionHeader={({ section }) => this.renderSection(section)}
  93. />
  94. );
  95. } else {
  96. return this.renderEmpty();
  97. }
  98. }
  99.  
  100. renderRow(data) {
  101. return (
  102. <TouchableOpacity activeOpacity={0.8} key={data.notificationId}>
  103. <Notification data={data} />
  104. </TouchableOpacity>
  105. );
  106. }
  107.  
  108.  
  109. }
  110.  
  111.  
  112. const Notifications = connect(
  113. state => ({
  114. NotificationCount: state.NotificationCount
  115. }),
  116. dispatch => bindActionCreators(Actions, dispatch)
  117. )(NotificationsClass);
  118.  
  119.  
  120. export { Notifications };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement