@cocreate/users 1.12.3 → 1.13.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/package.json +6 -6
  3. package/src/client.js +94 -132
package/CHANGELOG.md CHANGED
@@ -1,3 +1,23 @@
1
+ # [1.13.0](https://github.com/CoCreate-app/CoCreate-users/compare/v1.12.4...v1.13.0) (2022-12-29)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * bump dependencies ([df8da39](https://github.com/CoCreate-app/CoCreate-users/commit/df8da39598a9f0339b2d56f02e45c16a61b2448b))
7
+
8
+
9
+ ### Features
10
+
11
+ * if userStatus is off trigger redirect ([afe3a67](https://github.com/CoCreate-app/CoCreate-users/commit/afe3a67890d9328dfdf106a81f9eb427aacf92d4))
12
+
13
+ ## [1.12.4](https://github.com/CoCreate-app/CoCreate-users/compare/v1.12.3...v1.12.4) (2022-12-27)
14
+
15
+
16
+ ### Bug Fixes
17
+
18
+ * bump dependecies ([9062616](https://github.com/CoCreate-app/CoCreate-users/commit/90626161128ec491198d11fbf684f1585ae1c33c))
19
+ * bump dependencies ([e38cb03](https://github.com/CoCreate-app/CoCreate-users/commit/e38cb034ddd56b5b336476a017d5aecc2846ebac))
20
+
1
21
  ## [1.12.3](https://github.com/CoCreate-app/CoCreate-users/compare/v1.12.2...v1.12.3) (2022-12-25)
2
22
 
3
23
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/users",
3
- "version": "1.12.3",
3
+ "version": "1.13.0",
4
4
  "description": "A simple users component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "users",
@@ -60,10 +60,10 @@
60
60
  "webpack-log": "^3.0.1"
61
61
  },
62
62
  "dependencies": {
63
- "@cocreate/actions": "^1.5.43",
64
- "@cocreate/crud-client": "^1.17.0",
65
- "@cocreate/docs": "^1.4.25",
66
- "@cocreate/element-prototype": "^1.2.12",
67
- "@cocreate/render": "^1.17.9"
63
+ "@cocreate/actions": "^1.5.45",
64
+ "@cocreate/crud-client": "^1.17.5",
65
+ "@cocreate/docs": "^1.4.27",
66
+ "@cocreate/element-prototype": "^1.2.17",
67
+ "@cocreate/render": "^1.17.11"
68
68
  }
69
69
  }
package/src/client.js CHANGED
@@ -6,14 +6,10 @@ import '@cocreate/element-prototype';
6
6
  import './index.css';
7
7
 
8
8
 
9
- const CONST_PERMISSION_CLASS = 'checkPermission';
10
-
11
9
  const CoCreateUser = {
12
10
  init: function() {
13
- // this.updatedCurrentOrg = false;
14
11
  this.initSocket();
15
12
  this.initChangeOrg();
16
- this.checkSession();
17
13
  },
18
14
 
19
15
  initSocket: function() {
@@ -34,9 +30,9 @@ const CoCreateUser = {
34
30
  detail: data
35
31
  }));
36
32
  });
37
- crud.listen('fetchedUser', this.checkPermissions);
38
- crud.listen('signIn', (instance) => self.signInResponse(instance));
39
- crud.listen('updateUserStatus', this.updateUserStatus);
33
+
34
+ crud.listen('signIn', (data) => self.signInResponse(data));
35
+ crud.listen('updateUserStatus', (data) => self.updateUserStatus(data));
40
36
  },
41
37
 
42
38
  signInRequest: function(btn) {
@@ -141,131 +137,6 @@ const CoCreateUser = {
141
137
  document.dispatchEvent(new CustomEvent('signOut'));
142
138
  },
143
139
 
144
- initChangeOrg: () => {
145
- const user_id = window.localStorage.getItem('user_id');
146
-
147
- if (!user_id) return;
148
-
149
- let orgChangers = document.querySelectorAll('.org-changer');
150
-
151
- for (let i = 0; i < orgChangers.length; i++) {
152
- let orgChanger = orgChangers[i];
153
-
154
- const collection = orgChanger.getAttribute('collection');
155
- const id = orgChanger.getAttribute('document_id');
156
-
157
- if (collection == 'users' && id == user_id) {
158
- orgChanger.addEventListener('selected', function(e) {
159
- // ToDo: can get selected value from event/element, readDocument not required.
160
- crud.readDocument({
161
- collection: collection || 'users',
162
- document: {
163
- _id: user_id
164
- },
165
- }).then((data) => {
166
- window.localStorage.setItem('apiKey', data['apiKey']);
167
- window.localStorage.setItem('organization_id', data.document[0]['current_org']);
168
- window.localStorage.setItem('host', crud.socket.config.host);
169
-
170
- document.dispatchEvent(new CustomEvent('signIn'));
171
- window.location.reload();
172
-
173
- })
174
-
175
- });
176
- }
177
- }
178
- },
179
-
180
- checkSession: () => {
181
- let user_id = window.localStorage.getItem('user_id');
182
- let token = window.localStorage.getItem('token');
183
- if (user_id && token) {
184
- let redirectTag = document.querySelector('[session="true"]');
185
-
186
- if (redirectTag) {
187
- let redirectLink = redirectTag.getAttribute('href');
188
- if (redirectLink) {
189
- document.location.href = redirectLink;
190
- }
191
- }
192
- }
193
- else {
194
- let redirectTag = document.querySelector('[session="false"]');
195
-
196
- if (redirectTag) {
197
- let redirectLink = redirectTag.getAttribute('href');
198
- if (redirectLink) {
199
- window.localStorage.removeItem("user_id");
200
- window.localStorage.removeItem("token");
201
-
202
- // this.deleteCookie();
203
- document.location.href = redirectLink;
204
- }
205
- }
206
- }
207
- },
208
-
209
- checkPermissions: (data) => {
210
- const tags = document.querySelectorAll('.' + CONST_PERMISSION_CLASS);
211
- tags.forEach((tag) => {
212
- let module_id = tag.getAttribute('document_id') ? tag.getAttribute('document_id') : tag.getAttribute('pass-document_id');
213
- let data_permission = tag.getAttribute('data-permission');
214
- let userPermission = data['permission-' + module_id];
215
-
216
- if (userPermission.indexOf(data_permission) == -1) {
217
- switch (data_permission) {
218
- case 'create':
219
- tag.style.display = 'none';
220
- break;
221
- case 'read':
222
- tag.style.display = 'none';
223
- break;
224
- case 'update':
225
- tag.style.display = 'none';
226
- break;
227
- case 'delete':
228
- tag.readOnly = true;
229
- break;
230
- default:
231
- // code
232
- }
233
- }
234
- else {
235
- switch (data_permission) {
236
-
237
- // code
238
- }
239
- }
240
- });
241
- },
242
-
243
- updateUserStatus: (data) => {
244
- if (!data.user_id) {
245
- return;
246
- }
247
- let statusEls = document.querySelectorAll(`[user-status][document_id='${data['user_id']}']`);
248
-
249
- statusEls.forEach((el) => {
250
- el.setAttribute('user-status', data['userStatus']);
251
- });
252
- },
253
-
254
- // ToDo: variations exist in a few components
255
- setDocumentId: function(collection, id) {
256
- let orgIdElements = document.querySelectorAll(`[collection='${collection}']`);
257
- if (orgIdElements && orgIdElements.length > 0) {
258
- orgIdElements.forEach((el) => {
259
- if (!el.getAttribute('document_id')) {
260
- el.setAttribute('document_id', id);
261
- }
262
- if (el.getAttribute('name') == "_id") {
263
- el.value = id;
264
- }
265
- });
266
- }
267
- },
268
-
269
140
  signUp: function(btn) {
270
141
  let form = btn.closest("form");
271
142
  if (!form) return;
@@ -328,7 +199,98 @@ const CoCreateUser = {
328
199
  broadcastBrowser: false
329
200
  });
330
201
  }
202
+ },
203
+
204
+ updateUserStatus: function(data) {
205
+ this.redirect(data)
206
+ if (data.user_id) {
207
+ let statusEls = document.querySelectorAll(`[user-status][document_id='${data['user_id']}']`);
208
+
209
+ statusEls.forEach((el) => {
210
+ el.setAttribute('user-status', data['userStatus']);
211
+ });
212
+ }
213
+
214
+ },
215
+
216
+ redirect: (data) => {
217
+ if (data.userStatus == 'on') {
218
+ let redirectTag = document.querySelector('[session="true"]');
219
+
220
+ if (redirectTag) {
221
+ let redirectLink = redirectTag.getAttribute('href');
222
+ if (redirectLink) {
223
+ document.location.href = redirectLink;
224
+ }
225
+ }
226
+ } else {
227
+ let redirectTag = document.querySelector('[session="false"]');
228
+
229
+ if (redirectTag) {
230
+ let redirectLink = redirectTag.getAttribute('href');
231
+ if (redirectLink) {
232
+ window.localStorage.removeItem("user_id");
233
+ window.localStorage.removeItem("token");
234
+
235
+ // this.deleteCookie();
236
+ document.location.href = redirectLink;
237
+ }
238
+ }
239
+ }
240
+
241
+ },
242
+
243
+ initChangeOrg: () => {
244
+ const user_id = window.localStorage.getItem('user_id');
245
+
246
+ if (!user_id) return;
247
+
248
+ let orgChangers = document.querySelectorAll('.org-changer');
249
+
250
+ for (let i = 0; i < orgChangers.length; i++) {
251
+ let orgChanger = orgChangers[i];
252
+
253
+ const collection = orgChanger.getAttribute('collection');
254
+ const id = orgChanger.getAttribute('document_id');
255
+
256
+ if (collection == 'users' && id == user_id) {
257
+ orgChanger.addEventListener('selected', function(e) {
258
+ // ToDo: can get selected value from event/element, readDocument not required.
259
+ crud.readDocument({
260
+ collection: collection || 'users',
261
+ document: {
262
+ _id: user_id
263
+ },
264
+ }).then((data) => {
265
+ window.localStorage.setItem('apiKey', data['apiKey']);
266
+ window.localStorage.setItem('organization_id', data.document[0]['current_org']);
267
+ window.localStorage.setItem('host', crud.socket.config.host);
268
+
269
+ document.dispatchEvent(new CustomEvent('signIn'));
270
+ window.location.reload();
271
+
272
+ })
273
+
274
+ });
275
+ }
276
+ }
277
+ },
278
+
279
+ // ToDo: variations of setDocumentId exists in a few components
280
+ setDocumentId: function(collection, id) {
281
+ let orgIdElements = document.querySelectorAll(`[collection='${collection}']`);
282
+ if (orgIdElements && orgIdElements.length > 0) {
283
+ orgIdElements.forEach((el) => {
284
+ if (!el.getAttribute('document_id')) {
285
+ el.setAttribute('document_id', id);
286
+ }
287
+ if (el.getAttribute('name') == "_id") {
288
+ el.value = id;
289
+ }
290
+ });
291
+ }
331
292
  }
293
+
332
294
  };
333
295
 
334
296