-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGiftView.js
More file actions
104 lines (79 loc) · 3.1 KB
/
GiftView.js
File metadata and controls
104 lines (79 loc) · 3.1 KB
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
// GiftView.js
// componente che descrive nel dettaglio il gruppo regalo, con le varie proposte
import React from 'react'
import { StyleSheet, Text, TextInput, View, Button ,Image,Switch,FlatList} from 'react-native'
import GiftSummary from './GiftSummary'
import firebase from 'react-native-firebase'
export default class GiftView extends React.Component {
state= {refId:null, groupId:null, imageUrl : null ,proposedByImgUrl : null , description:null ,canSetPreference:false, toggled:false }
_setPreference= (value)=>{
console.log('value switch: ',value)
console.log('setPreference , item: ',this.state.item)
this.state.item.subscriptions[this.currentUserId] =value;
this.setState({toggled:value})
strPath='items.'+this.state.refId.toString();
firebase.firestore().collection('gifts').doc(this.state.groupId).update(strPath,this.state.item)
.then(()=>{console.log('updated')})
.catch((err)=>{console.log('upload error',err)})
}
constructor(){
super();
this.giftRef = firebase.firestore().collection('gifts');
this.currentUserId = firebase.auth().currentUser.uid.toString();
this.unsubscribe=null;
}
_onDocumentUpdate = (documentSnapshot) => {
const item = documentSnapshot.get('items.'+this.state.refId)
console.log('_onDocumentUpdate refId: ',this.state.refId)
console.log('_onDocumentUpdate this.props.navigation: ',this.props.navigation)
this.props.navigation.setParams({ title: item.title })
console.log('_onDocumentUpdate item: ',item)
// set ImageURL
const imageRef = firebase.storage().ref(item.imageURL);
this.setState({imageUrl : item.imageURL})
// set proposedByImgUrl and description ,canSetPreference
const proposedImgRef = firebase.firestore().collection('users').doc(item.proposedBy).get('photoURL');
var strPath = 'guests.'+this.currentUserId+'.delegateTo';
var strPath2 = 'items.'+this.state.refId+'.subscriptions.'+this.currentUserId;
console.log('_onDocumentUpdate subscriptions user : ', strPath2 );
console.log('_onDocumentUpdate get toggled : ', documentSnapshot.get(strPath2) )
this.setState({
item : item,
proposedByImgUrl : proposedImgRef,
description: item.description,
canSetPreference : documentSnapshot.get(strPath),
toggled : documentSnapshot.get(strPath2),
})
console.log('_onDocumentUpdate toggled: ', this.state.toggled)
}
componentDidMount() {
const { navigation } = this.props;
const groupId = navigation.getParam('groupId', 'NO-ID');
const refId = navigation.getParam('refId', 'NO-ID');
console.log('group to view: ',groupId )
this.setState({groupId:groupId,
refId:refId})
this.unsubscribe = this.giftRef.doc(groupId).onSnapshot(this._onDocumentUpdate);
}
render(){
//nome da inserire nella barra di stato
//
return (
<View>
<View>
<Image
style={{width: 50, height: 50}}
source={{uri: this.state.imageUrl}}
/>
<Text>{this.state.description}</Text>
</View>
<View>
<Switch
onValueChange={ this._setPreference}
value={this.state.toggled}
/>
</View>
</View>
)
}
}