Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { combineReducers } from "redux";
- import NotificationReducer from "./NotificationReducer";
- export default function getRootReducer(navReducer) {
- return combineReducers({
- nav: navReducer,
- notificationReducer: NotificationReducer
- });
- }
- const initialState = {
- NotificationCount: 0
- };
- export default function notifications(state = initialState, action = {}) {
- switch (action.type) {
- case 'SET_COUNT' :
- console.log('REDUCER NOTIFICATION SET_COUNT',state)
- return {
- ...state,
- NotificationCount: action.payload
- };
- default:
- return state;
- }
- };
- export function setNotificationCount(count) {
- return function (dispatch, getState) {
- console.log('Action - setNotificationCount: '+count)
- dispatch( {
- type: 'SET_COUNT',
- payload: count,
- });
- };
- };
- import React, { Component } from 'react';
- import { View, Text, StyleSheet, ScrollView, Dimensions, TouchableOpacity, SectionList, Alert } from 'react-native';
- import Icon from 'react-native-vector-icons/Ionicons';
- import { Notification } from '@Components';
- import { ORANGE } from '@Theme/colors';
- import { NotificationService } from '@Services';
- import Style from './style';
- import { connect } from 'react-redux';
- import { bindActionCreators } from 'redux';
- import * as Actions from '@Redux/Actions';
- const width = Dimensions.get('window').width
- const height = Dimensions.get('window').height
- export class NotificationsClass extends Component {
- constructor(props) {
- super(props);
- this.state = {
- dataSource: [],
- NotificationCount: undefined
- };
- }
- async componentWillMount() {
- this.updateNotifications();
- }
- componentWillReceiveProps(nextProps){
- console.log('receive new props',nextProps);
- }
- async updateNotifications() {
- this.props.setNotificationCount(10); <---
- let data = await NotificationService.get();
- if (data && data.data.length > 0) {
- this.setState({ dataSource: data });
- console.log(this.props) <-- NotificationCount is undefined
- }
- }
- render() {
- if (this.state.dataSource.length > 0) {
- return (
- <SectionList
- stickySectionHeadersEnabled
- refreshing
- keyExtractor={(item, index) => item.notificationId}
- style={Style.container}
- sections={this.state.dataSource}
- renderItem={({ item }) => this.renderRow(item)}
- renderSectionHeader={({ section }) => this.renderSection(section)}
- />
- );
- } else {
- return this.renderEmpty();
- }
- }
- renderRow(data) {
- return (
- <TouchableOpacity activeOpacity={0.8} key={data.notificationId}>
- <Notification data={data} />
- </TouchableOpacity>
- );
- }
- }
- const Notifications = connect(
- state => ({
- NotificationCount: state.NotificationCount
- }),
- dispatch => bindActionCreators(Actions, dispatch)
- )(NotificationsClass);
- export { Notifications };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement