Skip to content

Commit e875c8d

Browse files
committed
feat: opens socket chaner for user Status
1 parent 0443fd2 commit e875c8d

File tree

1 file changed

+43
-36
lines changed

1 file changed

+43
-36
lines changed

src/index.js

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ const CoCreateUser = {
1313
this.initSocket();
1414
this.initChangeOrg();
1515
this.checkSession();
16+
this.createUserSocket();
17+
},
18+
19+
createUserSocket: function() {
20+
var user_id = window.localStorage.getItem('user_id');
21+
if (user_id) {
22+
crud.socket.create({
23+
namespace: 'users',
24+
room: user_id,
25+
host: window.config.host
26+
})
27+
}
1628
},
1729

1830
initSocket: function() {
@@ -45,12 +57,12 @@ const CoCreateUser = {
4557
inputs.forEach((input) => {
4658
const name = input.getAttribute('name');
4759
let value = input.value;
48-
if(input.type == 'password') {
60+
if (input.type == 'password') {
4961
value = btoa(value);
5062
}
5163
collection = input.getAttribute('collection') || collection;
5264

53-
if(name) {
65+
if (name) {
5466
loginData[name] = value;
5567
}
5668
});
@@ -66,7 +78,7 @@ const CoCreateUser = {
6678
loginResult: function(data) {
6779
let { success, status, message, token } = data;
6880

69-
if(success) {
81+
if (success) {
7082
window.localStorage.setItem('user_id', data['id']);
7183
window.localStorage.setItem("token", token);
7284
document.cookie = `token=${token};path=/`;
@@ -75,24 +87,19 @@ const CoCreateUser = {
7587
document.dispatchEvent(new CustomEvent('login', {
7688
detail: {}
7789
}));
78-
7990
}
8091
else
8192
message = "The email or password you entered is incorrect";
8293

8394
render.data({
8495
selector: "[template_id='login']",
85-
data: {
86-
type: 'login',
87-
status,
88-
message,
89-
success
90-
}
96+
data: {
97+
type: 'login',
98+
status,
99+
message,
100+
success
101+
}
91102
});
92-
93-
document.dispatchEvent(new CustomEvent('login', {
94-
detail: {}
95-
}));
96103
},
97104

98105
getCurrentOrg: function(user_id, collection) {
@@ -122,7 +129,7 @@ const CoCreateUser = {
122129

123130
let allCookies = document.cookie.split(';');
124131

125-
for(var i = 0; i < allCookies.length; i++)
132+
for (var i = 0; i < allCookies.length; i++)
126133
document.cookie = allCookies[i] + "=;expires=" +
127134
new Date(0).toUTCString();
128135

@@ -133,24 +140,24 @@ const CoCreateUser = {
133140
initChangeOrg: () => {
134141
const user_id = window.localStorage.getItem('user_id');
135142

136-
if(!user_id) return;
143+
if (!user_id) return;
137144

138145
let orgChangers = document.querySelectorAll('.org-changer');
139146

140-
for(let i = 0; i < orgChangers.length; i++) {
147+
for (let i = 0; i < orgChangers.length; i++) {
141148
let orgChanger = orgChangers[i];
142149

143150
const collection = orgChanger.getAttribute('collection') ? orgChanger.getAttribute('collection') : 'module_activity';
144151
const id = orgChanger.getAttribute('document_id');
145152

146-
if(collection == 'users' && id == user_id) {
153+
if (collection == 'users' && id == user_id) {
147154
orgChanger.addEventListener('selectedValue', function(e) {
148155

149156
setTimeout(function() {
150157
getCurrentOrg(user_id);
151158

152159
var timer = setInterval(function() {
153-
if(updatedCurrentOrg) {
160+
if (updatedCurrentOrg) {
154161
window.location.reload();
155162

156163
clearInterval(timer);
@@ -165,22 +172,22 @@ const CoCreateUser = {
165172
checkSession: () => {
166173
let user_id = window.localStorage.getItem('user_id');
167174
let token = window.localStorage.getItem('token');
168-
if(user_id && token) {
175+
if (user_id && token) {
169176
let redirectTag = document.querySelector('[session="true"]');
170177

171-
if(redirectTag) {
178+
if (redirectTag) {
172179
let redirectLink = redirectTag.getAttribute('href');
173-
if(redirectLink) {
180+
if (redirectLink) {
174181
document.location.href = redirectLink;
175182
}
176183
}
177184
}
178185
else {
179186
let redirectTag = document.querySelector('[session="false"]');
180187

181-
if(redirectTag) {
188+
if (redirectTag) {
182189
let redirectLink = redirectTag.getAttribute('href');
183-
if(redirectLink) {
190+
if (redirectLink) {
184191
window.localStorage.clear();
185192
// this.deleteCookie();
186193
document.location.href = redirectLink;
@@ -196,8 +203,8 @@ const CoCreateUser = {
196203
let data_permission = tag.getAttribute('data-permission');
197204
let userPermission = data['permission-' + module_id];
198205

199-
if(userPermission.indexOf(data_permission) == -1) {
200-
switch(data_permission) {
206+
if (userPermission.indexOf(data_permission) == -1) {
207+
switch (data_permission) {
201208
case 'create':
202209
tag.style.display = 'none';
203210
break;
@@ -215,7 +222,7 @@ const CoCreateUser = {
215222
}
216223
}
217224
else {
218-
switch(data_permission) {
225+
switch (data_permission) {
219226

220227
// code
221228
}
@@ -224,7 +231,7 @@ const CoCreateUser = {
224231
},
225232

226233
changedUserStatus: (data) => {
227-
if(!data.user_id) {
234+
if (!data.user_id) {
228235
return;
229236
}
230237
let statusEls = document.querySelectorAll(`[user-status][document_id='${data['user_id']}']`);
@@ -236,12 +243,12 @@ const CoCreateUser = {
236243

237244
setDocumentId: function(collection, id) {
238245
let orgIdElements = document.querySelectorAll(`[collection='${collection}']`);
239-
if(orgIdElements && orgIdElements.length > 0) {
246+
if (orgIdElements && orgIdElements.length > 0) {
240247
orgIdElements.forEach((el) => {
241-
if(!el.getAttribute('document_id')) {
248+
if (!el.getAttribute('document_id')) {
242249
el.setAttribute('document_id', id);
243250
}
244-
if(el.getAttribute('name') == "_id") {
251+
if (el.getAttribute('name') == "_id") {
245252
el.value = id;
246253
}
247254
});
@@ -250,7 +257,7 @@ const CoCreateUser = {
250257

251258
createUserNew: function(btn) {
252259
let form = btn.closest("form");
253-
if(!form) return;
260+
if (!form) return;
254261
let newOrg_id = form.querySelector("input[collection='organizations'][name='_id']");
255262
let user_id = form.querySelector("input[collection='users'][name='_id']");
256263

@@ -268,22 +275,22 @@ const CoCreateUser = {
268275

269276
createUser: function(btn) {
270277
let form = btn.closest("form");
271-
if(!form) return;
278+
if (!form) return;
272279
let org_id = "";
273280
let elements = form.querySelectorAll("[collection='users'][name]");
274281
let orgIdElement = form.querySelector("input[collection='organizations'][name='_id']");
275282

276-
if(orgIdElement) {
283+
if (orgIdElement) {
277284
org_id = orgIdElement.value;
278285
}
279286
let data = {};
280287
//. get form data
281288
elements.forEach(el => {
282289
let name = el.getAttribute('name');
283290
let value = input.getValue(el) || el.getAttribute('value');
284-
if(!name || !value) return;
291+
if (!name || !value) return;
285292

286-
if(el.getAttribute('data-type') == 'array') {
293+
if (el.getAttribute('data-type') == 'array') {
287294
value = [value];
288295
}
289296
data[name] = value;

0 commit comments

Comments
 (0)