From 5bc2977d1e274b4db676216b0352d73f06c85427 Mon Sep 17 00:00:00 2001
From: shixiang <shixiang0703@163.com>
Date: Mon, 9 Jan 2017 20:39:25 +0800
Subject: [PATCH] 分期开通界面 未添加事件处理 review 于良

---
 js/installment/Installment.js                                  |  1 +
 js/installment/components/installment/Installment.js           | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 js/installment/constants/actionTypes.js                        |  3 +++
 js/installment/containers/InstallmentContainer.js              | 12 +++++++++++-
 js/installment/reducers/installment/installmentActions.js      | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 js/installment/reducers/installment/installmentInitialState.js | 10 +++++++++-
 js/installment/reducers/installment/installmentReducer.js      | 21 +++++++++++++++++++--
 js/installment/services/InstallmentService.js                  | 24 ++++++++++++++++++++++++
 8 files changed, 176 insertions(+), 11 deletions(-)

diff --git a/js/installment/Installment.js b/js/installment/Installment.js
index c0144a1..9f0956c 100644
--- a/js/installment/Installment.js
+++ b/js/installment/Installment.js
@@ -41,6 +41,7 @@ export default function native(platform) {
 			store.dispatch(setPlatform(platform));
 			let channel = this.props.channel;
 			channel && store.dispatch(setChannel(channel));
+			console.log(channel);
 			return (
 				<Provider store={store}>
 					<InstallmentContainer />
diff --git a/js/installment/components/installment/Installment.js b/js/installment/components/installment/Installment.js
index 9c4df5c..b3f65bc 100644
--- a/js/installment/components/installment/Installment.js
+++ b/js/installment/components/installment/Installment.js
@@ -12,37 +12,85 @@ import ReactNative, {
 } from 'react-native';
 
 import Immutable, {Map} from 'immutable';
+import ProductListCell from '../../../common/components/ListCell/ProductListCell';
+import OpenInstallmentTop from './OpenInstallmentTop';
 
 export default class Installment extends React.Component {
     constructor(props) {
             super(props);
+            this._renderRow = this._renderRow.bind(this);
+            this._renderHeader=this._renderHeader.bind(this);
+            this.dataSource = new ListView.DataSource({
+            rowHasChanged: (r1, r2) => !Immutable.is(r1, r2),
+        });
     }
 
-    renderRow(rowData, sectionID, rowID, highlightRow) {
-
+    _renderRow(rowData, sectionID, rowID, highlightRow) {
+        let paddingLeft = rowID % 2 == 1 ? rowMarginHorizontal / 2 : rowMarginHorizontal;
+        let customStyle = rowID == 0 || rowID == 1 ? {paddingLeft} : {paddingLeft};
+            return (
+                <ProductListCell
+                    style={[styles.listContainer, customStyle]}
+                    key={'row' + rowID}
+                    rowID={rowID}
+                    data={rowData}
+                    onPressProduct={this.props.onPressProductListProduct}
+                />
+            );
     }
-    renderTop(rowData, sectionID, rowID, highlightRow) {
-        
+
+    _renderHeader() {
+        return (
+            <OpenInstallmentTop
+            />
+        );
     }
+
     render() {
+        let {
+            isFetching,
+            productListForInstallment,
+            installmentInfo,
+        } = this.props;
+        let dataSource=null;
+        dataSource = productListForInstallment.list.toArray();
+        console.log('========');
+        console.log(dataSource);
+        console.log('========');
         return(
         <View style={styles.container}>
            <ListView
                 contentContainerStyle={styles.contentContainer}
                 enableEmptySections={true}
-                dataSource={this.dataSource.cloneWithRowsAndSections(dataSource)}
-                renderRow={this.renderRow}
+                dataSource={this.dataSource.cloneWithRows(dataSource)}
+                renderRow={this._renderRow}
+                renderHeader={this._renderHeader}
                 onEndReached={() => {
-
+                    if (productListForInstallment && productListForInstallment.list && productListForInstallment.list.size > 0) {
+                            this.props.onEndReached && this.props.onEndReached();
+                        }
                     }}
            />
         </View>
         );
     }
 };
+
+let {width, height} = Dimensions.get('window');
+let rowWidth = Math.ceil(137.5 * width / 320);
+let rowHeight = Math.ceil(254 * width / 320);
+let rowMarginTop = Math.ceil(10 * width / 320);
+let rowMarginHorizontal = (width - rowWidth * 2) / 3;
 let styles = StyleSheet.create({
     container: {
     flex: 1,
     backgroundColor: 'white',
     },
+    contentContainer:{
+		flexDirection: 'row',
+		flexWrap: 'wrap',
+    },  
+    listContainer: {
+        width: width / 2,
+    },  
 });
\ No newline at end of file
diff --git a/js/installment/constants/actionTypes.js b/js/installment/constants/actionTypes.js
index 0126a4e..6b5b361 100644
--- a/js/installment/constants/actionTypes.js
+++ b/js/installment/constants/actionTypes.js
@@ -4,4 +4,7 @@ export default keyMirror({
 	SET_PLATFORM:null,
 	SET_CHANNEL:null,
 
+	INSTALLMENT_PRODUCT_REQUEST:null,
+	INSTALLMENT_PRODUCT_SUCCESS:null,
+	INSTALLMENT_PRODUCT_FAILURE:null,
 });
diff --git a/js/installment/containers/InstallmentContainer.js b/js/installment/containers/InstallmentContainer.js
index 3a0d6bd..e3026d4 100644
--- a/js/installment/containers/InstallmentContainer.js
+++ b/js/installment/containers/InstallmentContainer.js
@@ -40,9 +40,19 @@ class InstallmentContainer extends Component {
     constructor(props) {
         super(props);
     }
-     render() {
+    componentDidMount() {
+        this.props.actions.productListForInstallment();
+    }
+
+    render() {
+        let {isFetching,open} = this.props.installment;
+         console.log(open);
+        console.log(open.productListForInstallment);
         return (
             <Installment
+                isFetching={open.isFetching}
+                productListForInstallment={open.productListForInstallment}
+                installmentInfo={open.installmentInfo}
             />
         );
     }
diff --git a/js/installment/reducers/installment/installmentActions.js b/js/installment/reducers/installment/installmentActions.js
index 7f2207e..f179d49 100644
--- a/js/installment/reducers/installment/installmentActions.js
+++ b/js/installment/reducers/installment/installmentActions.js
@@ -1,9 +1,63 @@
 'use strict';
 
 import ReactNative from 'react-native';
+import InstallmentService from '../../services/InstallmentService';
 
 const {
 
 SET_PLATFORM,
+
+INSTALLMENT_PRODUCT_REQUEST,
+INSTALLMENT_PRODUCT_SUCCESS,
+INSTALLMENT_PRODUCT_FAILURE,
+
+
+
 } = require('../../constants/actionTypes').default;
 
+export function productListForInstallmentRequest(){
+     return {
+        type: INSTALLMENT_PRODUCT_REQUEST,
+    }
+}
+
+export function productListForInstallmentSuccess(json){
+     return {
+        type: INSTALLMENT_PRODUCT_SUCCESS,
+        payload:json,
+    }
+}
+
+export function productListForInstallmentFailure(){
+     return {
+        type: INSTALLMENT_PRODUCT_FAILURE,
+    }
+}
+
+export function productListForInstallment() {
+    return (dispatch, getState) => {
+        let {app, installment} = getState();
+        let {isFetching,open} = installment;
+        if(isFetching){
+            return
+        }
+        console.log('----productListForInstallment-----');
+        dispatch(productListForInstallmentRequest());
+        return new InstallmentService(app.host).fetchInstallmentProductList()
+            .then(json => {
+                console.log(json);
+                let payload = json;//product_list
+                if (payload.page > 1) {
+                    let oldList = open.fetchInstallmentProductList.list.toJS();
+                    let list = [...oldList, ...payload.list];
+                    payload.product_list = list;
+                }
+                dispatch(productListForInstallmentSuccess(payload));
+            })
+            .catch(error => {
+                console.log(error);
+                dispatch(productListForInstallmentFailure());
+            });
+    }
+}
+
diff --git a/js/installment/reducers/installment/installmentInitialState.js b/js/installment/reducers/installment/installmentInitialState.js
index 672798b..2c2ab91 100644
--- a/js/installment/reducers/installment/installmentInitialState.js
+++ b/js/installment/reducers/installment/installmentInitialState.js
@@ -3,7 +3,15 @@
 import {Record, List, Map} from 'immutable';
 
 let InitialState = Record({
-    
+    isFetching:false,
+    open:new (Record({
+        productListForInstallment:new (Record({
+            list:List(),
+            page:1,
+            totalPage:1,
+        })),
+        installmentInfo:'',
+    })),
 });
 
 export default InitialState;
\ No newline at end of file
diff --git a/js/installment/reducers/installment/installmentReducer.js b/js/installment/reducers/installment/installmentReducer.js
index fd7651f..b6be56c 100644
--- a/js/installment/reducers/installment/installmentReducer.js
+++ b/js/installment/reducers/installment/installmentReducer.js
@@ -1,9 +1,14 @@
 'use strict';
 
 import InitialState from './installmentInitialState';
+import Immutable, {Map} from 'immutable';
 
 const {
-    SET_PLATFORM,    
+    SET_PLATFORM,
+
+    INSTALLMENT_PRODUCT_REQUEST,
+    INSTALLMENT_PRODUCT_SUCCESS,
+    INSTALLMENT_PRODUCT_FAILURE,
 } = require('../../constants/actionTypes').default;
 
 const initialState = new InitialState;
@@ -12,7 +17,19 @@ export default function appReducer(state = initialState, action) {
     if (!(state instanceof InitialState)) return initialState.merge(state);
 
     switch (action.type) {
-        
+        case INSTALLMENT_PRODUCT_SUCCESS:{
+            let {
+				page,
+				page_total,
+				product_list,
+			} = action.payload;
+            
+            let productListForInstallment = state.open.productListForInstallment.set('list', Immutable.fromJS(product_list))
+				.set('page', page)
+				.set('totalPage', page_total);
+            return state.setIn(['open', 'productListForInstallment'], productListForInstallment);
+        }
+
     }
 
     return state;
diff --git a/js/installment/services/InstallmentService.js b/js/installment/services/InstallmentService.js
index 90b9b1e..24d2ba8 100644
--- a/js/installment/services/InstallmentService.js
+++ b/js/installment/services/InstallmentService.js
@@ -10,4 +10,28 @@ export default class InstallmentService {
 		}
 		this.api = new Request(baseURL);
 	}
+	async fetchInstallmentProductList(yh_channel=1, order='s_n_asc', page=1, limit=60, brand='',v=7) {
+		 return await this.api.get({
+			 url: '',
+			 body: {
+				 method: 'app.search.li',
+				 yh_channel,
+				 order,
+				 page,
+				 limit,
+				 status: 1,
+				 sales: 'Y',
+				 stocknumber: 1,
+				 attribute_not: 2,
+				 v,
+				 brand,
+			 }
+		 })
+		 .then((json) => {
+			 return json;
+		 })
+		 .catch((error) => {
+			 throw(error);
+		 });
+	}
 }
\ No newline at end of file
--
libgit2 0.24.0