datastake-daf 0.6.536 → 0.6.538

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "datastake-daf",
3
- "version": "0.6.536",
3
+ "version": "0.6.538",
4
4
  "dependencies": {
5
5
  "@ant-design/icons": "^5.2.5",
6
6
  "@antv/g2": "^5.1.1",
@@ -22,6 +22,7 @@
22
22
  "docx-preview": "^0.3.7",
23
23
  "dot-object": "^2.1.5",
24
24
  "file-saver": "^2.0.5",
25
+ "firebase": "^12.4.0",
25
26
  "html-to-image": "^1.11.13",
26
27
  "leaflet": "^1.0.3",
27
28
  "leaflet-editable": "^1.3.0",
package/rollup.config.js CHANGED
@@ -46,6 +46,7 @@ const external = [
46
46
  "file-saver",
47
47
  "axios",
48
48
  "form-data",
49
+ "firebase"
49
50
  ];
50
51
 
51
52
  // Four builds, components, utils, hooks, context
@@ -0,0 +1,28 @@
1
+ import React, { createContext, useContext } from "react";
2
+ import useNotifications from "../useNotifications.js";
3
+
4
+ const status = 'all';
5
+
6
+ export const NotificationsHistoryContext = createContext({
7
+ loading: false,
8
+ _notifications: [],
9
+ _fetch: [],
10
+ total: 0,
11
+ });
12
+
13
+ // eslint-disable-next-line react/prop-types
14
+ export const NotificationsHistoryProvider = ({ children }) => {
15
+ const { loading, _notifications, _fetch, total } = useNotifications({ status });
16
+ const value = { loading, _notifications, _fetch, total };
17
+
18
+ return (
19
+ <NotificationsHistoryContext.Provider value={value}>
20
+ {children}
21
+ </NotificationsHistoryContext.Provider>
22
+ )
23
+ };
24
+
25
+ export const useNotificationsHistoryContext = () => {
26
+ const value = useContext(NotificationsHistoryContext);
27
+ return value;
28
+ }
@@ -0,0 +1,84 @@
1
+ import { useCallback, useEffect, useState } from "react";
2
+ import NotificationService from '../../../services/NotificationService.js';
3
+
4
+ export default function useNotifications({ status = 'unread' }) {
5
+ const [total, setTotal] = useState(0);
6
+ const [loading, setLoading] = useState(false);
7
+ const [notifications, setNotifications] = useState([]);
8
+
9
+ const addNotification = useCallback((notification) => {
10
+ if (notification) {
11
+ // push
12
+ setTotal((p) => p + 1);
13
+ setNotifications((p) => [...p, notification]);
14
+ }
15
+ }, []);
16
+
17
+ const removeNotification = useCallback(async (id) => {
18
+ try {
19
+ await NotificationService.readOne(id);
20
+ setTotal((p) => p - 1);
21
+ setNotifications((p) => p.filter((n) => n.id !== id));
22
+ } catch (err) {
23
+ console.log(err);
24
+ // TO-DO Remove this
25
+ setTotal((p) => p - 1);
26
+ setNotifications((p) => p.filter((n) => n.id !== id));
27
+ }
28
+ }, []);
29
+
30
+ const clearAll = useCallback(async () => {
31
+ try {
32
+ await NotificationService.readAll();
33
+ setTotal(0);
34
+ setNotifications([]);
35
+ } catch (err) {
36
+ console.log(err);
37
+ // TO-DO Remove this
38
+ setTotal(0);
39
+ setNotifications([]);
40
+ }
41
+ }, []);
42
+
43
+ const _fetch = async () => {
44
+ if ((notifications.length && total >= notifications.length) || loading) {
45
+ return;
46
+ }
47
+
48
+ setLoading(true);
49
+
50
+ try {
51
+ const _res = await NotificationService.get({ status });
52
+ if (_res.data) {
53
+ setTotal(() => _res.data.total);
54
+ setNotifications((p) => [...p, ..._res.data.data]);
55
+ setLoading(false);
56
+ return;
57
+ }
58
+ setLoading(false);
59
+
60
+ } catch {
61
+ setLoading(false);
62
+ setTotal(0);
63
+ setNotifications([]);
64
+ }
65
+ }
66
+
67
+ useEffect(() => {
68
+ _fetch();
69
+ }, []);
70
+
71
+ const _notifications = notifications
72
+ .sort((a, b) => b.createdAt - a.createdAt);
73
+
74
+ return {
75
+ total,
76
+ loading,
77
+ notifications,
78
+ _notifications,
79
+ _fetch,
80
+ removeNotification,
81
+ addNotification,
82
+ clearAll,
83
+ };
84
+ }
@@ -0,0 +1,53 @@
1
+ import { useState, useEffect } from "react";
2
+ import { initializeApp } from "firebase/app";
3
+ import { isSupported, getMessaging, getToken, onMessage } from "firebase/messaging";
4
+
5
+ export const useFirebase = (firebaseConfig, vapidKey, onFirebaseMessage = () => {}) => {
6
+ const [app] = useState(initializeApp(firebaseConfig));
7
+ const [messaging, setMessaging] = useState();
8
+ const [firebaseToken, setFirebaseToken] = useState();
9
+
10
+ useEffect(() => {
11
+ isSupported().then((supported) => {
12
+ if (supported) {
13
+ setMessaging(getMessaging(app));
14
+ }
15
+ });
16
+ }, [setMessaging, app]);
17
+
18
+ const notificationHandler = function (permission) {
19
+ console.log("Notification permission: ", permission, messaging);
20
+ if (permission === "granted" && messaging) {
21
+ getToken(messaging, { vapidKey })
22
+ .then((token) => {
23
+ setFirebaseToken(token);
24
+ })
25
+ .catch((err) => {
26
+ console.error("Firebase Error: ", err);
27
+ });
28
+ }
29
+ }
30
+
31
+ useEffect(() => {
32
+ if ('Notification' in window) {
33
+ try {
34
+ Notification.requestPermission().then((permission) => notificationHandler(permission));
35
+ } catch (error) {
36
+ if (error instanceof TypeError) {
37
+ Notification.requestPermission((permission) => notificationHandler(permission));
38
+ } else {
39
+ console.log(error)
40
+ }
41
+ }
42
+ }
43
+ }, [messaging]);
44
+
45
+ useEffect(() => {
46
+ if (messaging) {
47
+ const unsubscribe = onMessage(messaging, onFirebaseMessage);
48
+ return () => unsubscribe();
49
+ }
50
+ }, [messaging, onFirebaseMessage]);
51
+
52
+ return { firebaseToken };
53
+ }
@@ -0,0 +1,21 @@
1
+ import { useCallback, useMemo } from 'react';
2
+
3
+ export default function useIsDatastake({location, APP}) {
4
+ const isDatastake = useMemo(() => location.pathname.includes(`/${APP}`), [location]);
5
+
6
+ const getRedirectLink = useCallback((link) => {
7
+ if (isDatastake) {
8
+ return `/${APP}${link}`;
9
+ }
10
+ return link;
11
+ }, [isDatastake])
12
+
13
+ return { getRedirectLink, isDatastake };
14
+ }
15
+
16
+ export const getRedirectLink = (link, APP) => {
17
+ if (window.location.pathname.includes(`/${APP}`)) {
18
+ return `${APP}${link}`;
19
+ }
20
+ return link;
21
+ }
package/src/context.js CHANGED
@@ -4,6 +4,7 @@ export { useForms, FormsProvider, NOTIFICATION_MODE, onBeforeUnload, FormsContex
4
4
  export { useHistory, HistoryContext, HistoryProvider } from './@daf/core/context/History/index'
5
5
  export { GeneralProvider, useGeneralContext } from "./@daf/core/context/General/index.js";
6
6
  export { LinkingProvider, LinkingContext } from "./@daf/core/context/Linking/index.js";
7
- export { ApplicationsProvider, ApplicationsContext } from "./@daf/core/context/Applications/index.js";
7
+ export { ApplicationsProvider, ApplicationsContext, useApplications } from "./@daf/core/context/Applications/index.js";
8
8
  export { ProjectSourcesProvider, ProjectSourcesContext } from "./@daf/core/context/ProjectSources/index.js";
9
- export { SelectProvider, SelectContext } from "./@daf/core/context/Select/index.js";
9
+ export { SelectProvider, SelectContext } from "./@daf/core/context/Select/index.js";
10
+ export { NotificationsHistoryContext, useNotificationsHistoryContext, NotificationsHistoryProvider } from "./@daf/core/context/Notifications/NotificationsHistory/index.js"
package/src/hooks.js CHANGED
@@ -8,4 +8,5 @@ export { useMapConfig } from "./@daf/hooks/useMapHelper.js";
8
8
  export { useIsMobile } from "./@daf/hooks/useIsMobile";
9
9
  export { default as useSource } from "./@daf/hooks/useSources.js";
10
10
  export { useMapOnExpandableWidget } from "./@daf/hooks/useMapOnExpandableWidget";
11
- export { checkPermission, checkPermissionList, usePermissions } from "./@daf/hooks/usePermissions";
11
+ export { checkPermission, checkPermissionList, usePermissions } from "./@daf/hooks/usePermissions";
12
+ export { useFirebase } from "./@daf/hooks/useFirebase.js"