1{
2 type: <name of action defined in constants.js>,
3 payload: <the actual payload -- it's optional>
4}
5
1import { FETCH_ACCOUNT_ERROR, FETCH_ACCOUNT_LOADING, FETCH_ACCOUNT_SUCCESS } from './actions';
2
3export const initialState = {
4 loading: false,
5 account: [],
6 error: null
7}
8
9function selectInterestedAccountInfo(account) {
10 return Object.keys(account).reduce(function(obj, k) {
11 if (["login", "url", "name", "created_at", "bio", "email", "public_repos"].includes(k)) {
12 obj[k] = account[k];
13 }
14 return obj;
15 }, {});
16}
17
18export function accountReducer(state = initialState, action) {
19 switch(action.type) {
20 case FETCH_ACCOUNT_LOADING:
21 return {
22 ...state,
23 loading: true
24 }
25 case FETCH_ACCOUNT_SUCCESS:
26 return {
27 ...state,
28 loading: false,
29 account: selectInterestedAccountInfo(action.payload)
30 }
31 case FETCH_ACCOUNT_ERROR:
32 return {
33 ...state,
34 loading: false,
35 error: action.error
36 }
37 default:
38 return state;
39 }
40}
41
42export const getAccountSuccess = state => state.account;
43export const getAccountLoading = state => state.loading;
44export const getAccountError = state => state.error;
1import React, { Component } from 'react';
2import { StyleSheet, View } from 'react-native';
3import { Provider } from 'react-redux';
4import { applyMiddleware, createStore } from 'redux';
5import thunk from 'redux-thunk';
6import GitAccount from './GitAccount';
7import { accountReducer, initialState } from './reducer';
8
9console.log(`${Date()} 1111111111>>> initialization of Redux for the application.`);
10const rootReducer = accountReducer;
11const middlewares = [thunk];
12const store = createStore(rootReducer, initialState, applyMiddleware(...middlewares));
13
14export default class App extends Component {
15 render() {
16 console.log(`${Date()} 2222222222>>> Provider is a wrapper to the application and responsible for providing access to the created store`);
17 return (
18 <Provider store={store}>
19 <View style={styles.container}>
20 <GitAccount />
21 </View>
22 </Provider>
23 );
24 }
25}
26
27const styles = StyleSheet.create({
28 container: {
29 flex: 1,
30 backgroundColor: '#fff',
31 marginTop: 50
32 }
33});