1
+ import { Ionicons } from "@expo/vector-icons" ;
2
+ import to from "await-to-js" ;
1
3
import * as Clipboard from "expo-clipboard" ;
2
- import React from "react" ;
3
- import { View , Text , StyleSheet , TouchableOpacity } from "react-native" ;
4
+ import {
5
+ onSnapshot ,
6
+ doc ,
7
+ collection ,
8
+ query ,
9
+ orderBy ,
10
+ } from "firebase/firestore" ;
11
+ import React , { useState , useEffect } from "react" ;
12
+ import {
13
+ View ,
14
+ Text ,
15
+ StyleSheet ,
16
+ TouchableOpacity ,
17
+ FlatList ,
18
+ Pressable ,
19
+ } from "react-native" ;
4
20
5
- import { auth } from "../config" ;
21
+ import { auth , db } from "../config" ;
6
22
7
23
const WEB_API_KEY = "AIzaSyCJ2FY69u3jR8WMVLCT_TDrkKyqkUE2Y3k" ;
8
24
9
25
const copyInvitationUrl = async ( ) => {
10
- try {
11
- const payload = {
12
- dynamicLinkInfo : {
13
- domainUriPrefix : "https://dogether.page.link" ,
14
- link : `https://dogether-78b6f.web.app/user/${ auth . currentUser . uid } ` ,
15
- androidInfo : {
16
- androidPackageName : "gg.dogether.app" ,
17
- } ,
18
- socialMetaTagInfo : {
19
- socialTitle : `New invite from ${ auth . currentUser . displayName } ` ,
20
- socialDescription : `${ auth . currentUser . displayName } is using Dogether to stay accountable and motivated. Join them on their journey!` ,
21
- // socialImageLink: "<image-url>",
22
- } ,
26
+ const payload = {
27
+ dynamicLinkInfo : {
28
+ domainUriPrefix : "https://dogether.page.link" ,
29
+ link : `https://dogether-78b6f.web.app/user/${ auth . currentUser . uid } ` ,
30
+ androidInfo : {
31
+ androidPackageName : "gg.dogether.app" ,
23
32
} ,
24
- } ;
33
+ socialMetaTagInfo : {
34
+ socialTitle : `New invite from ${ auth . currentUser . displayName } ` ,
35
+ socialDescription : `${ auth . currentUser . displayName } is using Dogether to stay accountable and motivated. Join them on their journey!` ,
36
+ // socialImageLink: "<image-url>",
37
+ } ,
38
+ } ,
39
+ } ;
25
40
26
- const url = `https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=${ WEB_API_KEY } ` ;
27
- const response = await fetch ( url , {
41
+ const url = `https://firebasedynamiclinks.googleapis.com/v1/shortLinks?key=${ WEB_API_KEY } ` ;
42
+ const [ error , response ] = await to (
43
+ fetch ( url , {
28
44
method : "POST" ,
29
45
headers : {
30
46
"Content-Type" : "application/json" ,
31
47
} ,
32
48
body : JSON . stringify ( payload ) ,
33
- } ) ;
34
- const json = await response . json ( ) ;
35
- await Clipboard . setStringAsync ( json . shortLink ) ;
36
- alert ( "Copied to Clipboard!" ) ;
37
- } catch ( error ) {
49
+ } )
50
+ ) ;
51
+ if ( error ) {
38
52
alert ( error . message ) ;
53
+ return ;
39
54
}
55
+ const json = await response . json ( ) ;
56
+ await Clipboard . setStringAsync ( json . shortLink ) ;
57
+ alert ( "Copied to Clipboard!" ) ;
40
58
} ;
59
+
41
60
const Partner = ( ) => {
61
+ const [ hasPartner , setHasPartner ] = useState ( false ) ;
62
+ const [ todos , setTodos ] = useState ( [ ] ) ;
63
+
64
+ useEffect ( ( ) => {
65
+ const unsubHasPartner = onSnapshot (
66
+ doc ( db , `users/${ auth . currentUser . uid } ` ) ,
67
+ ( doc ) => {
68
+ const { partner } = doc . data ( ) ;
69
+ setHasPartner ( ! ! partner ) ;
70
+ } ,
71
+ ( error ) => {
72
+ console . log ( error ) ;
73
+ }
74
+ ) ;
75
+
76
+ const unsubPartnerTodos = onSnapshot (
77
+ query (
78
+ collection ( db , `users/${ auth . currentUser . uid } /todos` ) ,
79
+ orderBy ( "createdAt" , "desc" )
80
+ ) ,
81
+ ( querySnapshot ) => {
82
+ const todos = [ ] ;
83
+ querySnapshot . forEach ( ( doc ) => {
84
+ const { title, status } = doc . data ( ) ;
85
+ todos . push ( {
86
+ id : doc . id ,
87
+ title,
88
+ status,
89
+ } ) ;
90
+ } ) ;
91
+ setTodos ( todos ) ;
92
+ console . log ( "todos: " , todos , "todos.length: " , todos . length ) ;
93
+ } ,
94
+ ( error ) => {
95
+ console . log ( error ) ;
96
+ }
97
+ ) ;
98
+
99
+ return ( ) => {
100
+ unsubHasPartner ( ) ;
101
+ unsubPartnerTodos ( ) ;
102
+ } ;
103
+ } , [ ] ) ;
104
+
105
+ const showImage = ( item ) => {
106
+ alert ( "Show image" ) ;
107
+ } ;
108
+
109
+ if ( hasPartner ) {
110
+ return (
111
+ < View style = { { flex : 1 } } >
112
+ < FlatList
113
+ data = { todos }
114
+ numColumns = { 1 }
115
+ keyExtractor = { ( item ) => item . id }
116
+ renderItem = { ( { item } ) => (
117
+ < View style = { styles . container } >
118
+ < Ionicons
119
+ name = {
120
+ {
121
+ unfinished : "md-square-outline" ,
122
+ finished : "md-alert-circle-outline" ,
123
+ verified : "md-checkmark-circle-outline" ,
124
+ } [ item . status ]
125
+ }
126
+ size = { 24 }
127
+ color = "black"
128
+ style = { styles . todoIcon }
129
+ />
130
+ < Pressable
131
+ style = { styles . innerContainer }
132
+ onPress = { ( ) => {
133
+ if ( item . status === "finished" ) {
134
+ showImage ( item ) ;
135
+ }
136
+ } }
137
+ >
138
+ < Text style = { styles . itemHeading } >
139
+ { item . title . charAt ( 0 ) . toUpperCase ( ) + item . title . slice ( 1 ) }
140
+ </ Text >
141
+ </ Pressable >
142
+ </ View >
143
+ ) }
144
+ />
145
+ </ View >
146
+ ) ;
147
+ }
148
+
42
149
return (
43
150
< View style = { styles . container } >
44
151
< Text style = { styles . message } > Oops! You don't have a partner yet!</ Text >
@@ -50,6 +157,7 @@ const Partner = () => {
50
157
} ;
51
158
52
159
export default Partner ;
160
+
53
161
const styles = StyleSheet . create ( {
54
162
message : {
55
163
fontWeight : "bold" ,
0 commit comments