All files / app/src/views/warehouse-view warehouse-view.tsx

1.25% Statements 2/160
100% Branches 0/0
0% Functions 0/1
1.25% Lines 2/160

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 198 199 200 201 202 203 2041x                                                   1x                                                                                                                                                                                                                                                                                                                                                                  
import { FC, useMemo, useState } from 'react'
import { getNextPageWithHydraView, useTranslate } from '@/shared/lib'
import { selectedAccountingObjectsAtom, WarehouseContractsInfo } from '@/entities/warehouse'
import { useRouter } from 'next/router'
import { Button } from '@tmk/ui-kit'
import Plus from '@/shared/assets/icons/common/outlined-plus.svg'
import ArrowBulk from '@/shared/assets/icons/common/ArrowBulk.svg'
import { ModalMutateRack, Rack, WarehouseRack, useInfiniteRacksByWarehouseCollection } from '@/entities/rack'
import Skeleton from 'react-loading-skeleton'
import { ActionsFooter, EmptyData } from '@/shared/ui'
import { useAtom, useAtomValue } from 'jotai'
import cn from 'classnames'
import { isViewerOpenSidebarAtom } from '@/entities/viewer'
import {
  getOperationForm,
  OperationsSidebar,
  OperationType,
  selectedOperationType,
  useValidateModal,
  ValidationModal,
} from '@/entities/accounting-object'
import { useResetAtom } from 'jotai/utils'
import { PERMISSION_MANAGER_WAREHOUSE_RACK, RoleProvider } from '@/features/rolevik'
import { OPERATIONS_STATUSES } from '@/features/validator'
import { PipeStateEnum, PipeStatusEnum } from '@/entities/pipe'
 
export const WarehouseView: FC = () => {
  const { t } = useTranslate(['accounting-object', 'common'])
  const [isOpenModal, setIsOpenModal] = useState(false)
  const selectedAccountingObjects = useAtomValue(selectedAccountingObjectsAtom)
  const [selectedOperation, setSelectedOperationType] = useAtom(selectedOperationType)
  const resetSelectedOperation = useResetAtom(selectedOperationType)
  const resetAccountingObjectsForOperation = useResetAtom(selectedAccountingObjectsAtom)
  const {
    isOpen: isValidationModelOpen,
    setIsOpen: setIsOpenValidationOpen,
    checkValidHandler,
  } = useValidateModal(true)
 
  const isOpenSidebar = useAtomValue(isViewerOpenSidebarAtom)
 
  const router = useRouter()
  const warehouseId = router.query.pid as string
  const {
    data: racks,
    isIdle,
    isLoading,
    hasNextPage,
    isFetchingNextPage,
    fetchNextPage,
    refetch,
  } = useInfiniteRacksByWarehouseCollection(warehouseId, {
    enabled: !!warehouseId,
    key: ['warehouse-view', warehouseId as string],
    keepPreviousData: true,
    getNextPageParam: lastPage => getNextPageWithHydraView(lastPage),
  })
 
  const isLoadingRacks = isIdle || isLoading
  const onCloseHandler = () => {
    resetSelectedOperation(), resetAccountingObjectsForOperation()
  }
  const isShipmentAvailable = () => {
    const operation = OPERATIONS_STATUSES[OperationType.SHIPMENT]
    return selectedAccountingObjects.every(
      object =>
        operation.availableStates.includes(object.state as PipeStateEnum) &&
        operation.availableStatuses.includes(object.status as PipeStatusEnum)
    )
  }
 
  const mergedRacks = useMemo(() => {
    return racks?.pages?.reduce<Rack[]>((acc, page) => {
      return [...acc, ...page['hydra:member']]
    }, [])
  }, [racks])
 
  const isEmpty = mergedRacks?.length === 0
 
  return (
    <>
      <ValidationModal
        isOpen={isValidationModelOpen}
        onClose={() => {
          setIsOpenValidationOpen(false)
        }}
      />
      <ModalMutateRack
        isOpen={isOpenModal}
        onClose={() => {
          refetch()
          return setIsOpenModal(false)
        }}
        isCreate
      />
      <OperationsSidebar isOpen={!!selectedOperation} onClose={onCloseHandler}>
        {getOperationForm(selectedOperation, onCloseHandler, t)}
      </OperationsSidebar>
      <div
        className={cn('relative', {
          'mb-20': !!selectedAccountingObjects.length,
        })}
      >
        <div className='flex flex-col gap-y-5'>
          <WarehouseContractsInfo />
          <div className='w-full p-5 flex flex-col gap-y-5'>
            <div className='bg-white w-full py-3'>
              <RoleProvider availablePermissions={[PERMISSION_MANAGER_WAREHOUSE_RACK]}>
                <Button
                  variant='icon'
                  className='mx-auto'
                  childrenClassName='flex items-center gap-x-2.5'
                  onClick={() => setIsOpenModal(true)}
                >
                  <h2 className='text-text'>{t('Add rack')}</h2>
                  <Plus className='stroke-blue-dark fill-blue-dark' />
                </Button>
              </RoleProvider>
            </div>
            <div className='flex flex-col gap-y-5'>
              {isLoadingRacks ? (
                <Skeleton count={5} height={100} containerClassName='flex flex-col gap-y-5' />
              ) : (
                <>
                  {isEmpty ? (
                    <EmptyData
                      title={t('The racks have not been created yet')}
                      subTitle={t(
                        'Wait until the person in charge creates the racks and rows to see the actual distribution of objects'
                      )}
                    />
                  ) : (
                    <>
                      {mergedRacks?.map(rack => (
                        <WarehouseRack rack={rack} key={rack.id} />
                      ))}
                    </>
                  )}
                </>
              )}
            </div>
          </div>
        </div>
        {!!selectedAccountingObjects.length && (
          <ActionsFooter isOpenSidebar={isOpenSidebar}>
            <div className='flex items-center justify-between w-full'>
              <span className='text-main-regular text-text'>
                {t('common:Selected')}: <span className='text-main-semibold'>{selectedAccountingObjects.length}</span>
              </span>
              <div className='flex items-center gap-x-2.5'>
                <Button
                  onClick={() => {
                    const isValid = isShipmentAvailable()
                    if (!isValid) {
                      checkValidHandler(isValid, [
                        'valid_package',
                        'valid_status_shipment',
                        'valid_single_warehouse_contract',
                      ])
                    } else {
                      setSelectedOperationType(OperationType.SHIPMENT)
                    }
                  }}
                  variant='contained-icon'
                  color='blue'
                  childrenClassName='flex items-center gap-x-2.5'
                >
                  {t('common:Ship')}
                  <ArrowBulk className='fill-currentColor group-hover:fill-blue-dark' />
                </Button>
                <Button
                  onClick={() => setSelectedOperationType(OperationType.MOVEMENTS)}
                  variant='contained-icon'
                  color='blue'
                  childrenClassName='flex items-center gap-x-2.5'
                >
                  {t('common:Move')}
                  <ArrowBulk className='fill-currentColor group-hover:fill-blue-dark' />
                </Button>
              </div>
            </div>
          </ActionsFooter>
        )}
        {!isEmpty && (
          <Button
            onClick={async () => {
              if (hasNextPage) {
                await fetchNextPage()
              }
            }}
            loading={isLoading || isFetchingNextPage}
            className='mx-auto mb-5'
            variant='outlined'
            color='secondary'
            disabled={!hasNextPage}
          >
            <h3>{t('common:Show_more')}</h3>
          </Button>
        )}
      </div>
    </>
  )
}