@selfcommunity/react-ui 0.8.0-alpha.1 → 0.8.0-alpha.2

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.
@@ -154,7 +154,12 @@ function Events(inProps) {
154
154
  * Subscriber for pubsub callback
155
155
  */
156
156
  const onDeleteEventHandler = (0, react_1.useCallback)((_msg, deleted) => {
157
- setEvents((prev) => prev.filter((e) => e.id !== deleted));
157
+ setEvents((prev) => {
158
+ if (prev.some((e) => e.id === deleted)) {
159
+ return prev.filter((e) => e.id !== deleted);
160
+ }
161
+ return prev;
162
+ });
158
163
  }, [events]);
159
164
  /**
160
165
  * On mount, subscribe to receive event updates (only delete)
@@ -20,6 +20,8 @@ const Event_1 = tslib_1.__importDefault(require("../Event"));
20
20
  const Widget_1 = tslib_1.__importDefault(require("../Widget"));
21
21
  const constants_1 = require("./constants");
22
22
  const Skeleton_1 = tslib_1.__importDefault(require("./Skeleton"));
23
+ const pubsub_js_1 = tslib_1.__importDefault(require("pubsub-js"));
24
+ const PubSub_1 = require("../../constants/PubSub");
23
25
  const classes = {
24
26
  root: `${constants_1.PREFIX}-root`,
25
27
  titleWrapper: `${constants_1.PREFIX}-title-wrapper`,
@@ -66,6 +68,8 @@ function MyEventsWidget(inProps) {
66
68
  const scRoutingContext = (0, react_core_1.useSCRouting)();
67
69
  const { features } = (0, react_core_1.useSCPreferences)();
68
70
  const eventsEnabled = (0, react_1.useMemo)(() => features && features.includes(types_1.SCFeatureName.EVENT) && features.includes(types_1.SCFeatureName.TAGGING), [features]);
71
+ // REFS
72
+ const updatesSubscription = (0, react_1.useRef)(null);
69
73
  /**
70
74
  * Initialize component
71
75
  * Fetch data only if the component is not initialized and it is not loading data
@@ -120,6 +124,30 @@ function MyEventsWidget(inProps) {
120
124
  _fetchNext();
121
125
  }
122
126
  }, [eventIndex, state.results]);
127
+ /**
128
+ * Subscriber for pubsub callback
129
+ */
130
+ const onDeleteEventHandler = (0, react_1.useCallback)((_msg, deleted) => {
131
+ const _events = [...state.results];
132
+ if (_events.some((e) => e.id === deleted)) {
133
+ const updatedEvents = _events.filter((e) => e.id !== deleted);
134
+ dispatch({
135
+ type: widget_1.actionWidgetTypes.SET_RESULTS,
136
+ payload: { results: updatedEvents }
137
+ });
138
+ }
139
+ }, [state.results]);
140
+ /**
141
+ * On mount, subscribe to receive event updates (only delete)
142
+ */
143
+ (0, react_1.useEffect)(() => {
144
+ if (state.results) {
145
+ updatesSubscription.current = pubsub_js_1.default.subscribe(`${PubSub_1.SCTopicType.EVENT}.${PubSub_1.SCGroupEventType.DELETE}`, onDeleteEventHandler);
146
+ }
147
+ return () => {
148
+ updatesSubscription.current && pubsub_js_1.default.unsubscribe(updatesSubscription.current);
149
+ };
150
+ }, [state.results]);
123
151
  // RENDER
124
152
  if (!eventsEnabled || (state.initialized && state.count === 0)) {
125
153
  return (0, jsx_runtime_1.jsx)(HiddenPlaceholder_1.default, {});
@@ -20,6 +20,8 @@ const Widget_1 = tslib_1.__importDefault(require("../Widget"));
20
20
  const Arrow_1 = tslib_1.__importDefault(require("./Arrow"));
21
21
  const constants_1 = require("./constants");
22
22
  const Skeleton_1 = tslib_1.__importDefault(require("./Skeleton"));
23
+ const pubsub_js_1 = tslib_1.__importDefault(require("pubsub-js"));
24
+ const PubSub_1 = require("../../constants/PubSub");
23
25
  const classes = {
24
26
  root: `${constants_1.PREFIX}-root`,
25
27
  content: `${constants_1.PREFIX}-content`,
@@ -58,6 +60,8 @@ function SuggestedEventsWidget(inProps) {
58
60
  const scRoutingContext = (0, react_core_1.useSCRouting)();
59
61
  //HOOKS
60
62
  const theme = (0, material_1.useTheme)();
63
+ // REFS
64
+ const updatesSubscription = (0, react_1.useRef)(null);
61
65
  (0, react_1.useEffect)(() => {
62
66
  api_services_1.SuggestionService.getEventSuggestion(Object.assign({}, endpointQueryParams))
63
67
  .then((payload) => {
@@ -106,6 +110,28 @@ function SuggestedEventsWidget(inProps) {
106
110
  }
107
111
  }
108
112
  }, [count, hideMarginLeft, hideMarginRight]);
113
+ /**
114
+ * Subscriber for pubsub callback
115
+ */
116
+ const onDeleteEventHandler = (0, react_1.useCallback)((_msg, deleted) => {
117
+ setEvents((prev) => {
118
+ if (prev.some((e) => e.id === deleted)) {
119
+ return prev.filter((e) => e.id !== deleted);
120
+ }
121
+ return prev;
122
+ });
123
+ }, [events]);
124
+ /**
125
+ * On mount, subscribe to receive event updates (only delete)
126
+ */
127
+ (0, react_1.useEffect)(() => {
128
+ if (events) {
129
+ updatesSubscription.current = pubsub_js_1.default.subscribe(`${PubSub_1.SCTopicType.EVENT}.${PubSub_1.SCGroupEventType.DELETE}`, onDeleteEventHandler);
130
+ }
131
+ return () => {
132
+ updatesSubscription.current && pubsub_js_1.default.unsubscribe(updatesSubscription.current);
133
+ };
134
+ }, [events]);
109
135
  // RENDER
110
136
  if (!events && loading) {
111
137
  return (0, jsx_runtime_1.jsx)(Skeleton_1.default, {});
@@ -151,7 +151,12 @@ export default function Events(inProps) {
151
151
  * Subscriber for pubsub callback
152
152
  */
153
153
  const onDeleteEventHandler = useCallback((_msg, deleted) => {
154
- setEvents((prev) => prev.filter((e) => e.id !== deleted));
154
+ setEvents((prev) => {
155
+ if (prev.some((e) => e.id === deleted)) {
156
+ return prev.filter((e) => e.id !== deleted);
157
+ }
158
+ return prev;
159
+ });
155
160
  }, [events]);
156
161
  /**
157
162
  * On mount, subscribe to receive event updates (only delete)
@@ -7,7 +7,7 @@ import { Endpoints, EventService, http } from '@selfcommunity/api-services';
7
7
  import { SCCache, SCRoutes, useSCPreferences, useSCRouting, useSCUser } from '@selfcommunity/react-core';
8
8
  import { SCEventSubscriptionStatusType, SCFeatureName } from '@selfcommunity/types';
9
9
  import { Logger } from '@selfcommunity/utils';
10
- import { useCallback, useEffect, useMemo, useReducer, useState } from 'react';
10
+ import { useCallback, useEffect, useMemo, useReducer, useRef, useState } from 'react';
11
11
  import { FormattedMessage } from 'react-intl';
12
12
  import { SCOPE_SC_UI } from '../../constants/Errors';
13
13
  import { DEFAULT_PAGINATION_LIMIT, DEFAULT_PAGINATION_OFFSET } from '../../constants/Pagination';
@@ -18,6 +18,8 @@ import Event from '../Event';
18
18
  import Widget from '../Widget';
19
19
  import { PREFIX } from './constants';
20
20
  import Skeleton from './Skeleton';
21
+ import PubSub from 'pubsub-js';
22
+ import { SCGroupEventType, SCTopicType } from '../../constants/PubSub';
21
23
  const classes = {
22
24
  root: `${PREFIX}-root`,
23
25
  titleWrapper: `${PREFIX}-title-wrapper`,
@@ -64,6 +66,8 @@ export default function MyEventsWidget(inProps) {
64
66
  const scRoutingContext = useSCRouting();
65
67
  const { features } = useSCPreferences();
66
68
  const eventsEnabled = useMemo(() => features && features.includes(SCFeatureName.EVENT) && features.includes(SCFeatureName.TAGGING), [features]);
69
+ // REFS
70
+ const updatesSubscription = useRef(null);
67
71
  /**
68
72
  * Initialize component
69
73
  * Fetch data only if the component is not initialized and it is not loading data
@@ -118,6 +122,30 @@ export default function MyEventsWidget(inProps) {
118
122
  _fetchNext();
119
123
  }
120
124
  }, [eventIndex, state.results]);
125
+ /**
126
+ * Subscriber for pubsub callback
127
+ */
128
+ const onDeleteEventHandler = useCallback((_msg, deleted) => {
129
+ const _events = [...state.results];
130
+ if (_events.some((e) => e.id === deleted)) {
131
+ const updatedEvents = _events.filter((e) => e.id !== deleted);
132
+ dispatch({
133
+ type: actionWidgetTypes.SET_RESULTS,
134
+ payload: { results: updatedEvents }
135
+ });
136
+ }
137
+ }, [state.results]);
138
+ /**
139
+ * On mount, subscribe to receive event updates (only delete)
140
+ */
141
+ useEffect(() => {
142
+ if (state.results) {
143
+ updatesSubscription.current = PubSub.subscribe(`${SCTopicType.EVENT}.${SCGroupEventType.DELETE}`, onDeleteEventHandler);
144
+ }
145
+ return () => {
146
+ updatesSubscription.current && PubSub.unsubscribe(updatesSubscription.current);
147
+ };
148
+ }, [state.results]);
121
149
  // RENDER
122
150
  if (!eventsEnabled || (state.initialized && state.count === 0)) {
123
151
  return _jsx(HiddenPlaceholder, {});
@@ -6,7 +6,7 @@ import { Endpoints, http, SuggestionService } from '@selfcommunity/api-services'
6
6
  import { Link, SCRoutes, useSCRouting } from '@selfcommunity/react-core';
7
7
  import { Logger } from '@selfcommunity/utils';
8
8
  import classNames from 'classnames';
9
- import { useCallback, useEffect, useState } from 'react';
9
+ import { useCallback, useEffect, useRef, useState } from 'react';
10
10
  import { FormattedMessage } from 'react-intl';
11
11
  import { Swiper, SwiperSlide } from 'swiper/react';
12
12
  import { SCOPE_SC_UI } from '../../constants/Errors';
@@ -18,6 +18,8 @@ import Widget from '../Widget';
18
18
  import Arrow from './Arrow';
19
19
  import { PREFIX } from './constants';
20
20
  import Skeleton from './Skeleton';
21
+ import PubSub from 'pubsub-js';
22
+ import { SCGroupEventType, SCTopicType } from '../../constants/PubSub';
21
23
  const classes = {
22
24
  root: `${PREFIX}-root`,
23
25
  content: `${PREFIX}-content`,
@@ -56,6 +58,8 @@ export default function SuggestedEventsWidget(inProps) {
56
58
  const scRoutingContext = useSCRouting();
57
59
  //HOOKS
58
60
  const theme = useTheme();
61
+ // REFS
62
+ const updatesSubscription = useRef(null);
59
63
  useEffect(() => {
60
64
  SuggestionService.getEventSuggestion(Object.assign({}, endpointQueryParams))
61
65
  .then((payload) => {
@@ -104,6 +108,28 @@ export default function SuggestedEventsWidget(inProps) {
104
108
  }
105
109
  }
106
110
  }, [count, hideMarginLeft, hideMarginRight]);
111
+ /**
112
+ * Subscriber for pubsub callback
113
+ */
114
+ const onDeleteEventHandler = useCallback((_msg, deleted) => {
115
+ setEvents((prev) => {
116
+ if (prev.some((e) => e.id === deleted)) {
117
+ return prev.filter((e) => e.id !== deleted);
118
+ }
119
+ return prev;
120
+ });
121
+ }, [events]);
122
+ /**
123
+ * On mount, subscribe to receive event updates (only delete)
124
+ */
125
+ useEffect(() => {
126
+ if (events) {
127
+ updatesSubscription.current = PubSub.subscribe(`${SCTopicType.EVENT}.${SCGroupEventType.DELETE}`, onDeleteEventHandler);
128
+ }
129
+ return () => {
130
+ updatesSubscription.current && PubSub.unsubscribe(updatesSubscription.current);
131
+ };
132
+ }, [events]);
107
133
  // RENDER
108
134
  if (!events && loading) {
109
135
  return _jsx(Skeleton, {});