2024-02-23 23:01:53 +08:00
|
|
|
import { useSubscription } from '@apollo/client';
|
|
|
|
import React, { useEffect, useState } from 'react';
|
|
|
|
import CURRENT_USER_SUBSCRIPTION from '/imports/ui/core/graphql/queries/currentUserSubscription';
|
|
|
|
import { User } from '/imports/ui/Types/user';
|
|
|
|
import Users from '/imports/api/users';
|
|
|
|
import logger from '/imports/startup/client/logger';
|
|
|
|
|
|
|
|
interface UserCurrentResponse {
|
|
|
|
user_current: Array<User>;
|
|
|
|
}
|
|
|
|
|
2024-03-08 21:01:17 +08:00
|
|
|
const UserGrapQlMiniMongoAdapter: React.FC = () => {
|
2024-02-23 23:01:53 +08:00
|
|
|
const {
|
|
|
|
error,
|
|
|
|
data,
|
|
|
|
} = useSubscription<UserCurrentResponse>(CURRENT_USER_SUBSCRIPTION);
|
|
|
|
const [userDataSetted, setUserDataSetted] = useState(false);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (error) {
|
|
|
|
logger.error('Error in UserGrapQlMiniMongoAdapter', error);
|
|
|
|
}
|
|
|
|
}, [error]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (data && data.user_current) {
|
|
|
|
const { userId } = data.user_current[0];
|
|
|
|
Users.upsert({ userId }, data.user_current[0]);
|
|
|
|
if (!userDataSetted) {
|
|
|
|
setUserDataSetted(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [data]);
|
2024-03-08 21:01:17 +08:00
|
|
|
return null;
|
2024-02-23 23:01:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default UserGrapQlMiniMongoAdapter;
|