All files / app/src/shared/hooks useCancelModal.tsx

97.97% Statements 97/99
100% Branches 23/23
83.33% Functions 5/6
97.97% Lines 97/99

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 1331x                                       1x   1x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x   31x 4x 4x   31x       31x 3x 3x 3x   31x 31x 2x 1x 1x   1x 1x 1x 1x 2x 31x 31x 31x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x   31x 18x 31x 31x 21x 21x   31x 19x 19x 31x   31x 30x 22x 22x 22x 22x 22x   22x 22x 22x 22x 22x 22x 22x 22x 22x 22x   22x 22x 22x 22x 22x 22x 22x 22x 22x   22x 22x 22x 22x 22x   22x 31x   31x 31x 31x 31x 31x 31x 31x  
import { useCallback, useEffect, useMemo, useState } from 'react'
import { Modal } from '@/shared/ui'
import { Nullable } from '@/shared/@types'
import { useRouter } from 'next/router'
import { atom } from 'jotai'
import { useUpdateAtom } from 'jotai/utils'
import cn from 'classnames'
import { Button } from '@tmk/ui-kit'
 
interface CanselModalProps {
  modalData: {
    title: string
    description: string
    acceptAction: string
    declineAction: string
  }
  isNeedListen?: boolean
  variant?: 'cancel' | 'save'
  modalClassName?: string
}
export const isHaveChangesAtom = atom(false)
 
export const useCancelModal = ({
  modalData,
  isNeedListen = false,
  variant = 'cancel',
  modalClassName,
}: CanselModalProps) => {
  const [isOpen, setIsOpen] = useState(false)
  const [onAgree, setOnAgree] = useState<Nullable<() => void>>(null)
  const [isNeedListenUrl, setIsNeedListenUrl] = useState(() => isNeedListen)
  const [isOnAgreeLoading, setIsOnAgreeLoading] = useState(false)
  const [url, setUrl] = useState<Nullable<string>>(null)
  const setIsHaveChanges = useUpdateAtom(isHaveChangesAtom)
  const router = useRouter()
 
  const closeModal = () => {
    setIsOpen(false)
  }
 
  const changeListenMode = (mode: boolean) => {
    setIsNeedListenUrl(mode)
  }
 
  const openModal = (cb?: () => void) => {
    setIsOpen(true)
    cb && setOnAgree(() => cb)
  }
 
  const onRouteChangeStart = useCallback(
    (nextPath: string) => {
      if (!isNeedListenUrl) {
        return
      }
 
      setIsOpen(true)
      setUrl(nextPath)
      router.events.emit('routeChangeError', new Error('cancelRouteChange'), nextPath, { shallow: false })
      throw 'cancelRouteChange'
    },
    [isNeedListen, isNeedListenUrl]
  )
  const onAgreeHandler = async () => {
    try {
      setIsOnAgreeLoading(true)
      removeListener()
      onAgree && (await onAgree())
      closeModal()
      setIsHaveChanges(false)
      url && router.push(url)
    } finally {
      setIsOnAgreeLoading(false)
    }
  }
 
  useEffect(() => {
    return () => setIsHaveChanges(false)
  }, [])
  const removeListener = () => {
    router.events.off('routeChangeStart', onRouteChangeStart)
  }
 
  useEffect(() => {
    router.events.on('routeChangeStart', onRouteChangeStart)
    return removeListener
  }, [router, onRouteChangeStart])
 
  const ModalComponent = useMemo(() => {
    return () => {
      return (
        <Modal
          isOpen={isOpen}
          onClose={closeModal}
          className={cn('flex flex-col items-center max-w-lg text-center', modalClassName)}
        >
          <>
            <h2 className='mb-5 text-black'>{modalData.title}</h2>
            <h4 className='mb-10 text-text'>{modalData.description}</h4>
            <div className='flex items-center justify-between w-full gap-5 text-center'>
              <Button
                variant={variant !== 'save' ? 'contained' : 'outlined'}
                color='secondary'
                size='large'
                className='flex-grow'
                onClick={closeModal}
              >
                <h3>{modalData.declineAction}</h3>
              </Button>
              <Button
                variant={variant === 'save' ? 'contained' : 'outlined'}
                color={variant === 'cancel' ? 'secondary' : 'primary'}
                size='large'
                className='flex-grow'
                loading={isOnAgreeLoading}
                onClick={onAgreeHandler}
              >
                <h3>{modalData.acceptAction}</h3>
              </Button>
            </div>
          </>
        </Modal>
      )
    }
  }, [isOpen, modalClassName, modalData, variant])
 
  return {
    CancelModal: ModalComponent,
    closeModal,
    openModal,
    changeListenMode,
  }
}