@firecms/user_management 3.0.0-canary.83 → 3.0.0-canary.85

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/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/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.jsxRuntime, global.ui, global.Yup, global.core, global.formex, global.dateFns, global.locales));
3
+ })(this, function(exports2, React, 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,52 @@
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);
186
- setRoles(newRoles);
187
- } catch (e) {
188
- console.error("Error loading roles", e);
189
- setRolesError(e);
190
- }
191
- setRolesLoading(false);
192
- },
193
- error: (e) => {
177
+ if (!dataSourceDelegate || !rolesPath) return;
178
+ dataSourceDelegate.listenCollection?.({
179
+ path: rolesPath,
180
+ onUpdate(entities) {
181
+ setRolesError(void 0);
182
+ try {
183
+ const newRoles = entityToRoles(entities);
184
+ setRoles(newRoles);
185
+ } catch (e) {
194
186
  console.error("Error loading roles", e);
195
187
  setRolesError(e);
196
- setRolesLoading(false);
197
188
  }
189
+ setRolesLoading(false);
190
+ },
191
+ onError(e) {
192
+ console.error("Error loading roles", e);
193
+ setRolesError(e);
194
+ setRolesLoading(false);
198
195
  }
199
- );
200
- }, [firebaseApp, rolesPath]);
196
+ });
197
+ }, [dataSourceDelegate, rolesPath]);
201
198
  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);
211
- setUsersWithRoleIds(newUsers);
212
- } catch (e) {
213
- console.error("Error loading users", e);
214
- setUsersError(e);
215
- }
216
- setUsersLoading(false);
217
- },
218
- error: (e) => {
199
+ if (!dataSourceDelegate || !usersPath) return;
200
+ dataSourceDelegate.listenCollection?.({
201
+ path: usersPath,
202
+ onUpdate(entities) {
203
+ setUsersError(void 0);
204
+ try {
205
+ const newUsers = entitiesToUsers(entities);
206
+ setUsersWithRoleIds(newUsers);
207
+ } catch (e) {
219
208
  console.error("Error loading users", e);
220
209
  setUsersError(e);
221
- setUsersLoading(false);
222
210
  }
211
+ setUsersLoading(false);
212
+ },
213
+ onError(e) {
214
+ console.error("Error loading users", e);
215
+ setUsersError(e);
216
+ setUsersLoading(false);
223
217
  }
224
- );
225
- }, [firebaseApp, usersPath]);
218
+ });
219
+ }, [dataSourceDelegate, usersPath]);
226
220
  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");
221
+ if (!dataSourceDelegate) throw Error("useFirestoreUserManagement Firebase not initialised");
222
+ if (!usersPath) throw Error("useFirestoreUserManagement Firestore not initialised");
230
223
  console.debug("Persisting user", user);
231
224
  const roleIds = user.roles?.map((r) => r.id);
232
225
  const {
@@ -238,45 +231,66 @@
238
231
  roles: roleIds
239
232
  };
240
233
  if (uid) {
241
- return firestore.setDoc(firestore.doc(firestore$1, usersPath, uid), data, { merge: true }).then(() => user);
234
+ return dataSourceDelegate.saveEntity({
235
+ status: "existing",
236
+ path: usersPath,
237
+ entityId: uid,
238
+ values: data
239
+ }).then(() => user);
242
240
  } else {
243
- return firestore.addDoc(firestore.collection(firestore$1, usersPath), data).then(() => user);
241
+ return dataSourceDelegate.saveEntity({
242
+ status: "new",
243
+ path: usersPath,
244
+ values: data
245
+ }).then(() => user);
244
246
  }
245
- }, [usersPath, firebaseApp]);
247
+ }, [usersPath, dataSourceDelegate]);
246
248
  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");
249
+ if (!dataSourceDelegate) throw Error("useFirestoreUserManagement Firebase not initialised");
250
+ if (!rolesPath) throw Error("useFirestoreUserManagement Firestore not initialised");
250
251
  console.debug("Persisting role", role);
