@firecms/user_management 3.0.0-canary.84 → 3.0.0-canary.86

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/dist/index.umd.js CHANGED
@@ -1,6 +1,6 @@
1
1
  (function(global, factory) {
2
- typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("react"), require("@firebase/firestore"), require("react/jsx-runtime"), require("@firecms/ui"), require("yup"), require("@firecms/core"), require("@firecms/formex"), require("date-fns"), require("date-fns/locale")) : typeof define === "function" && define.amd ? define(["exports", "react", "@firebase/firestore", "react/jsx-runtime", "@firecms/ui", "yup", "@firecms/core", "@firecms/formex", "date-fns", "date-fns/locale"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.FireCMS = {}, global.React, global.firestore, global.jsxRuntime, global.ui, global.Yup, global.core, global.formex, global.dateFns, global.locales));
3
- })(this, function(exports2, React, firestore, jsxRuntime, ui, Yup, core, formex, dateFns, locales) {
2
+ typeof exports === "object" && typeof module !== "undefined" ? factory(exports, require("react"), require("react-fast-compare"), require("react/jsx-runtime"), require("@firecms/ui"), require("yup"), require("@firecms/core"), require("@firecms/formex"), require("date-fns"), require("date-fns/locale")) : typeof define === "function" && define.amd ? define(["exports", "react", "react-fast-compare", "react/jsx-runtime", "@firecms/ui", "yup", "@firecms/core", "@firecms/formex", "date-fns", "date-fns/locale"], factory) : (global = typeof globalThis !== "undefined" ? globalThis : global || self, factory(global.FireCMS = {}, global.React, global.equal, global.jsxRuntime, global.ui, global.Yup, global.core, global.formex, global.dateFns, global.locales));
3
+ })(this, function(exports2, React, equal, jsxRuntime, ui, Yup, core, formex, dateFns, locales) {
4
4
  "use strict";
5
5
  function _interopNamespaceDefault(e) {
6
6
  const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
@@ -153,8 +153,8 @@
153
153
  const alpha = opacity / 100;
154
154
  return `rgba(${r}, ${g}, ${b}, ${alpha})`;
155
155
  }
156
- function useFirestoreUserManagement({
157
- firebaseApp,
156
+ function useBuildUserManagement({
157
+ dataSourceDelegate,
158
158
  usersPath = "__FIRECMS/config/users",
159
159
  rolesPath = "__FIRECMS/config/roles",
160
160
  usersLimit,
@@ -174,59 +174,56 @@
174
174
  const [usersError, setUsersError] = React.useState();
175
175
  const loading = rolesLoading || usersLoading;
176
176
  React.useEffect(() => {
177
- if (!firebaseApp || !rolesPath) return;
178
- const firestore$1 = firestore.getFirestore(firebaseApp);
179
- return firestore.onSnapshot(
180
- firestore.collection(firestore$1, rolesPath),
181
- {
182
- next: (snapshot) => {
183
- setRolesError(void 0);
184
- try {
185
- const newRoles = docsToRoles(snapshot.docs);
177
+ if (!dataSourceDelegate || !rolesPath) return;
178
+ if (dataSourceDelegate.initialised !== void 0 && !dataSourceDelegate.initialised) return;
179
+ return dataSourceDelegate.listenCollection?.({
180
+ path: rolesPath,
181
+ onUpdate(entities) {
182
+ setRolesError(void 0);
183
+ try {
184
+ const newRoles = entityToRoles(entities);
185
+ if (!equal(newRoles, roles))
186
186
  setRoles(newRoles);
187
- } catch (e) {
188
- console.error("Error loading roles", e);
189
- setRolesError(e);
190
- }
191
- setRolesLoading(false);
192
- },
193
- error: (e) => {
187
+ } catch (e) {
194
188
  console.error("Error loading roles", e);
195
189
  setRolesError(e);
196
- setRolesLoading(false);
197
190
  }
191
+ setRolesLoading(false);
192
+ },
193
+ onError(e) {
194
+ console.error("Error loading roles", e);
195
+ setRolesError(e);
196
+ setRolesLoading(false);
198
197
  }
199
- );
200
- }, [firebaseApp, rolesPath]);
198
+ });
199
+ }, [dataSourceDelegate, rolesPath]);
201
200
  React.useEffect(() => {
202
- if (!firebaseApp || !usersPath) return;
203
- const firestore$1 = firestore.getFirestore(firebaseApp);
204
- return firestore.onSnapshot(
205
- firestore.collection(firestore$1, usersPath),
206
- {
207
- next: (snapshot) => {
208
- setUsersError(void 0);
209
- try {
210
- const newUsers = docsToUsers(snapshot.docs);
201
+ if (!dataSourceDelegate || !usersPath) return;
202
+ if (dataSourceDelegate.initialised !== void 0 && !dataSourceDelegate.initialised) return;
203
+ return dataSourceDelegate.listenCollection?.({
204
+ path: usersPath,
205
+ onUpdate(entities) {
206
+ setUsersError(void 0);
207
+ try {
208
+ const newUsers = entitiesToUsers(entities);
209
+ if (!equal(newUsers, usersWithRoleIds))
211
210
  setUsersWithRoleIds(newUsers);
212
- } catch (e) {
213
- console.error("Error loading users", e);
214
- setUsersError(e);
215
- }
216
- setUsersLoading(false);
217
- },
218
- error: (e) => {
211
+ } catch (e) {
219
212
  console.error("Error loading users", e);
220
213
  setUsersError(e);
221
- setUsersLoading(false);
222
214
  }
215
+ setUsersLoading(false);
216
+ },
217
+ onError(e) {
218
+ console.error("Error loading users", e);
219
+ setUsersError(e);
220
+ setUsersLoading(false);
223
221
  }
224
- );
225
- }, [firebaseApp, usersPath]);
222
+ });
223
+ }, [dataSourceDelegate, usersPath]);
226
224
  const saveUser = React.useCallback(async (user) => {
227
- if (!firebaseApp) throw Error("useFirestoreUserManagement Firebase not initialised");
228
- const firestore$1 = firestore.getFirestore(firebaseApp);
229
- if (!firestore$1 || !usersPath) throw Error("useFirestoreUserManagement Firestore not initialised");
225
+ if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
226
+ if (!usersPath) throw Error("useBuildUserManagement Firestore not initialised");
230
227
  console.debug("Persisting user", user);
231
228
  const roleIds = user.roles?.map((r) => r.id);
232
229
  const {
@@ -238,45 +235,66 @@
238
235
  roles: roleIds
239
236
  };
240
237
  if (uid) {
241
- return firestore.setDoc(firestore.doc(firestore$1, usersPath, uid), data, { merge: true }).then(() => user);
238
+ return dataSourceDelegate.saveEntity({
239
+ status: "existing",
240
+ path: usersPath,
241
+ entityId: uid,
242
+ values: data
243
+ }).then(() => user);
242
244
  } else {
243
- return firestore.addDoc(firestore.collection(firestore$1, usersPath), data).then(() => user);
245
+ return dataSourceDelegate.saveEntity({
246
+ status: "new",
247
+ path: usersPath,
248
+ values: data
249
+ }).then(() => user);
244
250
  }
245
- }, [usersPath, firebaseApp]);
251
+ }, [usersPath, dataSourceDelegate]);
246
252
  const saveRole = React.useCallback((role) => {
247
- if (!firebaseApp) throw Error("useFirestoreUserManagement Firebase not initialised");
248
- const firestore$1 = firestore.getFirestore(firebaseApp);
249
- if (!firestore$1 || !rolesPath) throw Error("useFirestoreUserManagement Firestore not initialised");
253
+ if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
254
+ if (!rolesPath) throw Error("useBuildUserManagement Firestore not initialised");
250
255
  console.debug("Persisting role", role);
251
256
  const {
252
257
  id,
253
258
  ...roleData
254
259
  } = role;
255
- const ref = firestore.doc(firestore$1, rolesPath, id);
256
- return firestore.setDoc(ref, roleData, { merge: true });
257
- }, [rolesPath, firebaseApp]);
260
+ return dataSourceDelegate.saveEntity({
261
+ status: "existing",
262
+ path: rolesPath,
263
+ entityId: id,
264
+ values: roleData
265
+ }).then(() => {
266
+ return;
267
+ });
268
+ }, [rolesPath, dataSourceDelegate]);
258
269
  const deleteUser = React.useCallback(async (user) => {
259
- if (!firebaseApp) throw Error("useFirestoreUserManagement Firebase not initialised");
260
- const firestore$1 = firestore.getFirestore(firebaseApp);
261
- if (!firestore$1 || !usersPath) throw Error("useFirestoreUserManagement Firestore not initialised");
270
+ if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
271
+ if (!usersPath) throw Error("useBuildUserManagement Firestore not initialised");
262
272
  console.debug("Deleting", user);
263
273
  const { uid } = user;
264
- return firestore.deleteDoc(firestore.doc(firestore$1, usersPath, uid));
265
- }, [usersPath, firebaseApp]);
266
- const deleteRole = React.useCallback((role) => {
267
- if (!firebaseApp) throw Error("useFirestoreUserManagement Firebase not initialised");
268
- const firestore$1 = firestore.getFirestore(firebaseApp);
269
- if (!firestore$1 || !rolesPath) throw Error("useFirestoreUserManagement Firestore not initialised");
274
+ const entity = {
275
+ path: usersPath,
276
+ id: uid,
277
+ values: {}
278
+ };
279
+ await dataSourceDelegate.deleteEntity({ entity });
280
+ }, [usersPath, dataSourceDelegate]);
281
+ const deleteRole = React.useCallback(async (role) => {
282
+ if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
283
+ if (!rolesPath) throw Error("useBuildUserManagement Firestore not initialised");
270
284
  console.debug("Deleting", role);
271
285
  const { id } = role;
272
- const ref = firestore.doc(firestore$1, rolesPath, id);
273
- return firestore.deleteDoc(ref);
274
- }, [rolesPath, firebaseApp]);
286
+ const entity = {
287
+ path: usersPath,
288
+ id,
289
+ values: {}
290
+ };
291
+ await dataSourceDelegate.deleteEntity({ entity });
292
+ }, [rolesPath, dataSourceDelegate]);
275
293
  const collectionPermissions = React.useCallback(({
276
- collection: collection2,
294
+ collection,
277
295
  user
278
296
  }) => resolveUserRolePermissions({
279
- collection: collection2,
297
+ collection,
280
298
  user
281
299
  }), []);
