2024-05-01 19:38:12 +08:00
|
|
|
import React, { useEffect, useRef, useState } from 'react';
|
2024-02-23 23:01:53 +08:00
|
|
|
import Users from '/imports/api/users';
|
|
|
|
import logger from '/imports/startup/client/logger';
|
2024-05-01 19:38:12 +08:00
|
|
|
import { AdapterProps } from '../graphqlToMiniMongoAdapterManager/component';
|
2024-06-04 21:40:54 +08:00
|
|
|
import useCurrentUser from '/imports/ui/core/hooks/useCurrentUser';
|
2024-02-23 23:01:53 +08:00
|
|
|
|
2024-05-01 19:38:12 +08:00
|
|
|
const UserGrapQlMiniMongoAdapter: React.FC<AdapterProps> = ({
|
|
|
|
onReady,
|
|
|
|
children,
|
|
|
|
}) => {
|
|
|
|
const ready = useRef(false);
|
2024-02-23 23:01:53 +08:00
|
|
|
const {
|
|
|
|
data,
|
2024-06-04 21:40:54 +08:00
|
|
|
errors: error,
|
|
|
|
} = useCurrentUser((u) => u);
|
2024-02-23 23:01:53 +08:00
|
|
|
const [userDataSetted, setUserDataSetted] = useState(false);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (error) {
|
|
|
|
logger.error('Error in UserGrapQlMiniMongoAdapter', error);
|
|
|
|
}
|
|
|
|
}, [error]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-06-04 21:40:54 +08:00
|
|
|
if (data) {
|
2024-05-01 19:38:12 +08:00
|
|
|
if (!ready.current) {
|
|
|
|
ready.current = true;
|
|
|
|
onReady('UserGrapQlMiniMongoAdapter');
|
|
|
|
}
|
2024-06-04 21:40:54 +08:00
|
|
|
const { userId } = data;
|
|
|
|
Users.upsert({ userId }, data);
|
2024-02-23 23:01:53 +08:00
|
|
|
if (!userDataSetted) {
|
|
|
|
setUserDataSetted(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, [data]);
|
2024-05-01 19:38:12 +08:00
|
|
|
return children;
|
2024-02-23 23:01:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
export default UserGrapQlMiniMongoAdapter;
|