React Native,如果从项目列表中选择了一个项目详细信息,该如何显示一个项目详细信息

时间:2020-04-22 20:00:28

标签: react-redux axios redux-thunk

我是新来的人,对当地人有反应。我有一个组件,使用带有宁静API的axios显示来自sqlite数据库的进度,我也使用redux。

我想从预付款列表中选择一个预付款记录,以便转到该预付款详细信息页面。但是,目前我不知道如何实现它。

**advanceActions.js**

import axios from "axios";
import {
  GET_ADVANCES,
  DETAIL_ADVANCE,

} from "./types";

//FETCH ADVANCES

export const getAdvances = () => dispatch => {
  axios
    .get("http://localhost:8000/api/advance/")
    .then(res => {
      dispatch({
        type: GET_ADVANCES,
        payload: res.data
      });
    })
    .catch(err => console.log(err));
};

    // GET ONE ADVANCE
export const detailAdvance = id => dispatch => {
  axios
    .get(`http://localhost:8000/api/advance/${id}/`)
    .then(res => {
      dispatch({
        type: DETAIL_ADVANCE,
        payload: res.data
      });
    })
    .catch(err => console.log("Axios not able to fetch single advance", err));
};

advanceReducers.js

import {
  GET_ADVANCES,
  DETAIL_ADVANCE,
} from "../actions/types";

const initialState = {
  items: [],
  item: {}
};


export default function(state = initialState, action) {
  switch (action.type) {
    case GET_ADVANCES:
      return {
        ...state,
        items: action.payload

      };
    case DETAIL_ADVANCE:
      return {
        ...state,
        item: action.payload
      };
    default:
      return state;
  }
}



**//Advance list page(AdvanceLand.js)**

import React, { Component } from "react";
import { connect } from "react-redux";
import PropTypes from "prop-types";
import {
  getAdvances,
  addAdvance
} from "../actions/advanceActions";

import {
  Text,
  View,
  Button,
  FlatList,
  StyleSheet,
  ScrollView,
  TouchableOpacity
} from "react-native";

class AdvanceLand extends Component {
  componentDidMount() {
    this.props.getAdvances();
  }

  renderItem = ({ item }) => {
    let { navigation } = this.props;
    return (
      <TouchableOpacity
        style={{ flex: 1, flexDirection: "row", marginBottom: 3 }}
        onPress={() => navigation.navigate("AdvanceDetails", { id: item.id })}
      >
        <View
          style={{
            flex: 1,
            alignSelf: "stretch",
            flexDirection: "row",
            height: 35,
            margin: 10
          }}
        >
          <View style={styles.containerOne}>
            <Text style={styles.textOne}>{`${item.name}`}</Text>
            <Text style={styles.textTwo}>{`${item.position}`}</Text>
          </View>
          <View style={styles.containerTwo}>
            <Text style={styles.textOne}>{`${item.advancedamnt}`}</Text>
            <Text style={styles.textTwo}>{`${item.date}`}</Text>
          </View>
        </View>
      </TouchableOpacity>
    );
  };
  render() {
    const { navigation } = this.props;
      return (
      <ScrollView>
        <View style={styles.container}>
          <Text style={styles.heading}> Advance Summaries</Text>
          <View
            style={{
              flex: 1,
              alignSelf: "stretch",
              flexDirection: "row",
              height: 35,
              fontSize: 30,
              margin: 10
            }}
          >
            <View style={styles.containerOne}>
              <Text style={styles.textOne}>Staff Name</Text>
              <Text style={styles.textTwo}>Position</Text>
            </View>
            <View style={styles.containerTwo}>
              <Text style={styles.textOne}>Shs: Advance</Text>
              <Text style={styles.textTwo}>Date of Payment</Text>
            </View>
          </View>
          <FlatList
            data={this.props.advances}
            renderItem={this.renderItem}
            keyExtractor={item => item.id.toString()}

          />
          <TouchableOpacity
            style={styles.TouchableOpacityStyle}
            onPress={() => navigation.navigate("advance")}
          >
            <Text style={styles.TouchableOpacityAdd}>+</Text>
          </TouchableOpacity>
        </View>
      </ScrollView>
    );
  }
}

AdvanceLand.propTypes = {
  getAdvances: PropTypes.func.isRequired,
  advances: PropTypes.array.isRequired
};

const mapStateToProps = state => ({
  advances: state.advances.items
});

export default connect(mapStateToProps, { getAdvances })(AdvanceLand);

advanceDetails.js 这是高级详细信息页面,我想显示一种产品的详细信息,但显示所有产品的

