import React from 'react'
import LinearGradient from "react-native-linear-gradient";
import {BackgroundImage} from "../../components/BackgroundImage";
import {Animated, FlatList, RefreshControl, ScrollView, StyleSheet, Text, TouchableOpacity, View} from "react-native";
import {NavigationEvents} from "react-navigation";
import IconFeather from "react-native-vector-icons/Feather";
import IconMaterial from 'react-native-vector-icons/MaterialCommunityIcons'
import Image from 'react-native-fast-image'
import {getLevelMembership, getListReward, getUserProfile} from "../../api/UserApi";
import {connect} from "react-redux";
import {bindActionCreators} from "redux";
import {appSetUser} from "../../redux/app/action";
import {t} from "../../utils/i18n";
import IndicatorLoading from "../../components/IndicatorLoading";
class RedeemScreen extends React.Component {
constructor(props){
super(props);
this.state = {
isLoading: false,
refreshing: false,
cateGoryRewards: [
{source: require('../../../assets/images/discount_rent.png'), text: t('rental_discount'), id: 1},
{source: require('../../../assets/images/discount_product.png'), text: t('product_discount'), id: 2},
{source: require('../../../assets/images/discount_redeem.png'), text: t('redeem_service'), id: 3},
{source: require('../../../assets/images/discount_coupon.png'), text: t('lucky_draw'), id: 4}
],
allRewards: [],
point: 0,
scrollY: '',
membel_level: ''
}
}
initData = () => {
this.setState({
isLoading: true,
refreshing: true,
}, () => {
Promise.all([
this.getListReward(),
this.getUserProfile(),
this.getLevelMember()
]).then(() => {
this.setState({
isLoading: false,
refreshing: false
})
});
})
}
async getListReward () {
return await getListReward()
.then(res => {
if(res.data){
this.setState({
allRewards: res.data.data,
isLoading: false
})
}
})
}
async getUserProfile () {
return await getUserProfile()
.then(res => {
if(res.data && res.ok){
this.setState({
point: res.data.point_balance,
}, () => {
let user_data = this.props.user;
user_data = {
...user_data,
point: this.state.point
}
this.props.appSetUser(user_data)
})
}
});
}
async getLevelMember () {
return await getLevelMembership()
.then(res => {
if(res.data && res.data.success){
this.setState({
membel_level: res.data.data && res.data.data.user_level ? res.data.data.user_level : ''
})
}
})
}
_onRefresh = () => {
this.initData()
}
renderItemImages = (item) => {
return (
this.props.navigation.navigate('RewardDetail', {id: item.id})}>
{item.name}
{item.points_to_redeem} Point
)
}
_keyExtractorNewHead = (item, index) => 'reward_' + index
render() {
let cateGoryRewards = this.state.cateGoryRewards;
let allRewards = this.state.allRewards;
return (
{
this.initData()
}}
/>
}
showsVerticalScrollIndicator={false}
scrollEventThrottle={16}
// onScroll={Animated.event(
// [{ nativeEvent: { contentOffset: { y: this.state.scrollY } } }]
// )}
>
this.props.navigation.navigate('HistoryRedeem')} style={{flex: 1, marginRight: 8}}>
this.props.navigation.navigate('MembershipLevelDetail')}>
{this.state.membel_level}
{parseFloat(this.state.point).toLocaleString()}
Points
this.props.navigation.navigate('MyRewards')} style={{flex: 1}}>
My Rewards
Category Rewards
{
cateGoryRewards && cateGoryRewards.map((category, index) => (
this.props.navigation.navigate('AllRewards', {id: category.id})}
key={`reward_${category.id}_${index}`}
>
{category.text}
))
}
All Rewards
{
allRewards && allRewards.length > 1 &&
this.props.navigation.navigate('AllRewards')}>
See More >
}
{
allRewards && allRewards.length > 0
?
this.renderItemImages(item)}
horizontal={true}
showsHorizontalScrollIndicator={false}
keyExtractor={this._keyExtractorNewHead}
initialNumToRender={2}
extraData={allRewards}
// onScroll={this._onCarouselScroll}
/>
:
Rewards not found.
}
);
}
}
const styles = StyleSheet.create({
contentContainer: {
paddingVertical: 0,
},
pointContain: {
paddingHorizontal: 7,
flex: 1,
borderRadius: 5,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between'
},
textPoint: {
color: 'white',
fontSize: 24,
fontWeight: 'bold',
fontFamily: 'Prompt-Regular',
},
categoryContain: {
flex: 0.3,
backgroundColor: '#00420A',
padding: 10,
marginTop: 8,
borderRadius: 6
},
subCateContain: {
flex: 1
},
textDiscount: {
color: 'white',
fontWeight: '600',
fontSize: 10.5,
width: '100%',
textAlign: 'center',
fontFamily: 'Prompt-Regular'
},
textDiscountContain: {
backgroundColor: '#318F00',
paddingVertical: 5,
paddingHorizontal: 5,
borderRadius: 50,
marginTop: -5
},
membershipLevelBox: {
backgroundColor: 'white',
paddingHorizontal: 5,
paddingVertical: 5,
borderBottomLeftRadius: 5,
borderBottomRightRadius: 5,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center'
}
});
const mapDisPatchToProps = state => {
return state.app
}
const propSet = dispatch => bindActionCreators({
appSetUser,
}, dispatch)
export default connect(mapDisPatchToProps, propSet)(RedeemScreen)