itizawa 6 лет назад
Родитель
Сommit
d30c4c5227

+ 3 - 8
src/client/js/components/Me/BasicInfoSettings.jsx

@@ -3,16 +3,12 @@ import React, { Fragment } from 'react';
 import PropTypes from 'prop-types';
 import PropTypes from 'prop-types';
 import { withTranslation } from 'react-i18next';
 import { withTranslation } from 'react-i18next';
 
 
-import loggerFactory from '@alias/logger';
-
 import { toastSuccess, toastError } from '../../util/apiNotification';
 import { toastSuccess, toastError } from '../../util/apiNotification';
 import { createSubscribedElement } from '../UnstatedUtils';
 import { createSubscribedElement } from '../UnstatedUtils';
 
 
 import AppContainer from '../../services/AppContainer';
 import AppContainer from '../../services/AppContainer';
 import PersonalContainer from '../../services/PersonalContainer';
 import PersonalContainer from '../../services/PersonalContainer';
 
 
-const logger = loggerFactory('growi:basicInfoSettings');
-
 class BasicInfoSettings extends React.Component {
 class BasicInfoSettings extends React.Component {
 
 
   constructor(appContainer) {
   constructor(appContainer) {
@@ -23,7 +19,7 @@ class BasicInfoSettings extends React.Component {
 
 
   async componentDidMount() {
   async componentDidMount() {
     try {
     try {
-      await this.props.personalContainer.retrieveExternalAccounts();
+      await this.props.personalContainer.retrievePersonalData();
     }
     }
     catch (err) {
     catch (err) {
       toastError(err);
       toastError(err);
@@ -39,7 +35,6 @@ class BasicInfoSettings extends React.Component {
     }
     }
     catch (err) {
     catch (err) {
       toastError(err);
       toastError(err);
-      logger.error(err);
     }
     }
   }
   }
 
 
@@ -88,7 +83,7 @@ class BasicInfoSettings extends React.Component {
 
 
         <div className="row mb-3">
         <div className="row mb-3">
           <label className="col-xs-2 text-right">{t('Disclose E-mail')}</label>
           <label className="col-xs-2 text-right">{t('Disclose E-mail')}</label>
-          <div className="col-xs-4">
+          <div className="col-xs-6">
             <div className="radio radio-primary radio-inline">
             <div className="radio radio-primary radio-inline">
               <input
               <input
                 type="radio"
                 type="radio"
@@ -114,7 +109,7 @@ class BasicInfoSettings extends React.Component {
 
 
         <div className="row mb-3">
         <div className="row mb-3">
           <label className="col-xs-2 text-right">{t('Language')}</label>
           <label className="col-xs-2 text-right">{t('Language')}</label>
-          <div className="col-xs-4">
+          <div className="col-xs-6">
             <div className="radio radio-primary radio-inline">
             <div className="radio radio-primary radio-inline">
               <input
               <input
                 type="radio"
                 type="radio"

+ 23 - 1
src/client/js/services/PersonalContainer.js

@@ -108,7 +108,29 @@ export default class PersonalContainer extends Container {
    * @return {Array} basic info
    * @return {Array} basic info
    */
    */
   async updateBasicInfo() {
   async updateBasicInfo() {
-    // TODO GW-1036 create apiV3
+    try {
+      const response = await this.appContainer.apiv3.put('/personal-setting/', {
+        name: this.state.name,
+        email: this.state.email,
+        isEmailPublished: this.state.isEmailPublished,
+        lang: this.state.lang,
+      });
+      const { updatedUser } = response.data;
+
+      console.log(updatedUser.lang);
+
+      this.setState({
+        name: updatedUser.name,
+        email: updatedUser.email,
+        isEmailPublished: updatedUser.isEmailPublished,
+        lang: updatedUser.lang,
+      });
+    }
+    catch (err) {
+      this.setState({ retrieveError: err });
+      logger.error(err);
+      throw new Error('Failed to update personal data');
+    }
   }
   }
 
 
 }
 }

+ 24 - 2
src/server/routes/apiv3/personal-setting.js

@@ -17,9 +17,7 @@ const ErrorV3 = require('../../models/vo/error-apiv3');
  */
  */
 module.exports = (crowi) => {
 module.exports = (crowi) => {
   const loginRequiredStrictly = require('../../middleware/login-required')(crowi);
   const loginRequiredStrictly = require('../../middleware/login-required')(crowi);
-  const adminRequired = require('../../middleware/admin-required')(crowi);
   const csrf = require('../../middleware/csrf')(crowi);
   const csrf = require('../../middleware/csrf')(crowi);
-  const { customizeService } = crowi;
 
 
   const { User, ExternalAccount } = crowi.models;
   const { User, ExternalAccount } = crowi.models;
 
 
@@ -51,6 +49,30 @@ module.exports = (crowi) => {
     return res.apiv3({ currentUser });
     return res.apiv3({ currentUser });
   });
   });
 
 
+  // TODO swagger & validation
+  router.put('/', loginRequiredStrictly, csrf, async(req, res) => {
+    const {
+      name, email, lang, isEmailPublished,
+    } = req.body;
+
+    try {
+      const user = await User.findOne({ email });
+      user.name = name;
+      user.email = email;
+      user.lang = lang;
+      user.isEmailPublished = isEmailPublished;
+
+      const updatedUser = await user.save();
+      req.i18n.changeLanguage(lang);
+      return res.apiv3({ updatedUser });
+    }
+    catch (err) {
+      logger.error(err);
+      return res.apiv3Err('update-personal-settings-failed');
+    }
+
+  });
+
   /**
   /**
    * @swagger
    * @swagger
    *
    *

+ 1 - 52
src/server/routes/me.js

@@ -104,58 +104,7 @@ module.exports = function(crowi, app) {
   };
   };
 
 
   actions.index = function(req, res) {
   actions.index = function(req, res) {
-    const userForm = req.body.userForm;
-    const userData = req.user;
-
-    if (req.method === 'POST' && req.form.isValid) {
-      const name = userForm.name;
-      const email = userForm.email;
-      const lang = userForm.lang;
-      const isEmailPublished = userForm.isEmailPublished;
-
-      /*
-       * disabled because the system no longer allows undefined email -- 2017.10.06 Yuki Takei
-       *
-      if (!User.isEmailValid(email)) {
-        req.form.errors.push('You can\'t update to that email address');
-        return res.render('me/index', {});
-      }
-      */
-
-      User.findOneAndUpdate(
-        /* eslint-disable object-curly-newline */
-        { email: userData.email }, //                   query
-        { name, email, lang, isEmailPublished }, //     updating data
-        { runValidators: true, context: 'query' }, //   for validation
-        // see https://www.npmjs.com/package/mongoose-unique-validator#find--updates -- 2017.09.24 Yuki Takei
-        /* eslint-enable object-curly-newline */
-        (err) => {
-          if (err) {
-            Object.keys(err.errors).forEach((e) => {
-              req.form.errors.push(err.errors[e].message);
-            });
-
-            return res.render('me/index', {});
-          }
-          req.i18n.changeLanguage(lang);
-          req.flash('successMessage', req.t('Updated'));
-          return res.redirect('/me');
-        },
-      );
-    }
-    else { // method GET
-      /*
-       * disabled because the system no longer allows undefined email -- 2017.10.06 Yuki Takei
-       *
-      /// そのうちこのコードはいらなくなるはず
-      if (!userData.isEmailSet()) {
-        req.flash('warningMessage', 'メールアドレスが設定されている必要があります');
-      }
-      */
-
-      return res.render('me/index', {
-      });
-    }
+    return res.render('me/index');
   };
   };
 
 
   actions.imagetype = function(req, res) {
   actions.imagetype = function(req, res) {