1
- import React , { useEffect , useRef , useState } from ' react' ;
1
+ import React , { useEffect , useRef , useState } from " react" ;
2
2
import {
3
3
View ,
4
4
Text ,
@@ -7,10 +7,10 @@ import {
7
7
// Button,
8
8
Alert ,
9
9
TouchableOpacity ,
10
- } from ' react-native' ;
11
- import { Icon , Button } from ' react-native-elements' ;
12
- import tw from ' tailwind-react-native-classnames' ;
13
- import * as ImagePicker from ' expo-image-picker' ;
10
+ } from " react-native" ;
11
+ import { Icon , Button } from " react-native-elements" ;
12
+ import tw from " tailwind-react-native-classnames" ;
13
+ import * as ImagePicker from " expo-image-picker" ;
14
14
15
15
const AddTransactionModal = ( {
16
16
visible,
@@ -29,7 +29,8 @@ const AddTransactionModal = ({
29
29
ref2 = useRef ( null ) ,
30
30
// [desc, setDesc] = useState(""),
31
31
// [amt, setAmt] = useState(null),
32
- [ image , setImage ] = useState ( '' ) ;
32
+ [ fileType , setFileType ] = useState ( "" ) ,
33
+ [ image , setImage ] = useState ( "" ) ;
33
34
34
35
useEffect ( ( ) => {
35
36
setLoading ( true ) ;
@@ -38,38 +39,44 @@ const AddTransactionModal = ({
38
39
39
40
const submitHandler = ( isGiving ) => {
40
41
if ( ! amt || ! desc ) {
41
- return Alert . alert ( ' All the fields are required' ) ;
42
+ return Alert . alert ( " All the fields are required" ) ;
42
43
}
43
- if ( isNaN ( amt ) ) return Alert . alert ( ' Amount should be a number' ) ;
44
+ if ( isNaN ( amt ) ) return Alert . alert ( " Amount should be a number" ) ;
44
45
if ( amt < 1 ) {
45
- return Alert . alert ( ' Amount must be grater than 0' ) ;
46
+ return Alert . alert ( " Amount must be grater than 0" ) ;
46
47
}
47
- fun ( isGiving , Number ( amt ) , desc , image ) ;
48
+ fun ( isGiving , Number ( amt ) , desc , image , fileType ) ;
48
49
} ;
49
50
50
51
const pickImage = async ( ) => {
51
52
const permission = await ImagePicker . requestMediaLibraryPermissionsAsync ( ) ;
52
53
53
54
if ( ! permission . granted ) {
54
- return Alert . alert ( ' Permission to access camera roll is required' ) ;
55
+ return Alert . alert ( " Permission to access camera roll is required" ) ;
55
56
}
56
57
const res = await ImagePicker . launchImageLibraryAsync ( {
57
58
base64 : true ,
58
59
quality : 0.5 ,
59
60
allowsEditing : true ,
60
61
mediaTypes : ImagePicker . MediaTypeOptions . Images ,
61
62
} ) ;
62
- setImage ( 'data:image/jpg;base64,' + res ?. base64 || null ) ;
63
+ if ( res . cancelled ) {
64
+ setFileType ( "" ) ;
65
+ setImage ( "" ) ;
66
+ } else {
67
+ setFileType ( res ?. uri ?. split ( "." ) [ res ?. uri ?. split ( "." ) . length - 1 ] ) ;
68
+ setImage ( "data:image/jpg;base64," + res ?. base64 || null ) ;
69
+ }
63
70
} ;
64
71
65
72
return (
66
73
< Modal
67
- animationType = ' fade'
74
+ animationType = " fade"
68
75
transparent = { true }
69
76
visible = { visible }
70
77
onRequestClose = { ( ) => {
71
78
setAmt ( null ) ;
72
- setDesc ( '' ) ;
79
+ setDesc ( "" ) ;
73
80
setSettling ( 0 ) ;
74
81
setVisible ( false ) ;
75
82
} }
@@ -78,13 +85,13 @@ const AddTransactionModal = ({
78
85
activeOpacity = { 1 }
79
86
onPress = { ( ) => {
80
87
setAmt ( null ) ;
81
- setDesc ( '' ) ;
88
+ setDesc ( "" ) ;
82
89
setSettling ( 0 ) ;
83
90
setVisible ( false ) ;
84
91
} }
85
92
style = { [
86
93
tw `h-full w-full absolute justify-center items-center` ,
87
- { backgroundColor : ' rgba(0,0,0,0.6)' } ,
94
+ { backgroundColor : " rgba(0,0,0,0.6)" } ,
88
95
] }
89
96
>
90
97
< TouchableOpacity
@@ -96,13 +103,13 @@ const AddTransactionModal = ({
96
103
style = { tw `absolute right-4 top-4 z-10` }
97
104
onPress = { ( ) => {
98
105
setAmt ( null ) ;
99
- setDesc ( '' ) ;
106
+ setDesc ( "" ) ;
100
107
setSettling ( 0 ) ;
101
108
setVisible ( false ) ;
102
109
} }
103
110
>
104
111
< Text style = { tw `text-black` } >
105
- < Icon type = ' feather' name = 'x' />
112
+ < Icon type = " feather" name = "x" />
106
113
</ Text >
107
114
</ TouchableOpacity >
108
115
< Text
@@ -113,10 +120,10 @@ const AddTransactionModal = ({
113
120
< TextInput
114
121
style = { tw `border border-gray-400 rounded p-2 m-2 mx-4` }
115
122
ref = { ref1 }
116
- placeholder = ' Amount'
117
- keyboardType = ' numeric'
118
- returnKeyType = ' next'
119
- value = { amt ?. toString ( ) || '' }
123
+ placeholder = " Amount"
124
+ keyboardType = " numeric"
125
+ returnKeyType = " next"
126
+ value = { amt ?. toString ( ) || "" }
120
127
onChangeText = { ( text ) => setAmt ( text ) }
121
128
onSubmitEditing = { ( ) => {
122
129
ref2 . current . focus ( ) ;
@@ -126,14 +133,14 @@ const AddTransactionModal = ({
126
133
< TextInput
127
134
style = { tw `border border-gray-400 rounded p-2 m-2 mx-4` }
128
135
ref = { ref2 }
129
- placeholder = ' Description'
136
+ placeholder = " Description"
130
137
value = { desc }
131
138
onChangeText = { ( text ) => setDesc ( text ) }
132
139
/>
133
140
< View style = { tw `mx-4 my-2` } >
134
141
< Button
135
142
onPress = { pickImage }
136
- title = ' Add Receipt'
143
+ title = " Add Receipt"
137
144
containerStyle = { tw `rounded-full` }
138
145
/>
139
146
</ View >
@@ -143,10 +150,10 @@ const AddTransactionModal = ({
143
150
< Button
144
151
loading = { loading }
145
152
onPress = { ( ) => submitHandler ( settling === 2 ) }
146
- title = ' Settle Up'
153
+ title = " Settle Up"
147
154
titleStyle = { tw `text-green-700` }
148
155
containerStyle = { tw `border border-gray-400 rounded-full` }
149
- type = ' clear'
156
+ type = " clear"
150
157
/>
151
158
</ View >
152
159
) : (
@@ -155,20 +162,20 @@ const AddTransactionModal = ({
155
162
< Button
156
163
loading = { loading }
157
164
onPress = { ( ) => submitHandler ( true ) }
158
- title = ' You Gave'
165
+ title = " You Gave"
159
166
titleStyle = { tw `text-red-700` }
160
167
containerStyle = { tw `border border-gray-400 rounded-full` }
161
- type = ' clear'
168
+ type = " clear"
162
169
/>
163
170
</ View >
164
171
< View style = { tw `p-2 w-1/2` } >
165
172
< Button
166
173
loading = { loading }
167
174
onPress = { ( ) => submitHandler ( false ) }
168
- title = ' You Got'
175
+ title = " You Got"
169
176
titleStyle = { tw `text-green-700` }
170
177
containerStyle = { tw `border border-gray-400 rounded-full` }
171
- type = ' clear'
178
+ type = " clear"
172
179
/>
173
180
</ View >
174
181
</ >
0 commit comments