MediaDetail.js 3.64 KB
Newer Older
Giang Tran committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import React, {useState, useEffect} from 'react';
import {
  View,
  Text,
  StyleSheet,
  ImageBackground,
  TouchableOpacity,
  FlatList,
} from 'react-native';
import YouTube from 'react-native-youtube';
import R from '../../../assets/R';
import HeaderBack from '../../../components/Header/HeaderBack';
import {getFontXD} from '../../../Config/Functions';
import Icon from 'react-native-vector-icons/AntDesign';
import {useNavigation} from '@react-navigation/native';
import {MEDIADETAIL} from '../../../routers/ScreenNames';
import {getDetailMedia} from '../../../apis/Functions/NewFeed';
Giang Tran committed
18
import I18n from '../../../helper/i18/i18n';
19
import AppText from '../../../components/AppText';
Giang Tran committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

const Item = (props) => {
  const navigate = useNavigation();

  const {title, poster, published_at, id} = props.item;

  return (
    <TouchableOpacity
      onPress={() => props.onChangeID(id)}
      style={styles.containerItem}>
      <ImageBackground
        resizeMode={'cover'}
        imageStyle={{borderRadius: 5, width: 140, height: 100}}
        style={styles.img}
        source={{uri: poster}}>
        <Icon
          name={'playcircleo'}
          color={R.colors.white}
          size={getFontXD(52)}
        />
      </ImageBackground>
      <View style={styles.wrapRight}>
        <View style={{flex: 1}}>
Giang Tran committed
43
          <Text style={styles.txtTitle} numberOfLines={4}>
Giang Tran committed
44 45 46
            {title}
          </Text>
        </View>
Giang Tran committed
47
        <Text style={{color: '#8E8C8C'}}>{published_at}</Text>
Giang Tran committed
48 49 50 51 52 53 54 55 56 57 58 59
      </View>
    </TouchableOpacity>
  );
};

const MediaDetail = (props) => {
  const [id, setID] = useState(props.route.params.id);
  const [data, setData] = useState();

  useEffect(() => {
    getData();
  }, [id]);
Giang Tran committed
60
  const onChangeID = (item) => setID(item);
Giang Tran committed
61 62 63 64 65 66

  const getData = async () => {
    const res = await getDetailMedia(id);
    if (res.data.code == 200 && res.data.data) {
      setData(res.data.data);
    } else {
Giang Tran committed
67
      Alert.alert(I18n.t('Notification'), I18n.t('Can_not_get_data'));
Giang Tran committed
68 69 70 71
    }
  };
  return (
    <View style={{flex: 1}}>
Giang Tran committed
72
      <HeaderBack title={'DetailVideo'} />
Giang Tran committed
73 74 75 76 77 78 79
      {data ? (
        <View style={{flex: 1}}>
          <YouTube
            apiKey={'AIzaSyAMj3UyhieazgdBFGeZ33V6C3zweAYpvP8'}
            videoId={data.video_id}
            play={false}
            onError={(e) => console.log({error: e.error})}
Giang Tran committed
80
            style={{alignSelf: 'stretch', height: 250}}
Giang Tran committed
81 82 83 84 85
          />
          <View style={styles.wrapTitle}>
            <Text style={styles.txtTitle}>{data.title}</Text>
          </View>
          <View style={styles.body}>
86
            <AppText style={styles.txtBig} i18nKey={('TopVideo')}/>
Giang Tran committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
            <FlatList
              showsVerticalScrollIndicator={false}
              data={data.related_videos}
              renderItem={({item}) => (
                <Item onChangeID={onChangeID} item={item} />
              )}
              keyExtractor={(item) => item.id}
            />
          </View>
        </View>
      ) : null}
    </View>
  );
};

const styles = StyleSheet.create({
  txtTitle: {
    fontSize: getFontXD(42),
  },
  txtBig: {
    fontSize: getFontXD(46),
    fontWeight: 'bold',
  },
  wrapTitle: {
Giang Tran committed
111
    paddingVertical: 10,
Giang Tran committed
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
    paddingHorizontal: 10,
    borderBottomWidth: 0.7,
    borderBottomColor: R.colors.borderGray,
  },
  body: {
    paddingHorizontal: 10,
    paddingTop: 10,
    flex: 1,
  },
  img: {
    width: 140,
    height: 100,
    borderRadius: 5,
    justifyContent: 'center',
    alignItems: 'center',
  },
  containerItem: {
    flexDirection: 'row',
    paddingVertical: 10,
    borderBottomColor: R.colors.borderGray,
    borderBottomWidth: 0.7,
  },
  wrapRight: {
    flex: 1,
    marginLeft: 10,
  },
});

export default MediaDetail;