2021-01-20 01:06:32 +08:00
|
|
|
import { useContext, useEffect } from 'react';
|
2022-06-10 20:52:00 +08:00
|
|
|
import { CurrentUser } from '/imports/api/users';
|
|
|
|
import Users from '/imports/api/users';
|
2021-03-23 04:44:53 +08:00
|
|
|
import UsersPersistentData from '/imports/api/users-persistent-data';
|
2021-01-20 01:06:32 +08:00
|
|
|
import { UsersContext, ACTIONS } from './context';
|
|
|
|
import ChatLogger from '/imports/ui/components/chat/chat-logger/ChatLogger';
|
|
|
|
|
|
|
|
const Adapter = () => {
|
|
|
|
const usingUsersContext = useContext(UsersContext);
|
|
|
|
const { dispatch } = usingUsersContext;
|
2021-02-18 04:43:41 +08:00
|
|
|
|
2021-10-05 04:01:20 +08:00
|
|
|
useEffect(() => {
|
2021-03-23 04:44:53 +08:00
|
|
|
const usersPersistentDataCursor = UsersPersistentData.find({}, { sort: { timestamp: 1 } });
|
|
|
|
usersPersistentDataCursor.observe({
|
|
|
|
added: (obj) => {
|
2021-10-05 04:01:20 +08:00
|
|
|
ChatLogger.debug('usersAdapter::observe::added_persistent_user', obj);
|
2021-03-23 04:44:53 +08:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.ADDED_USER_PERSISTENT_DATA,
|
|
|
|
value: {
|
|
|
|
user: obj,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
changed: (obj) => {
|
2021-10-05 04:01:20 +08:00
|
|
|
ChatLogger.debug('usersAdapter::observe::changed_persistent_user', obj);
|
2021-03-23 04:44:53 +08:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.CHANGED_USER_PERSISTENT_DATA,
|
|
|
|
value: {
|
|
|
|
user: obj,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
2022-06-10 20:52:00 +08:00
|
|
|
removed: () => {},
|
2021-03-23 04:44:53 +08:00
|
|
|
});
|
|
|
|
}, []);
|
2021-02-18 04:43:41 +08:00
|
|
|
|
2021-01-20 01:06:32 +08:00
|
|
|
useEffect(() => {
|
|
|
|
const usersCursor = Users.find({}, { sort: { timestamp: 1 } });
|
2021-10-05 04:01:20 +08:00
|
|
|
const CurrentUserCursor = CurrentUser.find({});
|
2021-01-20 01:06:32 +08:00
|
|
|
usersCursor.observe({
|
|
|
|
added: (obj) => {
|
2021-10-05 04:01:20 +08:00
|
|
|
ChatLogger.debug('usersAdapter::observe::added', obj);
|
2021-01-20 01:06:32 +08:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.ADDED,
|
|
|
|
value: {
|
|
|
|
user: obj,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
changed: (obj) => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.CHANGED,
|
|
|
|
value: {
|
|
|
|
user: obj,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2021-10-05 04:01:20 +08:00
|
|
|
|
|
|
|
CurrentUserCursor.observe({
|
|
|
|
added: (obj) => {
|
|
|
|
ChatLogger.debug('usersAdapter::observe::current-user::added', obj);
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.ADDED,
|
|
|
|
value: {
|
|
|
|
user: obj,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
2021-01-20 01:06:32 +08:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default Adapter;
|