Skip to content

Commit cca5241

Browse files
committed
account registration
1 parent c728f31 commit cca5241

File tree

6 files changed

+87
-21
lines changed

6 files changed

+87
-21
lines changed

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"css-loader": "^3.2.0",
5757
"electron": "^6.0.10",
5858
"emoji-mart": "^2.11.1",
59+
"file-type": "^12.3.0",
5960
"fs": "0.0.1-security",
6061
"history": "^4.10.1",
6162
"jquery": "^3.4.1",
@@ -69,6 +70,7 @@
6970
"react": "^16.9.0",
7071
"react-dom": "^16.9.0",
7172
"react-router-dom": "^5.0.1",
73+
"read-chunk": "^3.2.0",
7274
"readline": "^1.3.0",
7375
"sass-loader": "^8.0.0",
7476
"style-loader": "^1.0.0",

repo/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[^.]*

src/ts/component/page/auth/register.tsx

+59-19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import * as ReactDOM from 'react-dom';
33
import { RouteComponentProps } from 'react-router';
44
import { Frame, Cover, Title, Label, Error, Input, Button, IconUser, HeaderAuth as Header, FooterAuth as Footer } from 'ts/component';
55
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+
};
712

813
interface Props extends RouteComponentProps<any> {
914
authStore?: any;
@@ -17,18 +22,17 @@ interface State {
1722
@observer
1823
class PageAuthRegister extends React.Component<Props, State> {
1924

20-
fileRef: any;
2125
nameRef: any;
2226

2327
state = {
2428
error: '',
25-
preview: ''
29+
preview: '',
2630
};
2731

2832
constructor (props: any) {
29-
super(props);
33+
super(props);
3034

31-
this.onFileChange = this.onFileChange.bind(this);
35+
this.onFileClick = this.onFileClick.bind(this);
3236
this.onNameChange = this.onNameChange.bind(this);
3337
this.onSubmit = this.onSubmit.bind(this);
3438
};
@@ -37,7 +41,7 @@ class PageAuthRegister extends React.Component<Props, State> {
3741
const { error, preview } = this.state;
3842
const { authStore } = this.props;
3943

40-
return (
44+
return (
4145
<div>
4246
<Cover num={3} />
4347
<Header />
@@ -47,28 +51,33 @@ class PageAuthRegister extends React.Component<Props, State> {
4751
<Title text="Add name and profile picture" />
4852
<Error text={error} />
4953

50-
<div className="fileWrap">
54+
<div className="fileWrap" onMouseDown={this.onFileClick}>
5155
<IconUser icon={preview} className={preview ? 'active' : ''} />
52-
<Input ref={(ref: any) => this.fileRef = ref} id="file" type="file" onChange={this.onFileChange} />
5356
</div>
5457

5558
<Input ref={(ref: any) => this.nameRef = ref} placeHolder="Type your name" value={name} onKeyUp={this.onNameChange} />
5659
<Button text="Create profile" className="orange" onClick={this.onSubmit} />
5760
</Frame>
5861
</div>
5962
);
60-
};
63+
};
6164

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;
6867

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+
});
7281
};
7382

7483
onNameChange (e: any) {
@@ -77,9 +86,40 @@ class PageAuthRegister extends React.Component<Props, State> {
7786
};
7887

7988
onSubmit (e: any) {
89+
const { authStore } = this.props;
8090
e.preventDefault();
8191

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+
});
83123
};
84124

85125
};

src/ts/component/page/auth/setup.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ class PageAuthSetup extends React.Component<Props, State> {
5353
}, 1000);
5454

5555
if (match.params.id == 'register') {
56-
dispatcher.call('walletCreate', { pin: 'test' }, (message: any) => {
56+
let request = {
57+
rootPath: './repo',
58+
pin: 'test'
59+
};
60+
dispatcher.call('walletCreate', request, (message: any) => {
5761
this.props.history.push('/auth/register');
5862
});
5963
};

src/ts/lib/util.tsx

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
const loadImage = require('blueimp-load-image');
1+
const loadImage = window.require('blueimp-load-image');
2+
const fs = window.require('fs');
3+
const readChunk = window.require('read-chunk');
4+
const fileType = window.require('file-type');
25

36
class Util {
47

58
toCamelCase (str: string) {
69
return str[0].toUpperCase() + str.slice(1, str.length);
710
};
811

12+
makeFileFromPath (path: string) {
13+
let fn = path.split('/');
14+
let stat = fs.statSync(path);
15+
let buffer = readChunk.sync(path, 0, stat.size);
16+
let type = fileType(buffer);
17+
let file = new File([ new Blob([ buffer ]) ], fn[fn.length - 1], { type: type.mime });
18+
19+
return file;
20+
};
21+
922
loadPreviewCanvas (file: any, param: any, success?: (canvas: any) => void) {
1023
if (!file) {
1124
return;

src/ts/store/auth.tsx

+6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface AccountInterface {
99

1010
class AuthStore {
1111
@observable public pin: string = '';
12+
@observable public account: AccountInterface = null;
1213
@observable public accountList: AccountInterface[] = [];
1314
@observable public icon: string = '';
1415
@observable public name: string = '';
@@ -28,6 +29,11 @@ class AuthStore {
2829
this.accountList.push(account);
2930
};
3031

32+
@action
33+
accountSet (account: AccountInterface) {
34+
this.account = account as AccountInterface;
35+
};
36+
3137
@action
3238
iconSet (v: string) {
3339
this.icon = v;

0 commit comments

Comments
 (0)