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

Improve get droppable item method

https://youtrack.weseek.co.jp/issue/GW-7913
- Add level and root props to BookmarkFolderItem
- Change item value to props in useDrag method
- Remove getAllDesc method
- Compare root and level of item with target to get droppable value
- Adjust BookmarkFolderItem props
- Remove getAllAncestors from model and api route
Mudana-Grune 3 лет назад
Родитель
Сommit
0701c33257

+ 18 - 27
packages/app/src/components/Bookmarks/BookmarkFolderItem.tsx

@@ -30,9 +30,13 @@ import DeleteBookmarkFolderModal from './DeleteBookmarkFolderModal';
 type BookmarkFolderItemProps = {
   bookmarkFolder: BookmarkFolderItems
   isOpen?: boolean
+  level: number
+  root: string
 }
 const BookmarkFolderItem: FC<BookmarkFolderItemProps> = (props: BookmarkFolderItemProps) => {
-  const { bookmarkFolder, isOpen: _isOpen = false } = props;
+  const {
+    bookmarkFolder, isOpen: _isOpen = false, level, root,
+  } = props;
 
   const { t } = useTranslation();
   const {
@@ -167,7 +171,7 @@ const BookmarkFolderItem: FC<BookmarkFolderItemProps> = (props: BookmarkFolderIt
 
   const [, drag] = useDrag({
     type: 'FOLDER',
-    item:  bookmarkFolder,
+    item:  props,
     canDrag: () => {
 
       return true;
@@ -184,35 +188,21 @@ const BookmarkFolderItem: FC<BookmarkFolderItemProps> = (props: BookmarkFolderIt
     }),
   });
 
-  const folderItemDropHandler = async(item: BookmarkFolderItems) => {
+  const folderItemDropHandler = async(item: BookmarkFolderItemProps) => {
     // TODO: Implement update parent folder
   };
 
-  const getAllDesc = async(folderId: string) => {
-    try {
-      const res = await apiv3Get(`/bookmark-folder/get-parents/${folderId}`);
-      return res.data;
-    }
-    catch (err) {
-      toastError(err);
-    }
-  };
-  const isDropable = (item: BookmarkFolderItems, target: BookmarkFolderItems) => {
-    let dropable = false;
-    getAllDesc(bookmarkFolder._id).then((result) => {
-      if (result.ancestors.includes(item._id)) {
-        dropable = false;
-      }
-    });
 
-    if (target.parent === item._id || target._id === item._id || target.children?.includes(item)) {
-      dropable = false;
+  const isDroppable = (item: BookmarkFolderItemProps, targetRoot: string, targetLevel: number): boolean => {
+    if (item.bookmarkFolder.parent === bookmarkFolder._id) {
+      return false;
     }
-    else {
-      dropable = true;
+    if (item.root === targetRoot) {
+      if (item.level === level || item.level < targetLevel) {
+        return false;
+      }
     }
-
-    return dropable;
+    return true;
   };
 
   const [, drop] = useDrop(() => ({
@@ -230,8 +220,7 @@ const BookmarkFolderItem: FC<BookmarkFolderItemProps> = (props: BookmarkFolderIt
     },
     canDrop: (item) => {
       // Implement isDropable function & improve
-
-      return isDropable(item, bookmarkFolder);
+      return isDroppable(item, root, level);
     },
     collect: monitor => ({
       isOver: monitor.isOver(),
@@ -245,6 +234,8 @@ const BookmarkFolderItem: FC<BookmarkFolderItemProps> = (props: BookmarkFolderIt
           <BookmarkFolderItem
             key={childFolder._id}
             bookmarkFolder={childFolder}
+            level={level + 1}
+            root={root}
           />
         </div>
       );

+ 2 - 0
packages/app/src/components/Bookmarks/BookmarkFolderTree.tsx

@@ -21,6 +21,8 @@ const BookmarkFolderTree = (): JSX.Element => {
               key={item._id}
               bookmarkFolder={item}
               isOpen={false}
+              level={0}
+              root={item._id}
             />
           );
         })}

+ 0 - 14
packages/app/src/server/models/bookmark-folder.ts

@@ -33,7 +33,6 @@ export interface BookmarkFolderModel extends Model<BookmarkFolderDocument>{
   deleteFolderAndChildren(bookmarkFolderId: Types.ObjectId | string): Promise<{deletedCount: number}>
   updateBookmarkFolder(bookmarkFolderId: string, name: string, parent: string): Promise<BookmarkFolderDocument>
   insertOrUpdateBookmarkedPage(pageId: IPageHasId, userId: Types.ObjectId | string, folderId: string): Promise<BookmarkFolderDocument>
-  getAllAncestors(folderId: string): Promise<BookmarkFolderItems[]>
 }
 
 const bookmarkFolderSchema = new Schema<BookmarkFolderDocument, BookmarkFolderModel>({
@@ -172,17 +171,4 @@ Promise<BookmarkFolderDocument> {
   return bookmarkFolder;
 };
 
-bookmarkFolderSchema.statics.getAllAncestors = async function(folderId: string): Promise<string[]> {
-  const getParent = async(folderId) => {
-    let currentParent : string[] = [];
-    const parentFolder = await this.findById(folderId);
-    if (parentFolder && parentFolder.parent != null) {
-      currentParent.push(parentFolder.id);
-      currentParent = currentParent.concat(await getParent(parentFolder.parent));
-    }
-    return currentParent;
-  };
-  const parents = await getParent(folderId);
-  return parents;
-};
 export default getOrCreateModel<BookmarkFolderDocument, BookmarkFolderModel>('BookmarkFolder', bookmarkFolderSchema);

+ 0 - 11
packages/app/src/server/routes/apiv3/bookmark-folder.ts

@@ -108,16 +108,5 @@ module.exports = (crowi) => {
     }
   });
 
-  router.get('/get-parents/:folderId', accessTokenParser, loginRequiredStrictly, async(req, res) => {
-    const { folderId } = req.params;
-
-    try {
-      const ancestors = await BookmarkFolder.getAllAncestors(folderId);
-      return res.apiv3({ ancestors });
-    }
-    catch (err) {
-      return res.apiv3Err(err, 500);
-    }
-  });
   return router;
 };