Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. import React from 'react';
  2. import { StyleSheet, View } from 'react-native';
  3. import { Container, Header, Content, Card, CardItem, Thumbnail, List, ListItem, Text, Button, Icon, Left, Body, Right, Title } from 'native-base';
  4.  
  5. import firebase from './Config';
  6. import { NavigationActions } from "react-navigation";
  7. import SideMenu from "./SideMenu";
  8.  
  9. export default class YourProducts extends React.Component {
  10.  
  11. constructor() {
  12. super();
  13.  
  14. this.state = {
  15. items: [],
  16. itemKeys: [],
  17. user: null
  18. };
  19. }
  20.  
  21. deleteProduct = (key) => {
  22. console.log(key);
  23.  
  24. var itemsRef = firebase.database().ref('products/' + key);
  25. itemsRef.remove().then(function () {
  26. console.log('removed');
  27. })
  28. }
  29.  
  30. componentDidMount() {
  31. // Alert.alert("A " + this.props.navigation.state[0]);
  32. var user = firebase.auth().currentUser;
  33. // console.log(user);
  34. if (user) {
  35. this.setState({ user: user.email })
  36. }
  37.  
  38. var itemsRef = firebase.database().ref('products');
  39. itemsRef.on('value', (snapshot) => {
  40. var products = snapshot.val();
  41. var newList = Object.values(products);
  42. var newListKey = Object.keys(products)
  43.  
  44. this.setState({
  45. items: newList,
  46. itemKeys: newListKey
  47.  
  48. });
  49.  
  50. });
  51.  
  52. this.state.items.map((item, index) => { console.log(item) })
  53. }
  54.  
  55. goToDetail() {
  56. this.props.navigation.navigate("Detail");
  57. }
  58.  
  59. productList() {
  60. return this.state.items.map((product, index) => {
  61. return (
  62. this.state.user == product['seller_contact'] ?
  63. <Card key={index}>
  64. <CardItem >
  65. <Left>
  66. <Button transparent onPress={() => this.props.navigation.navigate("Detail", { product: product })}>
  67. <Thumbnail
  68.  
  69. source={{ uri: product["image"] }} />
  70. </Button>
  71. <Body>
  72. <Text>{product["name"]}</Text>
  73. <Text note>{product["seller"] + " - " + product["seller_contact"]}</Text>
  74. </Body>
  75. </Left>
  76. </CardItem>
  77. {/* <CardItem cardBody>
  78. <Image source={{ uri: product["image"] }} style={{ height: 200, width: null, flex: 1 }} />
  79. </CardItem> */}
  80. <CardItem>
  81. <Body>
  82.  
  83. <Button light bordered danger onPress={() => { this.deleteProduct(this.state.itemKeys[index]) }}><Text>Delete</Text></Button>
  84. </Body>
  85. <Right>
  86. <Text style={style.price}>{product["price"]} EUR</Text>
  87. </Right>
  88. </CardItem>
  89. </Card> : <Text></Text>
  90. )
  91. })
  92. }
  93.  
  94. render() {
  95.  
  96. return (
  97. <Container>
  98. <Header>
  99. <Left>
  100. <Button transparent onPress={() => this.props.navigation.navigate("DrawerOpen")}>
  101. <Icon name='menu' />
  102. </Button>
  103. </Left>
  104. <Body>
  105. <Title>Products</Title>
  106. </Body>
  107. <Right />
  108. </Header>
  109. <Content contentContainerStyle={style.content}>
  110. {this.productList()}
  111. {/* <Button onPress={this.deleteProduct}><Text>Click</Text></Button> */}
  112. </Content>
  113. </Container>
  114. );
  115. }
  116. }
  117.  
  118. const style = StyleSheet.create({
  119. content: {
  120. // flex: 1,
  121. // justifyContent: 'center',
  122. // alignItems: 'center'
  123. },
  124. price: {
  125. fontSize: 25
  126. }
  127. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement