Taichi Masuyama 4 лет назад
Родитель
Сommit
c05b22e28d

+ 10 - 5
packages/app/src/client/services/AdminAppContainer.js

@@ -165,7 +165,7 @@ export default class AdminAppContainer extends Container {
   /**
    * Change site url
    */
-  changeisV5Compatible(isV5Compatible) {
+  changeIsV5Compatible(isV5Compatible) {
     this.setState({ isV5Compatible });
   }
 
@@ -455,10 +455,15 @@ export default class AdminAppContainer extends Container {
    * @memberOf AdminAppContainer
    * @property action takes either 'notNow' or 'upgrade'. 'upgrade' will start or resume migration
    */
-  async startV5PageMigrationHandler(/* get action */) {
-    const response = await this.appContainer.apiv3.post('/pages/v5-schema-migration', { /* TODO: pass 'notNow' or 'upgrade' */ });
-    const { ok, status } = response.data;
-    return { ok, status };
+  async v5PageMigrationHandler(action) {
+    // not wait response and immediately set state to give priority to response time
+    if (action === 'notNow') {
+      this.changeIsV5Compatible(false);
+    }
+
+    const response = await this.appContainer.apiv3.post('/pages/v5-schema-migration', { action });
+    const { status } = response.data;
+    return { status };
   }
 
 }

+ 6 - 2
packages/app/src/components/Admin/App/V5PageMigration.tsx

@@ -13,7 +13,11 @@ const V5PageMigration: FC<any> = (props) => {
   const { t } = useTranslation();
 
   const onConfirm = async() => {
-    await adminAppContainer.startV5PageMigrationHandler();
+    await adminAppContainer.v5PageMigrationHandler('upgrade');
+  };
+
+  const onNotNowClicked = async() => {
+    await adminAppContainer.v5PageMigrationHandler('notNow');
   };
 
   return (
@@ -36,7 +40,7 @@ const V5PageMigration: FC<any> = (props) => {
         <div className="mx-auto">
           {
             isV5Compatible == null
-            && (<button type="button" className="btn btn-secondary mr-3" onClick={() => { /* TODO: POST to set false 80202 */ }}>Not now</button>)
+            && (<button type="button" className="btn btn-secondary mr-3" onClick={() => onNotNowClicked()}>Not now</button>)
           }
           <button type="button" className="btn btn-warning" onClick={() => setIsV5PageMigrationModalShown(true)}>Upgrade to v5</button>
         </div>

+ 33 - 10
packages/app/src/server/routes/apiv3/pages.js

@@ -681,20 +681,43 @@ module.exports = (crowi) => {
 
   });
 
-  // TODO: handle 'notNow' and 'upgrade' to either set config to false or start/resume migration 80202
+  // TODO: add validator for the action property
   // TODO: use socket conn to show progress
   router.post('/v5-schema-migration', /* accessTokenParser, loginRequired, adminRequired, csrf, */ async(req, res) => {
-    try {
-      const Page = crowi.model('Page');
-      // TODO: not await but should be dealed as a job
-      crowi.pageService.v5RecursiveMigration(Page.GRANT_PUBLIC);
-    }
-    catch (err) {
-      logger.error('Error\n', err);
-      return res.apiv3Err(new ErrorV3('Failed to migrate pages. Please try again.', 'v5_migration_failed'), 500);
+    const { action } = req.body;
+
+    switch (action) {
+      case 'notNow':
+        // set config to false
+        try {
+          await crowi.configManager.updateConfigsInTheSameNamespace('crowi', {
+            'app:isV5Compatible': false,
+          });
+          return res.apiv3({});
+        }
+        catch (err) {
+          logger.error('Error occurred while updating app:isV5Compatible.', err);
+        }
+        break;
+
+      case 'upgrade':
+        try {
+          const Page = crowi.model('Page');
+          crowi.pageService.v5RecursiveMigration(Page.GRANT_PUBLIC);
+        }
+        catch (err) {
+          logger.error('Error\n', err);
+          return res.apiv3Err(new ErrorV3('Failed to migrate pages. Please try again.', 'v5_migration_failed'), 500);
+        }
+        break;
+
+      default:
+        logger.error(`${action} action is not supported.`);
+        break;
     }
 
-    return res.apiv3({});
+    // TODO: determine what to respond
+    return res.apiv3({ status: 'inProgress' });
   });
 
   return router;