All files / app/src/shared/ui/admin-pagination admin-pagination.tsx

81.08% Statements 120/148
92.3% Branches 12/13
83.33% Functions 5/6
81.08% Lines 120/148

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 1981x                                                     1x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x   12x 12x 12x 12x 12x 12x   12x 10x 10x 7x 7x 7x 7x 7x   7x 7x   10x         10x   12x 12x 12x 12x   12x 10x 10x 10x 10x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x   13x 13x 88x 88x 88x 88x 88x 88x 88x 88x 88x 1x 1x 1x   88x 88x 88x 13x 13x 13x 13x   10x 10x   12x 12x 12x 12x 12x 12x 12x 12x 12x   12x 12x 12x 12x 12x 12x 12x 12x 12x 12x 12x   12x 12x 12x 12x 12x   12x             1x                                                          
import { FC, Fragment } from 'react'
import { Menu, Transition } from '@headlessui/react'
import { FCWithClassName } from '@/shared/@types'
import { Button } from '../button'
import { useTranslate } from '@/shared/lib'
import Skeleton from 'react-loading-skeleton'
import cn from 'classnames'
import ArrowIcon from '@/shared/assets/icons/common/pagination-select-arrow.svg'
 
export interface AdminPaginationProps {
  page: number
  totalPageProp?: number
  totalItems?: number
  withPageSize?: boolean
  pageSize?: number
  pageSizeOptions?: number[]
  pageSizeTitle?: string
  selectedText?: string
  selectedCount?: number
  disabled?: boolean
  statsClassName?: string
  paginationClassName?: string
  onPageChange: (page: number) => void
  onPageSizeChange?: (page: number) => void
  showTotal?: boolean
}
 
export const AdminPagination: FCWithClassName<AdminPaginationProps> = ({
  page,
  totalPageProp = 0,
  withPageSize,
  pageSize = 0,
  pageSizeOptions,
  pageSizeTitle,
  selectedText,
  selectedCount = 0,
  totalItems = 0,
  disabled,
  className = '',
  statsClassName = '',
  paginationClassName = '',
  onPageChange,
  onPageSizeChange,
  showTotal = false,
}) => {
  const { t } = useTranslate(['common'])
  const totalPage = totalPageProp || Math.ceil(totalItems / pageSize)
  const isPrevPageDisabled = disabled || page === 1
  const isNextPageDisabled = disabled || page === totalPage
  const arrowButtonStyle = 'group w-6 !rounded-small'
 
  return (
    <div
      className={cn('flex items-center justify-start w-full', {
        [className]: className,
        'gap-6': !!totalItems,
      })}
    >
      {showTotal && (
        <div className='flex gap-large'>
          {!!totalItems && (
            <h6
              data-testId='items-on-page'
              className={cn('text-text-secondary', {
                [statsClassName]: statsClassName,
              })}
            >
              {t('Page')} {page} {t('of')} {totalPage}
            </h6>
          )}
          {!!selectedCount && (
            <h6 className='text-gray'>
              {selectedText ? selectedText : 'Selected'}:{selectedCount}
            </h6>
          )}
        </div>
      )}
      <div
        className={cn('flex items-center gap-6', {
          [paginationClassName]: paginationClassName,
        })}
      >
        {withPageSize && (
          <div className='flex items-center gap-1'>
            <h6 className='text-text-secondary'>{pageSizeTitle}</h6>
            <Menu as='div' defaultValue={pageSize} className='relative inline-block text-left'>
              {({ open }) => (
                <>
                  <Menu.Button data-testid='page-size-select' className='inline-flex items-center gap-0.5'>
                    <h6 className='text-black'>{pageSize}</h6>
                    <div className='flex items-center justify-center w-4 h-4'>
                      <ArrowIcon
                        className={cn('stroke-black transition-transform', {
                          'rotate-180': open,
                        })}
                      />
                    </div>
                  </Menu.Button>
                  <Transition
                    as={Fragment}
                    enter='transition ease-out duration-100'
                    enterFrom='transform opacity-0 scale-95'
                    enterTo='transform opacity-100 scale-100'
                    leave='transition ease-in duration-75'
                    leaveFrom='transform opacity-100 scale-100'
                    leaveTo='transform opacity-0 scale-95'
                  >
                    <Menu.Items className='absolute p-1 origin-top -translate-x-3 bg-white border mb-extra-small drop-shadow-pagination border-border rounded-base'>
                      {pageSizeOptions?.map(size => (
                        <Menu.Item key={size}>
                          <button
                            className={cn(
                              'flex items-center justify-center w-10 h-8 rounded-base text-black hover:bg-gray-tertiary',
                              {
                                '!text-gray': pageSize === size,
                              }
                            )}
                            onClick={() => {
                              onPageSizeChange?.(size)
                              onPageChange(1)
                            }}
                          >
                            <h6>{size}</h6>
                          </button>
                        </Menu.Item>
                      ))}
                    </Menu.Items>
                  </Transition>
                </>
              )}
            </Menu>
          </div>
        )}
        <div className='flex items-center gap-base'>
          <Button
            color='secondary'
            variant='border-icon'
            data-testid='left-button'
            className={arrowButtonStyle}
            disabled={isPrevPageDisabled}
            onClick={() => onPageChange(page - 1)}
            size='small'
          >
            <ArrowIcon className='rotate-90 stroke-currentColor ' />
          </Button>
          <h6 className='text-black'>{page}</h6>
          <Button
            color='secondary'
            variant='border-icon'
            data-testid='right-button'
            className={arrowButtonStyle}
            disabled={isNextPageDisabled}
            onClick={() => onPageChange(page + 1)}
            size='small'
          >
            <ArrowIcon className='-rotate-90 stroke-currentColor' />
          </Button>
        </div>
      </div>
    </div>
  )
}
 
export interface AdminPaginationSkeletonProps {
  className?: string
  paginationClassName?: string
}
 
export const AdminPaginationSkeleton: FC<AdminPaginationSkeletonProps> = ({
  className = '',
  paginationClassName = '',
}) => {
  return (
    <div
      className={cn('flex items-center justify-between w-full', {
        [className]: className,
      })}
    >
      <Skeleton className='!w-[90px] h-5' />
      <div
        className={cn('flex items-center gap-6', {
          [paginationClassName]: paginationClassName,
        })}
      >
        <div className='flex items-center gap-1'>
          <Skeleton className='!w-[90px] h-5' />
          <Skeleton className='!w-[30px] h-5' />
        </div>
        <div className='flex items-center gap-base'>
          <Skeleton className='!w-6 h-5' />
          <Skeleton className='!w-6 h-5' />
          <Skeleton className='!w-6 h-5' />
        </div>
      </div>
    </div>
  )
}