|
| 1 | +import React, {Component} from 'react'; |
| 2 | +import StorageUtil from '../utils/StorageUtil'; |
| 3 | +import Utils from '../utils/Utils'; |
| 4 | +import Toast from '@remobile/react-native-toast'; |
| 5 | +import Api from "../api/Api"; |
| 6 | + |
| 7 | +import { |
| 8 | + Dimensions, |
| 9 | + Image, |
| 10 | + Modal, |
| 11 | + PixelRatio, |
| 12 | + StyleSheet, |
| 13 | + Text, |
| 14 | + TouchableOpacity, |
| 15 | + View |
| 16 | +} from 'react-native'; |
| 17 | + |
| 18 | +const {width, height} = Dimensions.get('window'); |
| 19 | + |
| 20 | +export default class MomentMenuView extends Component { |
| 21 | + constructor(props) { |
| 22 | + super(props); |
| 23 | + this.state = { |
| 24 | + show: false, |
| 25 | + pageX: 0, |
| 26 | + pageY: 0, |
| 27 | + height: 0 |
| 28 | + }; |
| 29 | + StorageUtil.get('username', (error, object) => { |
| 30 | + if (!error && object != null) { |
| 31 | + this.setState({username: object.username}); |
| 32 | + } |
| 33 | + }); |
| 34 | + } |
| 35 | + |
| 36 | + render() { |
| 37 | + return ( |
| 38 | + <View style={[styles.modalContainer, {height: this.state.height}]}> |
| 39 | + <Modal transparent={true} |
| 40 | + visible={this.state.show} |
| 41 | + onRequestClose={() => this.closeModal()}> |
| 42 | + <TouchableOpacity style={[styles.modalContainer, {height: this.state.height}]} |
| 43 | + onPress={() => this.closeModal()}> |
| 44 | + <View style={[styles.container, {left: this.state.pageX - 200, top: this.state.pageY - 20}]}> |
| 45 | + <TouchableOpacity onPress={() => this.doFavor()}> |
| 46 | + <View style={styles.menuItemContainer}> |
| 47 | + <Image style={styles.menuItemImg} source={require('../../images/ic_moment_favor.png')}/> |
| 48 | + <Text style={styles.menuItemText}> 赞 </Text> |
| 49 | + </View> |
| 50 | + </TouchableOpacity> |
| 51 | + <View style={styles.divider}/> |
| 52 | + <TouchableOpacity onPress={() => this.doComment()}> |
| 53 | + <View style={styles.menuItemContainer}> |
| 54 | + <Image style={styles.menuItemImg} source={require('../../images/ic_moment_comment.png')}/> |
| 55 | + <Text style={styles.menuItemText}>评论</Text> |
| 56 | + </View> |
| 57 | + </TouchableOpacity> |
| 58 | + </View> |
| 59 | + </TouchableOpacity> |
| 60 | + </Modal> |
| 61 | + </View> |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + doComment() { |
| 66 | + let callback2 = this.state.callback2; |
| 67 | + if (!Utils.isEmpty(callback2)) { |
| 68 | + callback2(this.state.momentId, this.state.momentUsername); |
| 69 | + } |
| 70 | + this.closeModal(); |
| 71 | + } |
| 72 | + |
| 73 | + doFavor() { |
| 74 | + let momentId = this.state.momentId; |
| 75 | + if (!Utils.isEmpty(momentId)) { |
| 76 | + let url = Api.MOMENT_FAVOR_URL; |
| 77 | + let username = this.state.username; |
| 78 | + let formData = new FormData(); |
| 79 | + formData.append('username', username); |
| 80 | + formData.append('momentId', momentId); |
| 81 | + fetch(url, {method: 'POST', body: formData}).then((res) => res.json()) |
| 82 | + |
| 83 | + .then((json) => { |
| 84 | + if (!Utils.isEmpty(json)) { |
| 85 | + if (json.code == 1) { |
| 86 | + // 需要刷新页面 |
| 87 | + let callback1 = this.state.callback1; |
| 88 | + if (!Utils.isEmpty(callback1)) { |
| 89 | + callback1(json.msg); |
| 90 | + } |
| 91 | + } else { |
| 92 | + Toast.showShortCenter(json.msg); |
| 93 | + } |
| 94 | + } |
| 95 | + }).catch((e) => { |
| 96 | + Toast.showShortCenter(e.toString()); |
| 97 | + }) |
| 98 | + } |
| 99 | + this.closeModal(); |
| 100 | + } |
| 101 | + |
| 102 | + closeModal() { |
| 103 | + this.setState({show: false, height: 0}); |
| 104 | + } |
| 105 | + |
| 106 | + showModal(pageX, pageY, momentId, momentUsername, callback1, callback2) { |
| 107 | + this.setState({ |
| 108 | + pageX: pageX, |
| 109 | + pageY: pageY, |
| 110 | + height: height, |
| 111 | + show: true, |
| 112 | + momentId: momentId, |
| 113 | + momentUsername: momentUsername, |
| 114 | + callback1: callback1, |
| 115 | + callback2: callback2 |
| 116 | + }); |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +const styles = StyleSheet.create({ |
| 121 | + modalContainer: { |
| 122 | + width: width, |
| 123 | + }, |
| 124 | + container: { |
| 125 | + backgroundColor: '#393A3E', |
| 126 | + borderRadius: 2, |
| 127 | + flexDirection: 'row', |
| 128 | + justifyContent: 'center', |
| 129 | + alignItems: 'center', |
| 130 | + width: 180, |
| 131 | + padding: 5, |
| 132 | + height: 35, |
| 133 | + position: 'absolute', |
| 134 | + left: 230, |
| 135 | + top: 500 |
| 136 | + }, |
| 137 | + divider: { |
| 138 | + width: 1 / PixelRatio.get(), |
| 139 | + marginLeft: 20, |
| 140 | + marginRight: 20, |
| 141 | + height: 25, |
| 142 | + backgroundColor: '#DDDDDD' |
| 143 | + }, |
| 144 | + menuItemContainer: { |
| 145 | + flex: 1, |
| 146 | + flexDirection: 'row', |
| 147 | + alignItems: 'center', |
| 148 | + justifyContent: 'center', |
| 149 | + }, |
| 150 | + menuItemImg: { |
| 151 | + width: 20, |
| 152 | + height: 20, |
| 153 | + marginTop: 10, |
| 154 | + marginBottom: 10, |
| 155 | + }, |
| 156 | + menuItemText: { |
| 157 | + color: '#FFFFFF', |
| 158 | + fontSize: 15 |
| 159 | + } |
| 160 | +}) |
0 commit comments