Преглед изворни кода

Merge pull request #4555 from weseek/feat/page-migration-button-interaction

feat: page migration button interaction
Haku Mizuki пре 4 година
родитељ
комит
cceca31faa

+ 6 - 6
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 });
   }
 
@@ -453,12 +453,12 @@ export default class AdminAppContainer extends Container {
   /**
    * Start v5 page migration
    * @memberOf AdminAppContainer
-   * @property action takes either 'notNow' or 'upgrade'. 'upgrade' will start or resume migration
+   * @property action takes only 'upgrade' for now. '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) {
+    const response = await this.appContainer.apiv3.post('/pages/v5-schema-migration', { action });
+    const { isV5Compatible } = response.data;
+    return { isV5Compatible };
   }
 
 }

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

@@ -13,7 +13,8 @@ const V5PageMigration: FC<any> = (props) => {
   const { t } = useTranslation();
 
   const onConfirm = async() => {
-    await adminAppContainer.startV5PageMigrationHandler();
+    setIsV5PageMigrationModalShown(false);
+    await adminAppContainer.v5PageMigrationHandler('upgrade');
   };
 
   return (
@@ -34,10 +35,6 @@ const V5PageMigration: FC<any> = (props) => {
       </p>
       <div className="row my-3">
         <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-warning" onClick={() => setIsV5PageMigrationModalShown(true)}>Upgrade to v5</button>
         </div>
       </div>

+ 1 - 1
packages/app/src/components/Admin/App/V5PageMigrationModal.tsx

@@ -6,7 +6,7 @@ import { useTranslation } from 'react-i18next';
 
 type V5PageMigrationModalProps = {
   isModalOpen: boolean
-  onConfirm?: () => Promise<any>;
+  onConfirm?: () => Promise<void>;
   onCancel?: () => void;
 };
 

+ 24 - 11
packages/app/src/server/routes/apiv3/pages.js

@@ -184,6 +184,9 @@ module.exports = (crowi) => {
       body('pageNameInput').trim().isLength({ min: 1 }).withMessage('pageNameInput is required'),
       body('isRecursively').if(value => value != null).isBoolean().withMessage('isRecursively must be boolean'),
     ],
+    v5PageMigration: [
+      body('action').isString().withMessage('action is required'),
+    ],
   };
 
   async function createPageAction({
@@ -681,20 +684,30 @@ module.exports = (crowi) => {
 
   });
 
-  // TODO: handle 'notNow' and 'upgrade' to either set config to false or start/resume migration 80202
   // 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);
+  router.post('/v5-schema-migration', accessTokenParser, loginRequired, adminRequired, csrf, validator.v5PageMigration, apiV3FormValidator, async(req, res) => {
+    const { action } = req.body;
+
+    switch (action) {
+      case 'upgrade':
+        try {
+          const Page = crowi.model('Page');
+          // not await
+          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.`);
+        return res.apiv3Err(new ErrorV3('This action is not supported.', 'not_supported'), 400);
     }
 
-    return res.apiv3({});
+    const isV5Compatible = crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
+    return res.apiv3({ isV5Compatible });
   });
 
   return router;

+ 7 - 2
packages/app/src/server/service/page.js

@@ -740,7 +740,7 @@ class PageService {
 
   async v5RecursiveMigration(grant, rootPath = null) {
     const BATCH_SIZE = 100;
-    const PAGES_LIMIT = 3000;
+    const PAGES_LIMIT = 1000;
     const Page = this.crowi.model('Page');
     const { PageQueryBuilder } = Page;
 
@@ -843,8 +843,13 @@ class PageService {
 
     await streamToPromise(migratePagesStream);
     if (await Page.exists({ grant, parent: null, path: { $ne: '/' } })) {
-      await this.v5RecursiveMigration(grant, rootPath);
+      return this.v5RecursiveMigration(grant, rootPath);
     }
+
+    logger.info('Successfully migrated all public pages.');
+    await this.crowi.configManager.updateConfigsInTheSameNamespace('crowi', {
+      'app:isV5Compatible': true,
+    });
   }
 
 }