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

Merge branch 'support/share-link-for-outside-for-merge' into support/call-api-for-delete-share-link

itizawa 5 лет назад
Родитель
Сommit
fef73b357e

+ 2 - 1
resource/locales/en-US/translation.json

@@ -344,7 +344,8 @@
     "deactivate_user_success": "Succeeded to deactivate {{username}}",
     "remove_user_success": "Succeeded to removing {{username}}",
     "remove_external_user_success": "Succeeded to remove {{accountId}}",
-    "remove_share_link_success": "Succeeded to remove {{shareLinkId}}"
+    "remove_share_link_success": "Succeeded to remove {{shareLinkId}}",
+    "issue_share_link": "Succeeded to issue new share link"
   },
   "template": {
     "modal_label": {

+ 2 - 1
resource/locales/ja/translation.json

@@ -345,7 +345,8 @@
     "deactivate_user_success": "{{username}}を無効化しました",
     "remove_user_success": "{{username}}を削除しました",
     "remove_external_user_success": "{{accountId}}を削除しました",
-    "remove_share_link_success": "{{shareLinkId}}を削除しました"
+    "remove_share_link_success": "{{shareLinkId}}を削除しました",
+    "issue_share_link": "共有リンクを作成しました"
   },
   "template": {
     "modal_label": {

+ 1 - 1
src/client/js/components/OutsideShareLinkModal.jsx

@@ -42,7 +42,7 @@ const OutsideShareLinkModal = (props) => {
             >
               {isOpenShareLinkForm ? 'Close' : 'New'}
             </button>
-            {isOpenShareLinkForm && <ShareLinkForm />}
+            {isOpenShareLinkForm && <ShareLinkForm onCloseForm={toggleShareLinkFormHandler} />}
           </div>
         </div>
       </ModalBody>

+ 69 - 4
src/client/js/components/ShareLinkForm.jsx

@@ -1,10 +1,14 @@
 import React from 'react';
+import PropTypes from 'prop-types';
 
 import { withTranslation } from 'react-i18next';
 import dateFnsFormat from 'date-fns/format';
+import parse from 'date-fns/parse';
 
 import { withUnstatedContainers } from './UnstatedUtils';
 
+import { toastSuccess, toastError } from '../util/apiNotification';
+
 import AppContainer from '../services/AppContainer';
 import PageContainer from '../services/PageContainer';
 
@@ -66,10 +70,63 @@ class ShareLinkForm extends React.Component {
     this.setState({ customExpirationTime });
   }
 
-  handleIssueShareLink() {
-    // use these options
-    console.log(this.state);
-    console.log('発行する!');
+  /**
+   * Generate expiredAt by expirationType
+   */
+  generateExpired() {
+    const { expirationType } = this.state;
+    let expiredAt;
+
+    if (expirationType === 'unlimited') {
+      return null;
+    }
+
+    if (expirationType === 'numberOfDays') {
+      const date = new Date();
+      date.setDate(date.getDate() + this.state.numberOfDays);
+      expiredAt = date;
+    }
+
+    if (expirationType === 'custom') {
+      const { customExpirationDate, customExpirationTime } = this.state;
+      expiredAt = parse(`${customExpirationDate}T${customExpirationTime}`, "yyyy-MM-dd'T'HH:mm:ss", new Date());
+    }
+
+    return expiredAt;
+  }
+
+  closeForm() {
+    const { onCloseForm } = this.props;
+
+    if (onCloseForm == null) {
+      return;
+    }
+    onCloseForm();
+  }
+
+  async handleIssueShareLink() {
+    const { t, pageContainer } = this.props;
+    const { pageId } = pageContainer.state;
+    const { description } = this.state;
+
+    let expiredAt;
+
+    try {
+      expiredAt = this.generateExpired();
+    }
+    catch (err) {
+      return toastError(err);
+    }
+
+    try {
+      await this.props.appContainer.apiv3.post('/share-links/', { relatedPage: pageId, expiredAt, description });
+      this.closeForm();
+      toastSuccess(t('toaster.issue_share_link'));
+    }
+    catch (err) {
+      toastError(err);
+    }
+
   }
 
   renderExpirationTypeOptions() {
@@ -188,5 +245,13 @@ class ShareLinkForm extends React.Component {
 
 const ShareLinkFormWrapper = withUnstatedContainers(ShareLinkForm, [AppContainer, PageContainer]);
 
+ShareLinkForm.propTypes = {
+  t: PropTypes.func.isRequired, // i18next
+  appContainer: PropTypes.instanceOf(AppContainer).isRequired,
+  pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
+
+  user: PropTypes.object.isRequired,
+  onCloseForm: PropTypes.func,
+};
 
 export default withTranslation()(ShareLinkFormWrapper);

+ 1 - 4
src/server/routes/apiv3/share-links.js

@@ -64,13 +64,10 @@ module.exports = (crowi) => {
   validator.shareLinkStatus = [
     // validate the page id is null
     body('relatedPage').not().isEmpty().withMessage('Page Id is null'),
-
     // validate expireation date is not empty, is not before today and is date.
-    body('expiredAt').isAfter(today.toString()).withMessage('Your Selected date is past'),
-
+    body('expiredAt').if(value => value != null).isAfter(today.toString()).withMessage('Your Selected date is past'),
     // validate the length of description is max 100.
     body('description').isLength({ min: 0, max: 100 }).withMessage('Max length is 100'),
-
   ];
 
   /**