Review.js 8.73 KB
import React, { Component } from 'react';
import {Image, View, Text, StyleSheet,ScrollView,TouchableOpacity,Dimensions } from 'react-native';
import CouponeItem from '../CouponItem'
import StampItem from '../StampItem'
import Star from '../Star'
import ReviewItem from '../ReviewItem'
import {get} from 'axios'
import { SERVER } from '../../common/servername';
import AsyncStorage from '@react-native-community/async-storage';
import Loading from './Loading'
export default class AddTab extends Component {
    static navigationOptions = {
        
        headerShown: false
        
    }
    state = {
        current:1,
        id:0,
        review:[],
        user:{},
        rating_1:0,
        rating_2:0,
        rating_3:0,
        rating_4:0,
        rating_5:0,
        store:{},
        loading:true
    }
    constructor(props) {
        super(props)
    }
    componentDidMount() {
        AsyncStorage.getItem('user',(err,res) => {
            this.setState({
                user:JSON.parse(res)
            })
        })
        if(this.props.navigation.state.params) {
            this.setState({
                id:this.props.navigation.state.params.id
            },() => {
                get(`http://${SERVER}/reviews/${this.state.id}`).then(res=> {
                    this.setState({
                        review:res.data.data,
                        rating_1:res.data.data.filter((e,i) => e.reviewGrade==1).length,
                        rating_2:res.data.data.filter((e,i) => e.reviewGrade==2).length,
                        rating_3:res.data.data.filter((e,i) => e.reviewGrade==3).length,
                        rating_4:res.data.data.filter((e,i) => e.reviewGrade==4).length,
                        rating_5:res.data.data.filter((e,i) => e.reviewGrade==5).length,
                    })
                })
                get(`http://${SERVER}/stores/${this.state.id}`).then(res => {
                    this.setState({
                        store:res.data.data[0],
                    })
                    setTimeout(() => {
                        this.setState({
                            
                        loading:false
                        })
                    }, 500);
                })
            })
        }
    }
    render() {
        const rating = this.state.store.rating
        const reviewCount = this.state.store.reviewCount
        return (
            <View style={{backgroundColor:"white",height:"100%"}}>
                <View style={{width:"100%",flexDirection:"row",paddingTop:16,paddingBottom:16,paddingLeft:16,backgroundColor:"#615F5F"}}>
                    <TouchableOpacity onPress={() => this.props.navigation.goBack(null)}>
                        <Image source={require('../../assets/back.png')}/>
                    </TouchableOpacity>
                    <Text style={{color:"white",fontSize:16,marginLeft:16}}>
                        평점  리뷰
                    </Text>
                </View>
                {this.state.loading?<Loading/>:
                <ScrollView style={{paddingLeft:16,paddingRight:16,paddingTop:16,backgroundColor:"white"}}>
                    <View style={{flexDirection:"row",marginBottom:16}}>
                        <Image style={{width:"30%",height:Dimensions.get("screen").width*0.15}} resizeMode="contain" source={require('../../assets/img.png')}/>
                        <Text style={{fontSize:15,color:"black",alignSelf:"center"}}>
                            {this.state.store.name}
                        </Text>
                    </View>
                    <View style={{flexDirection:"row"}}>
                        <View style={{width:"30%"}}> 
                            <Text style={{textAlign:"center",fontSize:40,fontWeight:"bold",color:"black"}}>
                                {rating}{rating==1|| rating==2 || rating==3 || rating==4 || rating==5?".0":""}
                            </Text>
                            <Star style={{flexDirection:"row",justifyContent:"center",marginTop:5,marginBottom:4.3}} navigation={this.props.navigation} item={this.state.store.rating} />
                            <View>
                                <Text style={{textAlign:"center",fontSize:11,color:"#615F5F"}}>
                                    {this.state.review.length}
                                </Text>
                            </View>
                        </View>
                        <View style={{width:"50%",marginLeft:70}}>
                            <View style={{flexDirection:"row",marginBottom:8}}>
                            <Text style={{marginRight:8, fontSize:9,color:"black"}}>
                                5
                            </Text>
                            <View style={{backgroundColor:"#F1F1F1",borderRadius:15,width:"80%",height:15 }}>
                                <View style={[{backgroundColor:"#FFD63A",width:this.state.rating_5/this.state.review.length*100+"%",height:"100%",borderRadius:15},this.state.review.length==0?{width:0}:undefined]}>

                                </View>
                            </View>
                            </View>
                            
                            <View style={{flexDirection:"row",marginBottom:8}}>
                            <Text style={{marginRight:8, fontSize:9,color:"black"}}>
                                4
                            </Text>
                            <View style={{backgroundColor:"#F1F1F1",borderRadius:15,width:"80%",height:15 }}>
                                <View style={[{backgroundColor:"#FFD63A",width:this.state.rating_4/this.state.review.length*100+"%",height:"100%",borderRadius:15},this.state.review.length==0?{width:0}:undefined]}>

                                </View>
                            </View>
                            </View>
                            
                            <View style={{flexDirection:"row",marginBottom:8}}>
                            <Text style={{marginRight:8, fontSize:9,color:"black"}}>
                                3
                            </Text>
                            <View style={{backgroundColor:"#F1F1F1",borderRadius:15,width:"80%",height:15 }}>
                                <View style={[{backgroundColor:"#FFD63A",width:this.state.rating_3/this.state.review.length*100+"%",height:"100%",borderRadius:15},this.state.review.length==0?{width:0}:undefined]}>

                                </View>
                            </View>
                            </View>
                            
                            <View style={{flexDirection:"row",marginBottom:8}}>
                            <Text style={{marginRight:8, fontSize:9,color:"black"}}>
                                2
                            </Text>
                            <View style={{backgroundColor:"#F1F1F1",borderRadius:15,width:"80%",height:15 }}>
                                <View style={[{backgroundColor:"#FFD63A",width:this.state.rating_2/this.state.review.length*100+"%",height:"100%",borderRadius:15},this.state.review.length==0?{width:0}:undefined]}>

                                </View>
                            </View>
                            </View>
                            
                            <View style={{flexDirection:"row",marginBottom:8}}>
                            <Text style={{marginRight:8, fontSize:9,color:"black"}}>
                                1
                            </Text>
                            <View style={{backgroundColor:"#F1F1F1",borderRadius:15,width:"80%",height:15 }}>
                                <View style={[{backgroundColor:"#FFD63A",width:this.state.rating_1/this.state.review.length*100+"%",height:"100%",borderRadius:15},this.state.review.length==0?{width:0}:undefined]}>

                                </View>
                            </View>
                            </View>
                        </View>
                    </View>
                    <View style={{marginTop:24}}>
                        {this.state.review.map((e,i) => <ReviewItem item={e} user={this.state.user} store={this.state.store}/>)}
                    </View>


                </ScrollView>}
                
            <View style={{zIndex:10,marginTop:20,bottom:0,width:"100%"}}>
                <TouchableOpacity onPress={() => this.props.navigation.navigate("WriteReview",{id:this.state.id})} style={{backgroundColor:"#f4cd37",padding:20}}>
                    <Text style={{textAlign:"center",fontSize:15,fontWeight:"bold",color:"black"}}>
                        리뷰쓰기
                    </Text>
                </TouchableOpacity>
            </View>
            </View>
        );
    }
}
 
const style = StyleSheet.create({
    container: {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center',
    }
});