forked from atef-najar/react-native-shop-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WishList.js
112 lines (100 loc) · 3.3 KB
/
WishList.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
/**
* This is the Search 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 Product from '../component/Product';
import Navbar from '../component/Navbar';
export default class WishList extends Component {
constructor(props) {
super(props);
this.state = {
items: []
};
}
componentWillMount() {
AsyncStorage.getItem("WISHLIST", (err, res) => {
if (!res) this.setState({items: []});
else this.setState({items: 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 WISHLIST" />
{this.state.items.length <=0 ?
<View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Icon name="heart" size={38} style={{fontSize: 38, color: '#95a5a6', marginBottom: 7}} />
<Text style={{color: '#95a5a6'}}>Your wishlist is empty...</Text>
</View>
:
<Content padder>
{this.renderItems()}
</Content>
}
</Container>
);
}
renderItems() {
let items = [];
this.state.items.map((item, i) => {
items.push(
<ListItem
key={i}
last={this.state.items.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.title}
</Text>
<Text style={{fontSize: 16, fontWeight: 'bold', marginBottom: 10}}>{item.price}</Text>
<Text style={{fontSize: 14 ,fontStyle: 'italic'}}>{item.category}</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;
}
itemClicked(item) {
Actions.product({product: item});
}
removeItemPressed(item) {
Alert.alert(
'Remove '+item.title,
'Are you sure you want this item from your wishlist ?',
[
{text: 'No', onPress: () => console.log('No Pressed'), style: 'cancel'},
{text: 'Yes', onPress: () => this.removeItem(item)},
]
)
}
removeItem(itemToRemove) {
let items = [];
this.state.items.map((item) => {
if(JSON.stringify(item) !== JSON.stringify(itemToRemove) )
items.push(item);
});
this.setState({items: items});
AsyncStorage.setItem("WISHLIST",JSON.stringify(items));
}
}