-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore.js
133 lines (111 loc) · 2.87 KB
/
store.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { observable, action } from "mobx";
import axios from "axios";
import qs from "qs";
const baseURL = "https://penxy-mock-api.herokuapp.com/";
const login = "/token";
const signup = "/signup";
const profile = "/profile";
const requestParams = (
dataToSend,
url,
method = "POST",
headers = { "content-type": "application/x-www-form-urlencoded" }
) => ({
method: method,
headers: headers,
data: qs.stringify(dataToSend),
baseURL: baseURL,
url: url
});
class Store {
@observable
token = "";
@observable
isLoggedIn = false;
@observable
loginRequestState = "stop"; // pending | done | error | stop
@observable
signupRequestState = "stop"; // pending | done | error | stop
@observable
userName = "";
@observable
userEmail = "";
@observable
isModalVisible = false;
@observable
renderModelContent = "login";
@action
setToken(data) {
const { access_token } = data;
this.token = access_token;
sessionStorage.setItem("cord_token", access_token);
}
@action
setUser(data) {
const { name, email } = data;
this.userName = name;
this.userEmail = email;
}
@action
async login(dataToSend) {
this.loginRequestState = "pending";
try {
const params = requestParams(dataToSend, login);
const response = await axios(params);
this.loginRequestState = "done";
this.setToken(response.data);
this.setUser(response.data);
this.isLoggedIn = true;
this.isModalVisible = false;
} catch (error) {
this.loginRequestState = "error";
console.error(error);
this.loginRequestState = "stop";
}
}
@action
async signup(dataToSend) {
this.signupRequestState = "pending";
try {
const params = requestParams(dataToSend, signup);
const response = await axios(params);
this.signupRequestState = "done";
this.setToken(response.data);
this.setUser(response.data);
this.isLoggedIn = true;
this.isModalVisible = false;
} catch (error) {
this.signupRequestState = "error";
console.error(error);
this.signupRequestState = "stop";
}
}
@action
async saveProfile(dataToSend) {
this.saveProfileRequestState = "pending";
try {
const params = requestParams(dataToSend, profile, "PUT", {
Authorization: "Bearer " + this.token
});
const response = await axios(params);
this.saveProfileRequestState = "done";
this.setUser(response.data);
this.isLoggedIn = true;
} catch (error) {
this.saveProfileRequestState = "error";
console.error(error);
this.saveProfileRequestState = "stop";
}
}
@action
logOut() {
this.isLoggedIn = false;
this.token = "";
}
@action
toggleModal(toggleState, renderContent) {
this.isModalVisible = toggleState;
this.renderModelContent = renderContent;
}
}
export default new Store();