Просмотр исходного кода

Merge pull request #1569 from weseek/reactify-admin/create-apiV3-delete-notification

Reactify admin/create api v3 delete notification
itizawa 6 лет назад
Родитель
Сommit
2fffd97190

+ 3 - 4
src/client/js/components/Admin/Notification/UserNotificationRow.jsx

@@ -8,7 +8,7 @@ import AppContainer from '../../../services/AppContainer';
 import AdminNotificationContainer from '../../../services/AdminNotificationContainer';
 
 
-class UserNotificationRow extends React.Component {
+class UserNotificationRow extends React.PureComponent {
 
   render() {
     const { t, notification } = this.props;
@@ -22,8 +22,7 @@ class UserNotificationRow extends React.Component {
             {notification.channel}
           </td>
           <td>
-            {/* TODO GW-806 create apiV3 for delete notification */}
-            <button type="submit" className="btn btn-default">{t('Delete')}</button>
+            <button type="submit" className="btn btn-default" onClick={() => { this.props.onClickDeleteBtn(notification._id) }}>{t('Delete')}</button>
           </td>
         </tr>
       </React.Fragment>
@@ -44,7 +43,7 @@ UserNotificationRow.propTypes = {
   adminNotificationContainer: PropTypes.instanceOf(AdminNotificationContainer).isRequired,
 
   notification: PropTypes.object.isRequired,
-
+  onClickDeleteBtn: PropTypes.func.isRequired,
 };
 
 export default withTranslation()(UserNotificationRowWrapper);

+ 15 - 1
src/client/js/components/Admin/Notification/UserTriggerNotification.jsx

@@ -27,6 +27,7 @@ class UserTriggerNotification extends React.Component {
     this.changeChannel = this.changeChannel.bind(this);
     this.validateForm = this.validateForm.bind(this);
     this.onClickSubmit = this.onClickSubmit.bind(this);
+    this.onClickDeleteBtn = this.onClickDeleteBtn.bind(this);
 
   }
 
@@ -62,6 +63,19 @@ class UserTriggerNotification extends React.Component {
     }
   }
 
+  async onClickDeleteBtn(notificationIdForDelete) {
+    const { t, adminNotificationContainer } = this.props;
+
+    try {
+      const deletedNotificaton = await adminNotificationContainer.deleteUserTriggerNotificationPattern(notificationIdForDelete);
+      toastSuccess(t('notification_setting.delete_notification_pattern', { path: deletedNotificaton.pathPattern }));
+    }
+    catch (err) {
+      toastError(err);
+      logger.error(err);
+    }
+  }
+
   // TODO GW-788 i18n
   render() {
     const { t, adminNotificationContainer } = this.props;
@@ -111,7 +125,7 @@ class UserTriggerNotification extends React.Component {
               </td>
             </tr>
             {adminNotificationContainer.state.userNotifications.map((notification) => {
-              return <UserNotificationRow notification={notification} />;
+              return <UserNotificationRow notification={notification} onClickDeleteBtn={this.onClickDeleteBtn} />;
             })
             }
           </tbody>

+ 10 - 0
src/client/js/services/AdminNotificationContainer.js

@@ -114,6 +114,16 @@ export default class AdminNotificationContainer extends Container {
     this.setState({ userNotifications: response.data.responseParams.userNotifications });
   }
 
+  /**
+   * Delete user trigger notification pattern
+   */
+  async deleteUserTriggerNotificationPattern(notificatiionId) {
+    const response = await this.appContainer.apiv3.delete(`/notification-setting/user-notification/${notificatiionId}`);
+    const deletedNotificaton = response.data;
+    await this.retrieveNotificationData();
+    return deletedNotificaton;
+  }
+
   /**
    * Delete global notification pattern
    */

+ 55 - 14
src/server/routes/apiv3/notification-setting.js

@@ -103,9 +103,9 @@ module.exports = (crowi) => {
   /**
    * @swagger
    *
-   *    /_api/v3/notification-setting/:
+   *    /notification-setting/:
    *      get:
-   *        tags: [NotificationSetting, apiv3]
+   *        tags: [NotificationSetting]
    *        description: Get notification paramators
    *        responses:
    *          200:
@@ -133,9 +133,9 @@ module.exports = (crowi) => {
   /**
    * @swagger
    *
-   *    /_api/v3/notification-setting/slack-configuration:
+   *    /notification-setting/slack-configuration:
    *      put:
-   *        tags: [NotificationSetting, apiv3]
+   *        tags: [NotificationSetting]
    *        description: Update slack configuration setting
    *        requestBody:
    *          required: true
@@ -180,9 +180,9 @@ module.exports = (crowi) => {
   /**
   * @swagger
   *
-  *    /_api/v3/notification-setting/user-notification:
+  *    /notification-setting/user-notification:
   *      post:
-  *        tags: [NotificationSetting, apiv3]
+  *        tags: [NotificationSetting]
   *        description: add user notification setting
   *        requestBody:
   *          required: true
@@ -227,9 +227,50 @@ module.exports = (crowi) => {
   /**
    * @swagger
    *
-   *    /_api/v3/notification-setting/global-notification:
+   *    /notification-setting/user-notification/{id}:
+   *      delete:
+   *        tags: [NotificationSetting]
+   *        description: delete user trigger notification pattern
+   *        parameters:
+   *          - name: id
+   *            in: path
+   *            required: true
+   *            description: id of user trigger notification
+   *            schema:
+   *              type: string
+   *        responses:
+   *          200:
+   *            description: Succeeded to delete user trigger notification pattern
+   *            content:
+   *              application/json:
+   *                schema:
+   *                  properties:
+   *                    deletedNotificaton:
+   *                      type: object
+   *                      description: deleted notification
+   */
+  router.delete('/user-notification/:id', loginRequiredStrictly, adminRequired, csrf, async(req, res) => {
+    const { id } = req.params;
+
+    try {
+      const deletedNotificaton = await UpdatePost.remove(id);
+      return res.apiv3(deletedNotificaton);
+    }
+    catch (err) {
+      const msg = 'Error occurred in delete user trigger notification';
+      logger.error('Error', err);
+      return res.apiv3Err(new ErrorV3(msg, 'delete-userTriggerNotification-failed'));
+    }
+
+
+  });
+
+  /**
+   * @swagger
+   *
+   *    /notification-setting/global-notification:
    *      post:
-   *        tags: [NotificationSetting, apiv3]
+   *        tags: [NotificationSetting]
    *        description: add global notification
    *        requestBody:
    *          required: true
@@ -283,9 +324,9 @@ module.exports = (crowi) => {
   /**
    * @swagger
    *
-   *    /_api/v3/notification-setting/global-notification/{id}:
+   *    /notification-setting/global-notification/{id}:
    *      put:
-   *        tags: [NotificationSetting, apiv3]
+   *        tags: [NotificationSetting]
    *        description: update global notification
    *        parameters:
    *          - name: id
@@ -363,9 +404,9 @@ module.exports = (crowi) => {
   /**
    * @swagger
    *
-   *    /_api/v3/notification-setting/global-notification/{id}/enabled:
+   *    /notification-setting/global-notification/{id}/enabled:
    *      put:
-   *        tags: [NotificationSetting, apiv3]
+   *        tags: [NotificationSetting]
    *        description: toggle enabled global notification
    *        parameters:
    *          - name: id
@@ -420,9 +461,9 @@ module.exports = (crowi) => {
   /**
   * @swagger
   *
-  *    /_api/v3/notification-setting/global-notification/{id}:
+  *    /notification-setting/global-notification/{id}:
   *      delete:
-  *        tags: [NotificationSetting, apiv3]
+  *        tags: [NotificationSetting]
   *        description: delete global notification pattern
   *        parameters:
   *          - name: id