import React, { Component } from "react";
import {
  Text,
  View,
  Button,
  FlatList,
  StyleSheet,
  ScrollView,
  Image,
  TouchableOpacity
} from "react-native";
import { connect } from "react-redux";
import PropTypes from "prop-types";
import { getAdvances, deleteAdvance } from "../actions/advanceActions";

class AdvanceDetails extends Component {
  componentDidMount() {
    this.props.getAdvances();
  }

  renderItem = ({ item }) => {
    return (
      <View>
        <View
          style={{
            flex: 1,
            alignSelf: "stretch",
            flexDirection: "row",
            height: 35,
            margin: 10
          }}
        >
          <Text style={{ flex: 1, alignSelf: "stretch" }}> Date</Text>
          <Text
            style={{ flex: 1, alignSelf: "stretch" }}
          >{`${item.date}`}</Text>
        </View>

        <View
          style={{
            flex: 1,
            alignSelf: "stretch",
            flexDirection: "row",
            height: 35,
            margin: 10
          }}
        >
          <Image
            style={{
              alignSelf: "center",
              width: 50,
              height: 50,
              marginRight: 5
            }}
            source={require("../images/worker.jpg")}
          />
          <Text style={{ flex: 1, alignSelf: "stretch" }}> Name </Text>
          <Text
            style={{ flex: 1, alignSelf: "stretch" }}
          >{`${item.name}`}</Text>
        </View>

        <View
          style={{
            flex: 1,
            alignSelf: "stretch",
            flexDirection: "row",
            height: 35,
            margin: 10
          }}
        >
          <Text style={{ flex: 1, alignSelf: "stretch" }}> Gender </Text>
          <Text
            style={{ flex: 1, alignSelf: "stretch" }}
          >{`${item.gender}`}</Text>
        </View>

        <View
          style={{
            flex: 1,
            alignSelf: "stretch",
            flexDirection: "row",
            height: 35,
            margin: 10
          }}
        >

          <Text style={{ flex: 1, alignSelf: "stretch" }}> Position </Text>
          <Text
            style={{ flex: 1, alignSelf: "stretch" }}
          >{`${item.position}`}</Text>
        </View>

        <View
          style={{
            flex: 1,
            alignSelf: "stretch",
            flexDirection: "row",
            height: 35,
            margin: 10
          }}
        >

          <Text style={{ flex: 1, alignSelf: "stretch" }}> Status </Text>
          <Text
            style={{ flex: 1, alignSelf: "stretch" }}
          >{`${item.status}`}</Text>
        </View>

        <View
          style={{
            flex: 1,
            alignSelf: "stretch",
            flexDirection: "row",
            height: 35,
            margin: 10
          }}
        >

          <Text style={{ flex: 1, alignSelf: "stretch" }}>Advanced Amount</Text>
          <Text style={{ flex: 1, alignSelf: "stretch" }}>
            Shs: {`${item.advancedamnt}`}
          </Text>
        </View>

        <View
          style={{
            flex: 1,
            alignSelf: "stretch",
            flexDirection: "row",
            height: 35,
            margin: 10
          }}
        >

          <Text style={{ flex: 1, alignSelf: "stretch" }}> Description </Text>
          <Text
            style={{ flex: 1, alignSelf: "stretch" }}
          >{`${item.description}`}</Text>
        </View>
        <TouchableOpacity
          onPress={this.props.deleteAdvance.bind(this, item.id)}
          style={{
            flex: 1,
            alignSelf: "stretch",
            flexDirection: "row",
            backgroundColor: "#CEDCF2",
            height: 35,
            borderBottomColor: "#CEDCF2",
            fontSize: 30,
            borderBottomWidth: 2,
            margin: 10
          }}
        >
          <Text>Delete</Text>
        </TouchableOpacity>
      </View>
    );
  };
  render() {
    const { navigation } = this.props;
    return (
      <View style={styles.container}>
        <Text style={styles.heading}>Advance details</Text>
        <FlatList
          data={this.props.advance}
          renderItem={this.renderItem}
          keyExtractor={(item, index) => index.toString()}

        />
      </View>
    );
  }
}
AdvanceDetails.propTypes = {
  getAdvances: PropTypes.func.isRequired,
  deleteAdvance: PropTypes.func.isRequired,
  advance: PropTypes.object

};
const mapStateToProps = state => ({
  advance: state.advances.items


});

export default connect(mapStateToProps, { getAdvances, deleteAdvance })(
  AdvanceDetails
);

0 个答案:

没有答案