1
- import React , { useRef , useMemo , useState } from 'react' ;
1
+ import React , { useRef , useState } from 'react' ;
2
2
import {
3
3
ActivityIndicator ,
4
4
Animated ,
@@ -29,8 +29,7 @@ const hapticFeedbackOptions: HapticOptions = {
29
29
interface Item extends UploadItem {
30
30
progress ?: number ;
31
31
failed ?: boolean ; // true on timeout or error
32
- completedAt ?: number ; // when request is done
33
- startedAt ?: number ; // when request starts
32
+ completed ?: boolean ;
34
33
}
35
34
36
35
export default function App ( ) {
@@ -42,15 +41,15 @@ export default function App() {
42
41
field : 'file' ,
43
42
// optional below
44
43
method : 'POST' ,
45
- timeout : 60000 , // you can set this lower to cause timeouts to happen
44
+ timeout : 60 * 1000 , // you can set this lower to cause timeouts to happen
46
45
onProgress,
47
46
onDone : ( { item } ) => {
48
47
updateItem ( {
49
48
item,
50
49
keysAndValues : [
51
50
{
52
- key : 'completedAt ' ,
53
- value : new Date ( ) . getTime ( ) ,
51
+ key : 'completed ' ,
52
+ value : true ,
54
53
} ,
55
54
] ,
56
55
} ) ;
@@ -75,15 +74,12 @@ export default function App() {
75
74
} ,
76
75
} ) ;
77
76
78
- const isUploading = useMemo ( ( ) => {
79
- return data . some ( ( item ) => {
80
- return (
81
- typeof item . progress === 'number' &&
82
- item . progress > 0 &&
83
- item . progress < 100
84
- ) ;
85
- } ) ;
86
- } , [ data ] ) ;
77
+ const isUploading = data . some (
78
+ ( item ) =>
79
+ typeof item . progress === 'number' &&
80
+ item . progress > 0 &&
81
+ item . progress < 100
82
+ ) ;
87
83
88
84
const updateItem = < K extends keyof Item > ( {
89
85
item,
@@ -106,7 +102,7 @@ export default function App() {
106
102
} ) ;
107
103
} ;
108
104
109
- async function onProgress ( { item, event } : OnProgressData < Item > ) {
105
+ function onProgress ( { item, event } : OnProgressData < Item > ) {
110
106
const progress = event ?. loaded
111
107
? Math . round ( ( event . loaded / event . total ) * 100 )
112
108
: 0 ;
@@ -133,7 +129,6 @@ export default function App() {
133
129
setData ( ( prevState ) => [ ...prevState , ...items ] ) ;
134
130
} ;
135
131
136
- // :~)
137
132
const putItOnTheLine = async ( _data : Item [ ] ) => {
138
133
const promises = _data
139
134
. filter ( ( item ) => typeof item . progress !== 'number' ) // leave out any in progress or completed
@@ -143,13 +138,12 @@ export default function App() {
143
138
console . log ( 'result: ' , result ) ;
144
139
} ;
145
140
146
- const onPressUpload = async ( ) => {
141
+ const onPressUpload = ( ) => {
147
142
// allow uploading any that previously failed
148
143
setData ( ( prevState ) => {
149
144
const newState = [ ...prevState ] . map ( ( item ) => ( {
150
145
...item ,
151
146
failed : false ,
152
- startedAt : new Date ( ) . getTime ( ) ,
153
147
} ) ) ;
154
148
155
149
putItOnTheLine ( newState ) ;
@@ -172,26 +166,23 @@ export default function App() {
172
166
abortUpload ( item . uri ) ;
173
167
} ;
174
168
175
- const onPressRetry = ( item : Item ) => async ( ) => {
169
+ const onPressRetry = ( item : Item ) => ( ) => {
176
170
updateItem ( {
177
171
item,
178
172
keysAndValues : [
179
173
{
180
174
key : 'failed' ,
181
175
value : false ,
182
176
} ,
183
- {
184
- key : 'startedAt' ,
185
- value : new Date ( ) . getTime ( ) ,
186
- } ,
187
177
] ,
188
178
} ) ;
189
- startUpload ( { ...item , startedAt : new Date ( ) . getTime ( ) } ) . catch ( ( ) => { } ) ;
179
+ startUpload ( { ...item , failed : false } ) . catch ( ( ) => { } ) ;
190
180
} ;
191
181
192
182
const onDragStart = ( ) => {
193
183
ReactNativeHapticFeedback . trigger ( 'impactMedium' , hapticFeedbackOptions ) ;
194
184
dragStartAnimatedValue . current . setValue ( 1 ) ;
185
+ // unable to set useNativeDriver true because of a limitation in react-native-sortable-grid
195
186
Animated . loop (
196
187
Animated . sequence ( [
197
188
Animated . timing ( dragStartAnimatedValue . current , {
@@ -210,6 +201,7 @@ export default function App() {
210
201
211
202
const onDragRelease = ( _itemOrder : ItemOrder ) => {
212
203
//console.log('onDragRelease, itemOrder: ', _itemOrder);
204
+ // you can see where this can go :~)
213
205
} ;
214
206
215
207
const getDragStartAnimation = ( ) => {
@@ -245,9 +237,7 @@ export default function App() {
245
237
< Text style = { styles . iconText } > ↻</ Text >
246
238
</ Pressable >
247
239
) : null }
248
- { item . completedAt ? (
249
- < Text style = { styles . iconText } > ✓</ Text >
250
- ) : null }
240
+ { item . completed ? < Text style = { styles . iconText } > ✓</ Text > : null }
251
241
< Pressable style = { styles . deleteIcon } onPress = { onPressDeleteItem ( item ) } >
252
242
< Text style = { styles . deleteIconText } > ✗</ Text >
253
243
</ Pressable >
0 commit comments