Skip to content

Commit fe7448d

Browse files
Register new device from settings form
1 parent 50f7fed commit fe7448d

File tree

2 files changed

+29
-56
lines changed

2 files changed

+29
-56
lines changed

src/Model/AARRStatAPI.ts

Lines changed: 16 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,23 @@
1-
export default class AARRStatAPI {
2-
baseUrl: string;
3-
apiKey: string;
4-
5-
constructor(baseUrl: string, apiKey: string) {
6-
const env = process.env.NODE_ENV || 'dev';
7-
this.baseUrl = (env === 'production') ? baseUrl : "";
8-
}
9-
10-
async rawGetRequest(auth: string, path: string) {
11-
let response = fetch(`${this.baseUrl}${path}`, {
12-
method: 'GET',
13-
headers: new Headers({
14-
'Accept': 'application/json',
15-
'Content-Type': 'application/json',
16-
'x-api-key': this.apiKey,
17-
}),
18-
});
19-
return response;
20-
}
21-
22-
async getRequest(auth: string, path: string) {
23-
// tslint:disable-next-line
24-
let rsp : any = await (
25-
await this.rawGetRequest(auth, path)
26-
.then(res => {
27-
if (res.status === 200) {
28-
return {
29-
data: res.json(),
30-
status: res.status
31-
};
32-
} else {
33-
return {
34-
status: res.status
35-
};
36-
}
37-
})
38-
.catch(err => {
39-
return {
40-
status: -1,
41-
message: "Request failed, please try again."
42-
};
43-
}));
44-
return rsp;
45-
}
1+
import { ApiBase } from './ApiBase';
462

3+
export default class AARRStatAPI extends ApiBase {
474
async ping() {
485
// tslint:disable-next-line
49-
return await this.getRequest(
50-
'',
6+
return await super.getRequest(
517
'/aarrstat/ping'
528
);
539
}
10+
11+
async newDevice(deviceId: string, userId: string, description: string) {
12+
return await this.postRequest(
13+
'/aarrstat/newdevice',
14+
JSON.stringify(
15+
{
16+
id: deviceId,
17+
user: userId,
18+
description: description
19+
}
20+
)
21+
);
22+
}
5423
}

src/Views/SettingsForm.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class SettingsForm extends React.Component<RootStore, SettingsFormState> {
7575

7676
// First fetch user info to get user id
7777
const authToken = localStorage.getItem('authToken') || "";
78-
let rsp : any = await OldReaderResource.userInfo(authToken);
78+
let rsp : any = await OldReaderResource.userInfo(authToken); // tslint:disable-line
7979
if (rsp.status !== 200) {
8080
this.setState({
8181
errorMessage: rsp.message,
@@ -84,7 +84,7 @@ class SettingsForm extends React.Component<RootStore, SettingsFormState> {
8484
return;
8585
}
8686
let data: any = await rsp.data; // tslint:disable-line
87-
console.log("OldReader user id: ", data.userId);
87+
console.log("OldReader user id: ", data.userId); // tslint:disable-line
8888

8989
// Create and store user id hash
9090
const userIdHash = sha256(data.userId).toString();
@@ -95,7 +95,7 @@ class SettingsForm extends React.Component<RootStore, SettingsFormState> {
9595
// This will stay the same as long as the app is installed
9696
let deviceIdHash = localStorage.getItem("deviceId") || "";
9797
if (deviceIdHash.length !== 64) {
98-
deviceIdHash = sha256(uuidv4()).toString()
98+
deviceIdHash = sha256(uuidv4()).toString();
9999
console.log("Hashed device id: ", deviceIdHash); // tslint:disable-line
100100
localStorage.setItem('deviceId', deviceIdHash);
101101
}
@@ -107,13 +107,14 @@ class SettingsForm extends React.Component<RootStore, SettingsFormState> {
107107
let aarrStatApi = new AARRStatApi(
108108
this.props.containerAppCallbacks.url,
109109
this.props.containerAppCallbacks.apiKey);
110-
111-
let response: any = await aarrStatApi.ping(); // tslint:disable-line
110+
let response: any = await aarrStatApi.newDevice(deviceIdHash, userIdHash, this.state.deviceName); // tslint:disable-line
112111
console.log(`Request result ${response.status}`); // tslint:disable-line
113-
114112
if (response.status !== 200) {
115-
// tslint:disable-next-line
116-
console.log(`Request failed, error code ${response.status}, response: ${response}`);
113+
console.log(`Request failed, error code ${response.status}, response: `, response); // tslint:disable-line
114+
this.setState({
115+
errorMessage: 'Request failed, please try again!',
116+
isSaving: false,
117+
});
117118
return;
118119
}
119120

@@ -123,6 +124,8 @@ class SettingsForm extends React.Component<RootStore, SettingsFormState> {
123124
} else {
124125
localStorage.setItem('enableTelemetry', "false");
125126
}
127+
128+
// Set state so that we properly update ui
126129
this.setState({
127130
isSaving: false,
128131
hasChanges: false,
@@ -153,7 +156,8 @@ class SettingsForm extends React.Component<RootStore, SettingsFormState> {
153156
"ui left input" :
154157
"ui left input disabled";
155158

156-
let errorMessageOrEmpty = this.state.errorMessage && <div className="ui error message">{this.state.errorMessage}</div>;
159+
let errorMessageOrEmpty = this.state.errorMessage &&
160+
<div className="ui error message">{this.state.errorMessage}</div>;
157161

158162
return (
159163
<div className="ui grid container">

0 commit comments

Comments
 (0)