Ver Fonte

Merge pull request #1333 from weseek/create-apiV3-update-layout

Create api v3 update layout
Yuki Takei há 6 anos atrás
pai
commit
e6e5a47e87

+ 1 - 1
src/client/js/components/Admin/Customize/CustomizeLayoutSetting.jsx

@@ -28,7 +28,7 @@ class CustomizeLayoutSetting extends React.Component {
     const { t, adminCustomizeContainer } = this.props;
     const { t, adminCustomizeContainer } = this.props;
 
 
     try {
     try {
-      await adminCustomizeContainer.updateCustomizeLayout();
+      await adminCustomizeContainer.updateCustomizeLayoutAndTheme();
       toastSuccess(t('customize_page.update_layout_success'));
       toastSuccess(t('customize_page.update_layout_success'));
     }
     }
     catch (err) {
     catch (err) {

+ 12 - 2
src/client/js/services/AdminCustomizeContainer.js

@@ -44,8 +44,18 @@ export default class AdminCustomizeContainer extends Container {
     this.setState({ currentTheme: themeName });
     this.setState({ currentTheme: themeName });
   }
   }
 
 
-  updateCustomizeLayout() {
-    // TODO GW-479 post api
+  /**
+   * Update layout
+   * @memberOf AdminCustomizeContainer
+   * @return {Array} Appearance
+   */
+  async updateCustomizeLayoutAndTheme() {
+    const response = await this.appContainer.apiv3.put('/customize-setting/layoutTheme', {
+      layoutType: this.state.currentLayout,
+      themeType: this.state.currentTheme,
+    });
+    const { customizedParams } = response.data;
+    return customizedParams;
   }
   }
 
 
 }
 }

+ 79 - 0
src/server/routes/apiv3/customize-setting.js

@@ -0,0 +1,79 @@
+/* eslint-disable no-unused-vars */
+const loggerFactory = require('@alias/logger');
+
+const logger = loggerFactory('growi:routes:apiv3:user-group');
+
+const express = require('express');
+
+const router = express.Router();
+
+const { body } = require('express-validator/check');
+const ErrorV3 = require('../../models/vo/error-apiv3');
+
+const validator = {};
+
+/**
+ * @swagger
+ *  tags:
+ *    name: CustomizeSetting
+ */
+
+module.exports = (crowi) => {
+  const loginRequiredStrictly = require('../../middleware/login-required')(crowi);
+  const adminRequired = require('../../middleware/admin-required')(crowi);
+  const csrf = require('../../middleware/csrf')(crowi);
+
+  const { ApiV3FormValidator } = crowi.middlewares;
+
+  validator.layoutTheme = [
+    body('layoutType').isString(),
+    body('themeType').isString(),
+  ];
+
+  /**
+   * @swagger
+   *
+   *    /customize-setting/layoutTheme:
+   *      put:
+   *        tags: [CustomizeSetting]
+   *        description: Update layout and theme
+   *        requestBody:
+   *          required: true
+   *          content:
+   *            application/json:
+   *              schama:
+   *                type: object
+   *                properties:
+   *                  layoutType:
+   *                    description: type of layout
+   *                    type: string
+   *                  themeType:
+   *                    description: type of theme
+   *                    type: string
+   *      responses:
+   *          200:
+   *            description: Succeeded to update layout and theme
+   */
+  router.put('/layoutTheme', loginRequiredStrictly, adminRequired, csrf, validator.layoutTheme, ApiV3FormValidator, async(req, res) => {
+    const requestParams = {
+      'customize:layout': req.body.layoutType,
+      'customize:theme': req.body.themeType,
+    };
+
+    try {
+      await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
+      const customizedParams = {
+        layoutType: await crowi.configManager.getConfig('crowi', 'customize:layout'),
+        themeType: await crowi.configManager.getConfig('crowi', 'customize:theme'),
+      };
+      return res.apiv3({ customizedParams });
+    }
+    catch (err) {
+      const msg = 'Error occurred in updating layout and theme';
+      logger.error('Error', err);
+      return res.apiv3Err(new ErrorV3(msg, 'update-layoutTheme-failed'));
+    }
+  });
+
+  return router;
+};

+ 2 - 0
src/server/routes/apiv3/index.js

@@ -15,6 +15,8 @@ module.exports = (crowi) => {
 
 
   router.use('/markdown-setting', require('./markdown-setting')(crowi));
   router.use('/markdown-setting', require('./markdown-setting')(crowi));
 
 
+  router.use('/customize-setting', require('./customize-setting')(crowi));
+
   router.use('/users', require('./users')(crowi));
   router.use('/users', require('./users')(crowi));
 
 
   router.use('/user-groups', require('./user-group')(crowi));
   router.use('/user-groups', require('./user-group')(crowi));