@@ -4,12 +4,16 @@ import type { State } from 'store';
4
4
import { connect } from 'react-redux' ;
5
5
import SettingProfile from 'components/settings/SettingProfile/SettingProfile' ;
6
6
import { type UserData } from 'store/modules/user' ;
7
- import { SettingsActions } from 'store/actionCreators' ;
7
+ import { SettingsActions , UserActions } from 'store/actionCreators' ;
8
8
import { type Profile } from 'store/modules/profile' ;
9
+ import { type UploadInfo } from 'store/modules/settings' ;
10
+ import axios from 'axios' ;
11
+ import storage , { keys } from 'lib/storage' ;
9
12
10
13
type Props = {
11
14
user : ?UserData ,
12
15
profile : ?Profile ,
16
+ uploadInfo : ?UploadInfo ,
13
17
} ;
14
18
15
19
class SettingProfileContainer extends Component < Props > {
@@ -29,21 +33,65 @@ class SettingProfileContainer extends Component<Props> {
29
33
}
30
34
}
31
35
36
+ uploadFile = async ( file : any ) => {
37
+ try {
38
+ await SettingsActions . createThumbnailSignedUrl ( file . name ) ;
39
+ if ( ! this . props . uploadInfo ) return ;
40
+ const { url, image_path : imagePath } = this . props . uploadInfo ;
41
+ await axios . put ( url , file , {
42
+ headers : {
43
+ 'Content-Type' : file . type ,
44
+ } ,
45
+ withCredentials : false ,
46
+ onUploadProgress : ( e ) => {
47
+ if ( window . nanobar ) {
48
+ window . nanobar . go ( e . loaded / e . total * 100 ) ;
49
+ }
50
+ } ,
51
+ } ) ;
52
+ const imageUrl = `https://images.velog.io/${ imagePath } ` ;
53
+ await SettingsActions . updateProfile ( {
54
+ thumbnail : imageUrl ,
55
+ } ) ;
56
+ await UserActions . checkUser ( ) ;
57
+ storage . set ( keys . user , this . props . user ) ;
58
+ } catch ( e ) {
59
+ console . log ( e ) ;
60
+ }
61
+ } ;
62
+ onUploadThumbnail = ( ) => {
63
+ const upload = document . createElement ( 'input' ) ;
64
+ upload . type = 'file' ;
65
+ upload . onchange = ( e ) => {
66
+ if ( ! upload . files ) return ;
67
+ const file = upload . files [ 0 ] ;
68
+ this . uploadFile ( file ) ;
69
+ } ;
70
+ upload . click ( ) ;
71
+ } ;
72
+
32
73
onUpdateProfile = ( { displayName, shortBio } : { displayName : string , shortBio : string } ) => {
33
74
return SettingsActions . updateProfile ( { displayName, shortBio } ) ;
34
75
} ;
35
76
36
77
render ( ) {
37
78
const { profile } = this . props ;
38
79
if ( ! profile ) return null ;
39
- return < SettingProfile profile = { profile } onUpdateProfile = { this . onUpdateProfile } /> ;
80
+ return (
81
+ < SettingProfile
82
+ profile = { profile }
83
+ onUpdateProfile = { this . onUpdateProfile }
84
+ onUploadThumbnail = { this . onUploadThumbnail }
85
+ />
86
+ ) ;
40
87
}
41
88
}
42
89
43
90
export default connect (
44
91
( { user, settings } : State ) => ( {
45
92
user : user . user ,
46
93
profile : settings . profile ,
94
+ uploadInfo : settings . uploadInfo ,
47
95
} ) ,
48
96
( ) => ( { } ) ,
49
97
) ( SettingProfileContainer ) ;
0 commit comments