@@ -3,7 +3,12 @@ import * as ReactDOM from 'react-dom';
3
3
import { RouteComponentProps } from 'react-router' ;
4
4
import { Frame , Cover , Title , Label , Error , Input , Button , IconUser , HeaderAuth as Header , FooterAuth as Footer } from 'ts/component' ;
5
5
import { observer , inject } from 'mobx-react' ;
6
- import { Util } from 'ts/lib' ;
6
+ import { dispatcher , Util } from 'ts/lib' ;
7
+
8
+ const { dialog } = window . require ( 'electron' ) . remote ;
9
+ const Err : any = {
10
+ FAILED_TO_SET_AVATAR : 103
11
+ } ;
7
12
8
13
interface Props extends RouteComponentProps < any > {
9
14
authStore ?: any ;
@@ -17,18 +22,17 @@ interface State {
17
22
@observer
18
23
class PageAuthRegister extends React . Component < Props , State > {
19
24
20
- fileRef : any ;
21
25
nameRef : any ;
22
26
23
27
state = {
24
28
error : '' ,
25
- preview : ''
29
+ preview : '' ,
26
30
} ;
27
31
28
32
constructor ( props : any ) {
29
- super ( props ) ;
33
+ super ( props ) ;
30
34
31
- this . onFileChange = this . onFileChange . bind ( this ) ;
35
+ this . onFileClick = this . onFileClick . bind ( this ) ;
32
36
this . onNameChange = this . onNameChange . bind ( this ) ;
33
37
this . onSubmit = this . onSubmit . bind ( this ) ;
34
38
} ;
@@ -37,7 +41,7 @@ class PageAuthRegister extends React.Component<Props, State> {
37
41
const { error, preview } = this . state ;
38
42
const { authStore } = this . props ;
39
43
40
- return (
44
+ return (
41
45
< div >
42
46
< Cover num = { 3 } />
43
47
< Header />
@@ -47,28 +51,33 @@ class PageAuthRegister extends React.Component<Props, State> {
47
51
< Title text = "Add name and profile picture" />
48
52
< Error text = { error } />
49
53
50
- < div className = "fileWrap" >
54
+ < div className = "fileWrap" onMouseDown = { this . onFileClick } >
51
55
< IconUser icon = { preview } className = { preview ? 'active' : '' } />
52
- < Input ref = { ( ref : any ) => this . fileRef = ref } id = "file" type = "file" onChange = { this . onFileChange } />
53
56
</ div >
54
57
55
58
< Input ref = { ( ref : any ) => this . nameRef = ref } placeHolder = "Type your name" value = { name } onKeyUp = { this . onNameChange } />
56
59
< Button text = "Create profile" className = "orange" onClick = { this . onSubmit } />
57
60
</ Frame >
58
61
</ div >
59
62
) ;
60
- } ;
63
+ } ;
61
64
62
- onFileChange ( e : any ) {
63
- if ( ! e . target . files . length ) {
64
- return ;
65
- } ;
66
-
67
- let icon = e . target . files [ 0 ] ;
65
+ onFileClick ( e : any ) {
66
+ const { authStore } = this . props ;
68
67
69
- Util . loadPreviewBase64 ( icon , { } , ( image : string , param : any ) => {
70
- this . setState ( { preview : image } ) ;
71
- } ) ;
68
+ dialog . showOpenDialog ( { properties : [ 'openFile' ] } , ( files : any ) => {
69
+ if ( ( files == undefined ) || ! files . length ) {
70
+ return ;
71
+ } ;
72
+
73
+ let path = files [ 0 ] ;
74
+ let file = Util . makeFileFromPath ( path ) ;
75
+
76
+ authStore . iconSet ( path ) ;
77
+ Util . loadPreviewBase64 ( file , { } , ( image : string , param : any ) => {
78
+ this . setState ( { preview : image } ) ;
79
+ } ) ;
80
+ } ) ;
72
81
} ;
73
82
74
83
onNameChange ( e : any ) {
@@ -77,9 +86,40 @@ class PageAuthRegister extends React.Component<Props, State> {
77
86
} ;
78
87
79
88
onSubmit ( e : any ) {
89
+ const { authStore } = this . props ;
80
90
e . preventDefault ( ) ;
81
91
82
- this . props . history . push ( '/auth/pin-select/register' ) ;
92
+ let request = {
93
+ username : authStore . name ,
94
+ avatarLocalPath : authStore . icon
95
+ } ;
96
+
97
+ dispatcher . call ( 'accountCreate' , request , ( message : any ) => {
98
+ if ( message . error . code ) {
99
+ let error = '' ;
100
+ switch ( message . error . code ) {
101
+ case Err . FAILED_TO_SET_AVATAR :
102
+ error = 'Please select profile picture' ;
103
+ break ;
104
+ default :
105
+ error = message . error . desc ;
106
+ break ;
107
+ } ;
108
+ if ( error ) {
109
+ this . setState ( { error : error } ) ;
110
+ } ;
111
+ } else {
112
+ let account = message . account ;
113
+
114
+ authStore . accountSet ( {
115
+ id : account . id ,
116
+ name : account . name ,
117
+ icon : account . avatar ,
118
+ } ) ;
119
+
120
+ this . props . history . push ( '/auth/pin-select/register' ) ;
121
+ } ;
122
+ } ) ;
83
123
} ;
84
124
85
125
} ;
0 commit comments