@selfcommunity/react-ui 0.8.0-alpha.19 → 0.8.0-alpha.20

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.
@@ -18,6 +18,8 @@ const constants_1 = require("./constants");
18
18
  const Group_1 = tslib_1.__importStar(require("../Group"));
19
19
  const Pagination_1 = require("../../constants/Pagination");
20
20
  const InfiniteScroll_1 = tslib_1.__importDefault(require("../../shared/InfiniteScroll"));
21
+ const pubsub_js_1 = tslib_1.__importDefault(require("pubsub-js"));
22
+ const PubSub_1 = require("../../constants/PubSub");
21
23
  const classes = {
22
24
  root: `${constants_1.PREFIX}-root`,
23
25
  filters: `${constants_1.PREFIX}-filter`,
@@ -85,6 +87,8 @@ function Groups(inProps) {
85
87
  scPreferencesContext.preferences[react_core_1.SCPreferences.CONFIGURATIONS_CONTENT_AVAILABILITY].value, [scPreferencesContext.preferences]);
86
88
  // CONST
87
89
  const authUserId = scUserContext.user ? scUserContext.user.id : null;
90
+ // REFS
91
+ const updatesSubscription = (0, react_1.useRef)(null);
88
92
  // HANDLERS
89
93
  const handleScrollUp = () => {
90
94
  window.scrollTo({ left: 0, top: 0, behavior: 'smooth' });
@@ -121,6 +125,28 @@ function Groups(inProps) {
121
125
  fetchGroups();
122
126
  }
123
127
  }, [contentAvailability, authUserId, search]);
128
+ /**
129
+ * Subscriber for pubsub callback
130
+ */
131
+ const onDeleteGroupHandler = (0, react_1.useCallback)((_msg, deleted) => {
132
+ setGroups((prev) => {
133
+ if (prev.some((e) => e.id === deleted)) {
134
+ return prev.filter((e) => e.id !== deleted);
135
+ }
136
+ return prev;
137
+ });
138
+ }, [groups]);
139
+ /**
140
+ * On mount, subscribe to receive event updates (only delete)
141
+ */
142
+ (0, react_1.useEffect)(() => {
143
+ if (groups) {
144
+ updatesSubscription.current = pubsub_js_1.default.subscribe(`${PubSub_1.SCTopicType.GROUP}.${PubSub_1.SCGroupEventType.DELETE}`, onDeleteGroupHandler);
145
+ }
146
+ return () => {
147
+ updatesSubscription.current && pubsub_js_1.default.unsubscribe(updatesSubscription.current);
148
+ };
149
+ }, [groups]);
124
150
  const handleNext = (0, react_1.useMemo)(() => () => {
125
151
  if (!next) {
126
152
  return;
@@ -1,6 +1,6 @@
1
1
  import { __rest } from "tslib";
2
2
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
3
- import { useEffect, useMemo, useState } from 'react';
3
+ import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
4
4
  import { styled } from '@mui/material/styles';
5
5
  import { Box, Button, Grid, TextField, Typography, useMediaQuery, useTheme } from '@mui/material';
6
6
  import { Endpoints, GroupService, http } from '@selfcommunity/api-services';
@@ -16,6 +16,8 @@ import { PREFIX } from './constants';
16
16
  import Group, { GroupSkeleton } from '../Group';
17
17
  import { DEFAULT_PAGINATION_OFFSET } from '../../constants/Pagination';
18
18
  import InfiniteScroll from '../../shared/InfiniteScroll';
19
+ import PubSub from 'pubsub-js';
20
+ import { SCGroupEventType, SCTopicType } from '../../constants/PubSub';
19
21
  const classes = {
20
22
  root: `${PREFIX}-root`,
21
23
  filters: `${PREFIX}-filter`,
@@ -83,6 +85,8 @@ export default function Groups(inProps) {
83
85
  scPreferencesContext.preferences[SCPreferences.CONFIGURATIONS_CONTENT_AVAILABILITY].value, [scPreferencesContext.preferences]);
84
86
  // CONST
85
87
  const authUserId = scUserContext.user ? scUserContext.user.id : null;
88
+ // REFS
89
+ const updatesSubscription = useRef(null);
86
90
  // HANDLERS
87
91
  const handleScrollUp = () => {
88
92
  window.scrollTo({ left: 0, top: 0, behavior: 'smooth' });
@@ -119,6 +123,28 @@ export default function Groups(inProps) {
119
123
  fetchGroups();
120
124
  }
121
125
  }, [contentAvailability, authUserId, search]);
126
+ /**
127
+ * Subscriber for pubsub callback
128
+ */
129
+ const onDeleteGroupHandler = useCallback((_msg, deleted) => {
130
+ setGroups((prev) => {
131
+ if (prev.some((e) => e.id === deleted)) {
132
+ return prev.filter((e) => e.id !== deleted);
133
+ }
134
+ return prev;
135
+ });
136
+ }, [groups]);
137
+ /**
138
+ * On mount, subscribe to receive event updates (only delete)
139
+ */
140
+ useEffect(() => {
141
+ if (groups) {
142
+ updatesSubscription.current = PubSub.subscribe(`${SCTopicType.GROUP}.${SCGroupEventType.DELETE}`, onDeleteGroupHandler);
143
+ }
144
+ return () => {
145
+ updatesSubscription.current && PubSub.unsubscribe(updatesSubscription.current);
146
+ };
147
+ }, [groups]);
122
148
  const handleNext = useMemo(() => () => {
123
149
  if (!next) {
124
150
  return;