2个数组/对象具有相同的值,但其中一个有更多的对象-如何简化代码而不重复?

7lrncoxx  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(287)

我有以下问题。我创建了两个数组,名为: editorDeskadminDesk 唯一的区别实际上是 adminDesk 具有菜单项=>路线、导航和好友。
问题是我有重复的代码,使这项工作。但我想简单一点,有一张这样的table: defaultDesk 通过一些检查,如果是管理员用户,则显示此信息,否则不执行任何操作。
我怎样才能做到这一点?
我的代码:

import S from '@sanity/desk-tool/structure-builder'

import sanityClient from 'part:@sanity/base/client'

import PagePreview from '../studio/src/components/preview/page/PagePreview'

import {
   FiPaperclip,
   FiUser,
   FiUsers,
   FiEdit,
   FiSettings,
   FiMessageSquare,
   FiHeart,
   FiInfo,
   FiNavigation,
   FiFileText,
   FiFile,
   FiTag,
   FiCheckSquare
} from 'react-icons/fi'

const client = sanityClient.withConfig({ apiVersion: 'v1' })

const groupQuery = '* [_type == "system.group" && $identity in members] {_id}'

const hiddenDocTypes = (listItem) =>
   ![
      'author',
      'post',
      'page',
      'category',
      'theme',
      'comment',
      'friends',
      'route',
      'navigation',
      'general',
      'featured',
      'cookie'
   ].includes(listItem.getId())

const siteMenu = () =>
   client
      .fetch(groupQuery)
      .then((docs) => docs.map((doc) => doc._id.split('.').pop()))
      .then((groupNames) => {
         const defaultDesk = [
            S.listItem()
               .title('Posts')
               .child(S.documentTypeList('post').title('Posts overview'))
               .schemaType('post')
               .icon(FiEdit),

            S.divider(),

            S.listItem()
               .title('Pages')
               .child(S.documentTypeList('page').title('Pages overview'))
               .schemaType('page')
               .icon(FiFile),

            S.divider(),

            S.listItem()
               .title('Authors')
               .child(S.documentTypeList('author').title('Authors overview'))
               .schemaType('author')
               .icon(FiUser),

            S.divider(),

            S.listItem()
               .title('Themes')
               .child(S.documentTypeList('theme').title('Themes overview'))
               .schemaType('theme')
               .icon(FiHeart),

            S.divider(),

            S.listItem()
               .title('Categories')
               .child(
                  S.documentTypeList('category').title('Categories overview')
               )
               .schemaType('category')
               .icon(FiTag),

            S.divider(),

            S.listItem()
               .title('Comments')
               .child(S.documentTypeList('comment').title('Comments overview'))
               .schemaType('comment')
               .icon(FiMessageSquare),

            S.divider()
         ]

         const editorDesk = [
            // Settings menu with sub-menu's.
            S.listItem()
               .title('Settings')
               .child(
                  S.list()
                     .title('Settings')
                     .items([
                        S.listItem()
                           .title('General')
                           .icon(FiInfo)
                           .child(
                              S.document()
                                 .schemaType('general')
                                 .documentId('generalSettings')
                                 .title('General Settings')
                           ),

                        S.divider(),

                        S.listItem()
                           .title('Featured articles')
                           .icon(FiFileText)
                           .child(
                              S.document()
                                 .schemaType('featured')
                                 .documentId('featuredArticlesSettings')
                                 .title('Featured Articles Settings')
                           ),

                        S.divider()
                     ])
               )
               .icon(FiSettings),

            ...S.documentTypeListItems().filter(hiddenDocTypes)
         ]

         const adminDesk = [
            // Settings menu with sub-menu's.
            S.listItem()
               .title('Settings')
               .child(
                  S.list()
                     .title('Settings')
                     .items([
                        S.listItem()
                           .title('General')
                           .icon(FiInfo)
                           .child(
                              S.document()
                                 .schemaType('general')
                                 .documentId('generalSettings')
                                 .title('General Settings')
                           ),

                        S.divider(),

                        S.listItem()
                           .title('Navigation')
                           .icon(FiNavigation)
                           .child(
                              S.document()
                                 .schemaType('navigation')
                                 .documentId('navigationSettings')
                                 .title('Navigation Settings')
                           ),

                        S.divider(),

                        S.listItem()
                           .title('Featured articles')
                           .icon(FiFileText)
                           .child(
                              S.document()
                                 .schemaType('featured')
                                 .documentId('featuredArticlesSettings')
                                 .title('Featured Articles Settings')
                           ),

                        S.divider(),

                        S.listItem()
                           .title('Cookie Consent')
                           .icon(FiCheckSquare)
                           .child(
                              S.document()
                                 .schemaType('cookie')
                                 .documentId('cookieSettings')
                                 .title('Cookie Consent Settings')
                           ),

                        S.divider(),

                        S.listItem()
                           .title('Friends team')
                           .icon(FiUsers)
                           .child(
                              S.document()
                                 .schemaType('friends')
                                 .documentId('settingsFriends')
                                 .title('Friends Settings')
                           )
                     ])
               )
               .icon(FiSettings),

            ...S.documentTypeListItems().filter(hiddenDocTypes)
         ]

         if (groupNames.includes('editors')) {
            return S.list()
               .title('Website (editor)')
               .items([...defaultDesk, ...editorDesk])
         }

         if (groupNames.includes('administrator')) {
            return S.list()
               .title('Website (admin)')
               .items([...defaultDesk, ...adminDesk])
         }

         return S.list().title('Website').items(deskItems)
      })
      .catch(() => {
         // In case of any errors fetching the groups, just return some standard
         // structure. This will only happen if the query cannot be performed for
         // some reason.
         return S.list().title('Website').items([])
      })

export default siteMenu

export const getDefaultDocumentNode = (props) => {
   /**
    * Here you can define fallback views for document types without
    * a structure definition for the document node. If you want different
    * fallbacks for different types, or document values (e.g. if there is a slug present)
    * you can set up that logic in here too.
    * https://www.sanity.io/docs/structure-builder-reference#getdefaultdocumentnode-97e44ce262c9
    */
   const { schemaType } = props
   if (schemaType === 'page') {
      return S.document().views([
         S.view.form(),
         S.view.component(PagePreview).title('Preview Page')
      ])
   }

   return S.document().views([S.view.form()])
}
tez616oj

tez616oj1#

合并在这里行吗
您可以用某种结构定义默认桌面,然后

const someOtherDesk = [
   ...defaultDesk,
   restOfTheCode
]

或者,如果它不是那么简单,必须在根级别更深地合并=>

const someOtherDesk = Object.assign([], defaultDesk, adminDesk);

创建新数组,合并defaultdesk,然后合并admindesk

相关问题