Browse Source

Merge pull request #4461 from weseek/imprv/#78557-fetch-notification-list-new

hit an api in-app-notificaton-list
Yuki Takei 4 years ago
parent
commit
107ee66303

+ 10 - 7
packages/app/src/components/InAppNotification/InAppNotificationDropdown.tsx

@@ -3,6 +3,7 @@ import {
   Dropdown, DropdownToggle, DropdownMenu, DropdownItem,
 } from 'reactstrap';
 import PropTypes from 'prop-types';
+import AppContainer from '~/client/services/AppContainer';
 import { withUnstatedContainers } from '../UnstatedUtils';
 import { InAppNotification as IInAppNotification } from '../../interfaces/in-app-notification';
 // import DropdownMenu from './InAppNotificationDropdown/DropdownMenu';
@@ -17,7 +18,7 @@ const InAppNotificationDropdown: FC = (props) => {
   const [count, setCount] = useState(0);
   const [isLoaded, setIsLoaded] = useState(false);
   const [notifications, setNotifications] = useState<IInAppNotification[]>([{
-    // This is dummy notification data. Delete it after fetching notification list by #78557
+    // This is dummy notification data. Delete it after fetching notification list by #78756
     _id: '1',
     user: 'kaori1',
     targetModel: 'Page',
@@ -31,7 +32,7 @@ const InAppNotificationDropdown: FC = (props) => {
 
   useEffect(() => {
     initializeSocket(props);
-    // fetchNotificationList();
+    fetchNotificationList(props);
     // fetchNotificationStatus();
   }, []);
 
@@ -84,13 +85,13 @@ const InAppNotificationDropdown: FC = (props) => {
     * TODO: Fetch notification list by GW-7473
     */
 
-  const fetchNotificationList = async() => {
+  const fetchNotificationList = async(props) => {
+    console.log('propsappContainerHoge', props.appContainer);
     const limit = 6;
     try {
-      // const { notifications } = await this.props.crowi.apiGet('/notification.list', { limit });
-      setIsLoaded(true);
+      const notifications = await props.appContainer.apiv3Get('/in-app-notification/list', { limit });
       // setNotifications(notifications);
-      // this.setState({ loaded: true, notifications });
+      // setIsLoaded(true);
     }
     catch (err) {
       // TODO: error handling
@@ -138,6 +139,7 @@ const InAppNotificationDropdown: FC = (props) => {
   // TODO: improve renderInAppNotificationList by GW-7535
   // refer to https://github.com/crowi/crowi/blob/eecf2bc821098d2516b58104fe88fae81497d3ea/client/components/Notification/Notification.tsx
   const RenderInAppNotificationList = () => {
+    console.log('notificationsHoge', notifications);
 
 
     if (notifications.length === 0) {
@@ -177,10 +179,11 @@ const InAppNotificationDropdown: FC = (props) => {
 /**
  * Wrapper component for using unstated
  */
-const InAppNotificationDropdownWrapper = withUnstatedContainers(InAppNotificationDropdown, [SocketIoContainer]);
+const InAppNotificationDropdownWrapper = withUnstatedContainers(InAppNotificationDropdown, [AppContainer, SocketIoContainer]);
 
 InAppNotificationDropdown.propTypes = {
   me: PropTypes.string,
+  appContainer: PropTypes.instanceOf(AppContainer).isRequired,
   socketIoContainer: PropTypes.instanceOf(SocketIoContainer).isRequired,
 };
 

+ 10 - 9
packages/app/src/server/models/in-app-notification.ts

@@ -84,17 +84,18 @@ inAppNotificationSchema.index({
   user: 1, target: 1, action: 1, createdAt: 1,
 });
 
-inAppNotificationSchema.statics.findLatestInAppNotificationsByUser = function(user, limitNum, offset) {
+inAppNotificationSchema.statics.findLatestInAppNotificationsByUser = async function(user, limitNum, offset) {
   const limit = limitNum || 10;
 
-  // TODO: improve populate refer to GROWI way by GW-7482
-  return InAppNotification.find({ user })
-    .sort({ createdAt: -1 })
-    .skip(offset)
-    .limit(limit)
-    .populate(['user', 'target'])
-    .populate({ path: 'activities', populate: { path: 'user' } })
-    .exec();
+  // TODO: improve populate refer to GROWI way by #78756
+  const notificatins = await InAppNotification.find({ user });
+  // .sort({ createdAt: -1 })
+  // .skip(offset)
+  // .limit(limit)
+  // .populate(['user', 'target'])
+  // .populate({ path: 'activities', populate: { path: 'user' } })
+  // .exec();
+  return notificatins;
 };
 
 inAppNotificationSchema.statics.STATUS_UNOPENED = function() {