282
300
  const defineRolesFor = React.useCallback((user) => {
@@ -320,22 +338,22 @@
320
338
  authenticator
321
339
  };
322
340
  }
323
- const docsToUsers = (docs) => {
324
- return docs.map((doc2) => {
325
- const data = doc2.data();
341
+ const entitiesToUsers = (docs) => {
342
+ return docs.map((doc) => {
343
+ const data = doc.values;
326
344
  const newVar = {
327
- uid: doc2.id,
345
+ uid: doc.id,
328
346
  ...data,
329
- created_on: data?.created_on?.toDate(),
330
- updated_on: data?.updated_on?.toDate()
347
+ created_on: data?.created_on,
348
+ updated_on: data?.updated_on
331
349
  };
332
350
  return newVar;
333
351
  });
334
352
  };
335
- const docsToRoles = (docs) => {
336
- return docs.map((doc2) => ({
337
- id: doc2.id,
338
- ...doc2.data()
353
+ const entityToRoles = (entities) => {
354
+ return entities.map((doc) => ({
355
+ id: doc.id,
356
+ ...doc.values
339
357
  }));
340
358
  };
341
359
  const UserManagementContext = React.createContext({});
@@ -1531,7 +1549,7 @@
1531
1549
  exports2.getUserRoles = getUserRoles;
1532
1550
  exports2.hexToRgbaWithOpacity = hexToRgbaWithOpacity;
1533
1551
  exports2.resolveUserRolePermissions = resolveUserRolePermissions;
1534
- exports2.useFirestoreUserManagement = useFirestoreUserManagement;
1552
+ exports2.useBuildUserManagement = useBuildUserManagement;
1535
1553
  exports2.useUserManagement = useUserManagement;
1536
1554
  exports2.useUserManagementPlugin = useUserManagementPlugin;
1537
1555
  exports2.userManagementAdminViews = userManagementAdminViews;