mizozobu 6 лет назад
Родитель
Сommit
7a56a40027

+ 34 - 3
src/client/js/components/Admin/Import/GrowiImportForm.jsx

@@ -14,6 +14,7 @@ class GrowiImportForm extends React.Component {
     this.initialState = {
       meta: {},
       zipFileName: '',
+      collections: new Set(),
       fileStats: [],
       schema: {
         pages: {},
@@ -26,6 +27,7 @@ class GrowiImportForm extends React.Component {
     this.inputRef = React.createRef();
 
     this.changeFileName = this.changeFileName.bind(this);
+    this.toggleCheckbox = this.toggleCheckbox.bind(this);
     this.uploadZipFile = this.uploadZipFile.bind(this);
     this.import = this.import.bind(this);
     this.validateForm = this.validateForm.bind(this);
@@ -37,6 +39,22 @@ class GrowiImportForm extends React.Component {
     this.setState({ name: e.target.files[0].name });
   }
 
+  toggleCheckbox(e) {
+    const { target } = e;
+    const { name, checked } = target;
+
+    this.setState((prevState) => {
+      const collections = new Set(prevState.collections);
+      if (checked) {
+        collections.add(name);
+      }
+      else {
+        collections.delete(name);
+      }
+      return { collections };
+    });
+  }
+
   async uploadZipFile(e) {
     e.preventDefault();
 
@@ -56,6 +74,7 @@ class GrowiImportForm extends React.Component {
     // TODO use appContainer.apiv3.post
     await this.props.appContainer.apiPost('/v3/import', {
       fileName: this.state.zipFileName,
+      collections: Array.from(this.state.collections),
       schema: this.state.schema,
     });
     // TODO toastSuccess, toastError
@@ -120,10 +139,22 @@ class GrowiImportForm extends React.Component {
               </thead>
               <tbody>
                 {this.state.fileStats.map((file) => {
+                  const { fileName, collectionName } = file;
                   return (
-                    <tr key={file.fileName}>
-                      <td>{file.fileName}</td>
-                      <td>{file.collectionName}</td>
+                    <tr key={fileName}>
+                      <td>
+                        <input
+                          type="checkbox"
+                          id={collectionName}
+                          name={collectionName}
+                          className="form-check-input"
+                          value={collectionName}
+                          checked={this.state.collections.has(collectionName)}
+                          onChange={this.toggleCheckbox}
+                        />
+                      </td>
+                      <td>{fileName}</td>
+                      <td>{collectionName}</td>
                     </tr>
                   );
                 })}

+ 5 - 2
src/server/routes/apiv3/import.js

@@ -96,7 +96,7 @@ module.exports = (crowi) => {
   router.post('/', async(req, res) => {
     // TODO: add express validator
 
-    const { fileName, schema } = req.body;
+    const { fileName, collections, schema } = req.body;
     const zipFile = importService.getFile(fileName);
 
     // unzip
@@ -104,10 +104,13 @@ module.exports = (crowi) => {
     // eslint-disable-next-line no-unused-vars
     const { meta, fileStats } = await importService.parseZipFile(zipFile);
 
+    // filter fileStats
+    const filteredFileStats = fileStats.filter(({ fileName, collectionName, size }) => { return collections.includes(collectionName) });
+
     // TODO: validate using meta data
 
     try {
-      await Promise.all(fileStats.map(async({ fileName, collectionName, size }) => {
+      await Promise.all(filteredFileStats.map(async({ fileName, collectionName, size }) => {
         const Model = importService.getModelFromCollectionName(collectionName);
         const jsonFile = importService.getFile(fileName);