251
252
  const {
252
253
  id,
253
254
  ...roleData
254
255
  } = role;
255
- const ref = firestore.doc(firestore$1, rolesPath, id);
256
- return firestore.setDoc(ref, roleData, { merge: true });
257
- }, [rolesPath, firebaseApp]);
256
+ return dataSourceDelegate.saveEntity({
257
+ status: "existing",
258
+ path: rolesPath,
259
+ entityId: id,
260
+ values: roleData
261
+ }).then(() => {
262
+ return;
263
+ });
264
+ }, [rolesPath, dataSourceDelegate]);
258
265
  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");
266
+ if (!dataSourceDelegate) throw Error("useFirestoreUserManagement Firebase not initialised");
267
+ if (!usersPath) throw Error("useFirestoreUserManagement Firestore not initialised");
262
268
  console.debug("Deleting", user);
263
269
  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");
270
+ const entity = {
271
+ path: usersPath,
272
+ id: uid,
273
+ values: {}
274
+ };
275
+ await dataSourceDelegate.deleteEntity({ entity });
276
+ }, [usersPath, dataSourceDelegate]);
277
+ const deleteRole = React.useCallback(async (role) => {
278
+ if (!dataSourceDelegate) throw Error("useFirestoreUserManagement Firebase not initialised");
279
+ if (!rolesPath) throw Error("useFirestoreUserManagement Firestore not initialised");
270
280
  console.debug("Deleting", role);
271
281
  const { id } = role;
272
- const ref = firestore.doc(firestore$1, rolesPath, id);
273
- return firestore.deleteDoc(ref);
274
- }, [rolesPath, firebaseApp]);
282
+ const entity = {
283
+ path: usersPath,
284
+ id,
285
+ values: {}
286
+ };
287
+ await dataSourceDelegate.deleteEntity({ entity });
288
+ }, [rolesPath, dataSourceDelegate]);
275
289
  const collectionPermissions = React.useCallback(({
276
- collection: collection2,
290
+ collection,
277
291
  user
278
292
  }) => resolveUserRolePermissions({
279
- collection: collection2,
293
+ collection,
280
294
  user
281
295
  }), []);
282
296
  const defineRolesFor = React.useCallback((user) => {
@@ -320,22 +334,22 @@
320
334
  authenticator
321
335
  };
322
336
  }
323
- const docsToUsers = (docs) => {
324
- return docs.map((doc2) => {
325
- const data = doc2.data();
337
+ const entitiesToUsers = (docs) => {
338
+ return docs.map((doc) => {
339
+ const data = doc.values;
326
340
  const newVar = {
327
- uid: doc2.id,
341
+ uid: doc.id,
328
342
  ...data,
329
- created_on: data?.created_on?.toDate(),
330
- updated_on: data?.updated_on?.toDate()
343
+ created_on: data?.created_on,
344
+ updated_on: data?.updated_on
331
345
  };
332
346
  return newVar;
333
347
  });
334
348
  };
335
- const docsToRoles = (docs) => {
336
- return docs.map((doc2) => ({
337
- id: doc2.id,
338
- ...doc2.data()
349
+ const entityToRoles = (entities) => {
350
+ return entities.map((doc) => ({
351
+ id: doc.id,
352
+ ...doc.values
339
353
  }));
340
354
  };
341
355
  const UserManagementContext = React.createContext({});
@@ -1531,7 +1545,7 @@
1531
1545
  exports2.getUserRoles = getUserRoles;
1532
1546
  exports2.hexToRgbaWithOpacity = hexToRgbaWithOpacity;
1533
1547
  exports2.resolveUserRolePermissions = resolveUserRolePermissions;
1534
- exports2.useFirestoreUserManagement = useFirestoreUserManagement;
1548
+ exports2.useBuildUserManagement = useBuildUserManagement;
1535
1549
  exports2.useUserManagement = useUserManagement;
1536
1550
  exports2.useUserManagementPlugin = useUserManagementPlugin;
1537
1551
  exports2.userManagementAdminViews = userManagementAdminViews;