-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cart.js
executable file
·160 lines (145 loc) · 5.8 KB
/
Cart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/**
* This is the Main file
**/
// React native and others libraries imports
import React, { Component } from 'react';
import { Alert, AsyncStorage } from 'react-native';
import { Container, Content, View, Header, Icon, Button, Left, Right, Body, Title, List, ListItem, Thumbnail, Grid, Col } from 'native-base';
import { Actions } from 'react-native-router-flux';
// Our custom files and classes import
import Colors from '../Colors';
import Text from '../component/Text';
import Navbar from '../component/Navbar';
export default class Cart extends Component {
constructor(props) {
super(props);
this.state = {
cartItems: []
};
}
componentWillMount() {
AsyncStorage.getItem("CART", (err, res) => {
if (!res) this.setState({cartItems: []});
else this.setState({cartItems: JSON.parse(res)});
});
}
render() {
var left = (
<Left style={{flex:1}}>
<Button transparent onPress={() => Actions.pop()}>
<Icon name="ios-close" size={38} style={{fontSize: 38}} />
</Button>
</Left>
);
return(
<Container style={{backgroundColor: '#fdfdfd'}}>
<Navbar left={left} title="MY CART" />
{this.state.cartItems.length <=0 ?
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Icon name="ios-cart" size={38} style={{fontSize: 38, color: '#95a5a6', marginBottom: 7}} />
<Text style={{color: '#95a5a6'}}>Your cart is empty</Text>
</View>
:
<Content style={{paddingRight: 10}}>
<List>
{this.renderItems()}
</List>
<Grid style={{marginTop: 20, marginBottom: 10}}>
<Col style={{paddingLeft: 10,paddingRight: 5}}>
<Button onPress={() => this.checkout()} style={{backgroundColor: Colors.navbarBackgroundColor}} block iconLeft>
<Icon name='ios-card' />
<Text style={{color: '#fdfdfd'}}>Checkout</Text>
</Button>
</Col>
<Col style={{paddingLeft: 5, paddingRight: 10}}>
<Button onPress={() => this.removeAllPressed()} style={{borderWidth: 1, borderColor: Colors.navbarBackgroundColor}} block iconRight transparent>
<Text style={{color: Colors.navbarBackgroundColor}}>Emtpy Cart</Text>
<Icon style={{color: Colors.navbarBackgroundColor}} name='ios-trash-outline' />
</Button>
</Col>
</Grid>
</Content>
}
</Container>
);
}
renderItems() {
let items = [];
this.state.cartItems.map((item, i) => {
items.push(
<ListItem
key={i}
last={this.state.cartItems.length === i+1}
onPress={() => this.itemClicked(item)}
>
<Thumbnail square style={{width: 110, height: 90}} source={{ uri: item.image }} />
<Body style={{paddingLeft: 10}}>
<Text style={{fontSize: 18}}>
{item.quantity > 1 ? item.quantity+"x " : null}
{item.title}
</Text>
<Text style={{fontSize: 16, fontWeight: 'bold', marginBottom: 10}}>{item.price}</Text>
<Text style={{fontSize: 14 ,fontStyle: 'italic'}}>Color: {item.color}</Text>
<Text style={{fontSize: 14 ,fontStyle: 'italic'}}>Size: {item.size}</Text>
</Body>
<Right>
<Button style={{marginLeft: -25}} transparent onPress={() => this.removeItemPressed(item)}>
<Icon size={30} style={{fontSize: 30, color: '#95a5a6'}} name='ios-remove-circle-outline' />
</Button>
</Right>
</ListItem>
);
});
return items;
}
removeItemPressed(item) {
Alert.alert(
'Remove '+item.title,
'Are you sure you want this item from your cart ?',
[
{text: 'No', onPress: () => console.log('No Pressed'), style: 'cancel'},
{text: 'Yes', onPress: () => this.removeItem(item)},
]
)
}
removeItem(itemToRemove) {
let items = [];
this.state.cartItems.map((item) => {
if(JSON.stringify(item) !== JSON.stringify(itemToRemove) )
items.push(item);
});
this.setState({cartItems: items});
AsyncStorage.setItem("CART",JSON.stringify(items));
}
removeAllPressed() {
Alert.alert(
'Empty cart',
'Are you sure you want to empty your cart ?',
[
{text: 'No', onPress: () => console.log('No Pressed'), style: 'cancel'},
{text: 'Yes', onPress: () => this.removeAll()}
]
)
}
removeAll() {
this.setState({cartItems: []})
AsyncStorage.setItem("CART",JSON.stringify([]));
}
checkout() {
Actions.checkout({cartItems: this.state.cartItems});
}
itemClicked(item) {
Actions.product({product: item});
}
}
const styles={
title: {
fontFamily: 'Roboto',
fontWeight: '100'
}
};
const items = [
{id: 1, quantity:1, title: 'Black Hat', categoryId: 5, categoryTitle: 'MEN', price: '22$', image: 'http://res.cloudinary.com/atf19/image/upload/c_crop,h_250,w_358,x_150/v1500465309/pexels-photo-206470_nwtgor.jpg', description: "Hello there, i'm a cool product with a heart of gold."},
{id: 2, quantity:3, title: 'V Neck T-Shirt', categoryId: 2, categoryTitle: 'WOMEN', price: '12$', image: 'http://res.cloudinary.com/atf19/image/upload/c_crop,h_250,x_226,y_54/v1500465309/pexels-photo-521197_hg8kak.jpg', description: "Hello there, i'm a cool product with a heart of gold."},
{id: 10, quantity:1, title: 'Black Leather Hat', categoryId: 1, categoryTitle: 'KIDS', price: '2$', image: 'http://res.cloudinary.com/atf19/image/upload/c_crop,g_face,h_250,x_248/v1500465308/fashion-men-s-individuality-black-and-white-157675_wnctss.jpg', description: "Hello there, i'm a cool product with a heart of gold."},
];