@cocreate/organizations 1.21.6 → 1.22.1

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.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [1.22.1](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.22.0...v1.22.1) (2023-10-19)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * getFormData renamed to getData ([dba019c](https://github.com/CoCreate-app/CoCreate-organizations/commit/dba019c295054be92576e0d94b92966109dcd19e))
7
+
8
+ # [1.22.0](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.21.6...v1.22.0) (2023-10-16)
9
+
10
+
11
+ ### Features
12
+
13
+ * get() retuns promise to limit request to one ([67d59b3](https://github.com/CoCreate-app/CoCreate-organizations/commit/67d59b38bbaa6e80727a1f2327e0ef60896565d2))
14
+
1
15
  ## [1.21.6](https://github.com/CoCreate-app/CoCreate-organizations/compare/v1.21.5...v1.21.6) (2023-10-14)
2
16
 
3
17
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/organizations",
3
- "version": "1.21.6",
3
+ "version": "1.22.1",
4
4
  "description": "A simple organizations component in vanilla javascript. Easily configured using HTML5 attributes and/or JavaScript API.",
5
5
  "keywords": [
6
6
  "organizations",
package/src/client.js CHANGED
@@ -10,10 +10,6 @@ async function generateDB(organization = { object: {} }, user = { object: {} })
10
10
  const apikey = organization.object.key || uuid.generate();
11
11
  const user_id = user.object._id || Crud.ObjectId();
12
12
 
13
- let hasApiKey = await Crud.send({ method: 'read.object', database: organization_id, array: 'keys', organization_id })
14
- if (hasApiKey && hasApiKey.object && hasApiKey.object[0])
15
- return
16
-
17
13
  try {
18
14
  // Create organization
19
15
  organization.method = 'create.object'
@@ -23,7 +19,7 @@ async function generateDB(organization = { object: {} }, user = { object: {} })
23
19
  organization.object._id = organization_id
24
20
  organization.object.name = organization.object.name || 'untitiled'
25
21
  organization.organization_id = organization_id
26
- Crud.send(organization);
22
+ Indexeddb.send(organization);
27
23
 
28
24
  // Create user
29
25
  user.method = 'create.object'
@@ -34,7 +30,7 @@ async function generateDB(organization = { object: {} }, user = { object: {} })
34
30
  user.object.firstname = user.object.firstname || 'untitiled'
35
31
  user.object.lastname = user.object.lastname || 'untitiled'
36
32
  user.organization_id = organization_id
37
- Crud.send(user);
33
+ Indexeddb.send(user);
38
34
 
39
35
  // Create default key
40
36
  let key = {
@@ -54,7 +50,7 @@ async function generateDB(organization = { object: {} }, user = { object: {} })
54
50
  },
55
51
  organization_id
56
52
  }
57
- Crud.send(key);
53
+ Indexeddb.send(key);
58
54
 
59
55
  // Create role
60
56
  let role_id = Crud.ObjectId();
@@ -71,7 +67,7 @@ async function generateDB(organization = { object: {} }, user = { object: {} })
71
67
  },
72
68
  organization_id
73
69
  };
74
- Crud.send(role);
70
+ Indexeddb.send(role);
75
71
 
76
72
  // Create user key
77
73
  let userKey = {
@@ -90,7 +86,7 @@ async function generateDB(organization = { object: {} }, user = { object: {} })
90
86
  },
91
87
  organization_id
92
88
  };
93
- Crud.send(userKey);
89
+ Indexeddb.send(userKey);
94
90
 
95
91
  return { organization: organization.object, apikey, user: user.object, role: role.object, userKey: userKey.object }
96
92
 
@@ -144,42 +140,52 @@ async function getOrganizationFromServiceWorker() {
144
140
  });
145
141
  }
146
142
 
147
- async function createOrganization() {
143
+ let organizationPromise = null;
144
+ async function createOrganizationPromise() {
148
145
  let createOrganization = document.querySelector('[actions*="createOrganization"]')
146
+ if (createOrganization)
147
+ return Crud.socket.organization = 'canceled'
149
148
 
150
149
  if (Crud.socket.organization == 'canceled' || Crud.socket.organization == 'pending') return
151
150
 
152
- if (!createOrganization && confirm("An organization_id could not be found, if you already have an organization_id add it to this html and refresh the page.\n\nOr click 'OK' create a new organization") == true) {
153
- Crud.socket.organization = 'pending'
154
- if (Indexeddb) {
155
- try {
156
- let org = { object: {} }
157
- let { organization, apikey, user } = await generateDB(org)
158
- if (organization && apikey && user) {
159
- Crud.socket.apikey = apikey
160
- Crud.socket.user_id = user._id
161
- Config.set('organization_id', organization._id)
162
- Config.set('apikey', apikey)
163
- Config.set('user_id', user._id)
164
- Crud.socket.organization = true
165
- return organization._id
166
- }
167
- } catch (error) {
168
- console.error('Failed to load the script:', error);
151
+ const organization_id = prompt("An organization_id could not be found, if you already have an organization_id input it now.\n\nOr leave blank and click 'OK' to create a new organization");
152
+
153
+ if (organization_id === null)
154
+ return Crud.socket.organization = 'canceled'
155
+
156
+ Crud.socket.organization = 'pending'
157
+ if (Indexeddb) {
158
+ try {
159
+ let org = { object: {} }
160
+ if (organization_id)
161
+ org.object._id = organization_id
162
+ let { organization, apikey, user } = await generateDB(org)
163
+ if (organization && apikey && user) {
164
+ Crud.socket.apikey = apikey
165
+ Crud.socket.user_id = user._id
166
+ Config.set('organization_id', organization._id)
167
+ Config.set('apikey', apikey)
168
+ Config.set('user_id', user._id)
169
+ Crud.socket.organization = true
170
+ return organization._id
169
171
  }
172
+ } catch (error) {
173
+ console.error('Failed to load the script:', error);
170
174
  }
171
- } else {
172
- Crud.socket.organization = 'canceled'
173
175
  }
174
176
  }
175
177
 
176
178
 
179
+ async function createOrganization() {
180
+ return organizationPromise || (organizationPromise = createOrganizationPromise());
181
+ }
182
+
177
183
  async function create(btn) {
178
184
  let formEl = btn.closest("form");
179
185
  if (!formEl) return;
180
186
 
181
- let organization = Elements.getFormData(formEl, 'organizations')
182
- let user = Elements.getFormData(formEl, 'users')
187
+ let organization = Elements.getData(formEl, 'organizations')
188
+ let user = Elements.getData(formEl, 'users')
183
189
 
184
190
  if (!organization || !organization.object)
185
191
  return