@@ -6,11 +6,17 @@ import {
6
6
Button ,
7
7
Box ,
8
8
Theme ,
9
+ IconButton ,
9
10
} from '@material-ui/core' ;
10
11
import { red , grey } from '@material-ui/core/colors' ;
11
12
12
13
import YouTubeIcon from '@material-ui/icons/YouTube' ;
13
- import { uploadVideo , userYouTubeApiToken } from '@/services/api' ;
14
+ import {
15
+ getVideoMetaData ,
16
+ getVideoUrl ,
17
+ uploadVideo ,
18
+ userYouTubeApiToken ,
19
+ } from '@/services/api' ;
14
20
import { UserInfoExtended } from '@/models/user.model' ;
15
21
import { useUser } from '@/utils/auth/useUser' ;
16
22
import React , { useEffect , useMemo , useState } from 'react' ;
@@ -23,8 +29,12 @@ import DialogContentText from '@material-ui/core/DialogContentText';
23
29
import DialogTitle from '@material-ui/core/DialogTitle' ;
24
30
import DialogActions from '@material-ui/core/DialogActions' ;
25
31
import Snackbar from '@material-ui/core/Snackbar' ;
32
+ import CloseIcon from '@material-ui/icons/Close' ;
26
33
27
34
import firebase from 'firebase/app' ;
35
+ import { Course } from '@/models/course.model.ts' ;
36
+ import { Post } from '@/models/post.model' ;
37
+ import { take } from 'rxjs/operators' ;
28
38
29
39
const useStyles = makeStyles ( ( theme : Theme ) =>
30
40
createStyles ( {
@@ -64,10 +74,19 @@ const useStyles = makeStyles((theme: Theme) =>
64
74
rejectStyle : {
65
75
borderColor : '#ff1744' ,
66
76
} ,
77
+ close : {
78
+ padding : theme . spacing ( 0.5 ) ,
79
+ } ,
67
80
} )
68
81
) ;
69
82
70
- export default function YouTubeUpload ( ) : JSX . Element {
83
+ export default function YouTubeUpload ( {
84
+ setHistory,
85
+ history,
86
+ } : {
87
+ setHistory : React . Dispatch < React . SetStateAction < Post | Course | undefined > > ;
88
+ history : Post ;
89
+ } ) : JSX . Element {
71
90
const { user } : { user : UserInfoExtended | null | undefined } = useUser ( ) ;
72
91
const [ token , setToken ] = useState < { refresh_token : string } | null > ( null ) ;
73
92
const [ userToken$ , setUserToken$ ] = useState < Observable < {
@@ -110,7 +129,7 @@ export default function YouTubeUpload(): JSX.Element {
110
129
setSnackOpen ( true ) ;
111
130
} ;
112
131
const handleSnackClose = ( ) => {
113
- setSnackOpen ( true ) ;
132
+ setSnackOpen ( false ) ;
114
133
} ;
115
134
116
135
const [ upload$ , setUpload$ ] = useState < Observable < {
@@ -121,17 +140,35 @@ export default function YouTubeUpload(): JSX.Element {
121
140
if ( ! upload$ ) {
122
141
return ;
123
142
}
124
- const sub = upload$ ?. subscribe ( ( u ) => {
143
+ const sub = upload$ ?. subscribe ( ( m ) => {
125
144
handleClose ( ) ;
126
145
handleSnackOpen ( ) ;
127
- setUploadPercentage ( u . progress ) ;
128
- console . log ( 'Snapshot' , u . snapshot ) ;
146
+ setUploadPercentage ( m . progress ) ;
147
+ if ( m . progress === 100 ) {
148
+ getVideoUrl ( m . snapshot . ref . fullPath )
149
+ . pipe ( take ( 1 ) )
150
+ . subscribe ( ( u ) => console . log ( u ) ) ;
151
+ getVideoMetaData ( m . snapshot . ref . fullPath )
152
+ . pipe ( take ( 1 ) )
153
+ . subscribe ( ( d ) => console . log ( d ) ) ;
154
+ if ( sub ) {
155
+ sub . unsubscribe ( ) ;
156
+ setUpload$ ( null ) ;
157
+ }
158
+ }
129
159
} ) ;
130
160
return ( ) => {
131
161
if ( sub ) sub . unsubscribe ( ) ;
132
162
} ;
133
163
} , [ upload$ ] ) ;
134
164
165
+ const handleVidoUploadCancel = ( ) => {
166
+ upload$ ?. pipe ( take ( 1 ) ) . subscribe ( ( m ) => {
167
+ console . log ( m . snapshot . task ) ;
168
+ console . log ( m . snapshot . task . cancel ( ) ) ;
169
+ } ) ;
170
+ } ;
171
+
135
172
const onUploadVideo = ( ) => {
136
173
acceptedFiles . map ( ( file : any ) => setUpload$ ( uploadVideo ( '/alex' , file ) ) ) ;
137
174
} ;
@@ -246,7 +283,26 @@ export default function YouTubeUpload(): JSX.Element {
246
283
open = { snackOpen }
247
284
onClose = { handleSnackClose }
248
285
message = { `Uploading ${ uploadPercentage } ` }
249
- />
286
+ action = {
287
+ < React . Fragment >
288
+ < Button
289
+ color = "secondary"
290
+ size = "small"
291
+ onClick = { handleVidoUploadCancel }
292
+ >
293
+ CANCEL
294
+ </ Button >
295
+ < IconButton
296
+ aria-label = "close"
297
+ color = "inherit"
298
+ className = { classes . close }
299
+ onClick = { handleSnackClose }
300
+ >
301
+ < CloseIcon />
302
+ </ IconButton >
303
+ </ React . Fragment >
304
+ }
305
+ > </ Snackbar >
250
306
</ >
251
307
) }
252
308
</ >
0 commit comments