king
2020-07-17 e70553694d5ab6869a85a0db60bc14942a06bc15
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
import React from 'react'
import { useDrag, useDrop } from 'react-dnd'
import { Icon } from 'antd'
 
import asyncComponent from '@/utils/asyncComponent'
 
import './index.scss'
 
// const Home = asyncComponent(() => import('@/mob/home'))
const MobLogin1 = asyncComponent(() => import('@/mob/components/login/mob-login-1'))
const MobLogin2 = asyncComponent(() => import('@/mob/components/login/mob-login-2'))
 
const Card = ({ id, card, moveCard, findCard, editId, editCard, delCard, hasDrop, doubleClickCard, updateConfig }) => {
  const originalIndex = findCard(id).index
  const [{ isDragging }, drag] = useDrag({
    item: { type: 'mob', id, originalIndex },
    collect: monitor => ({
      isDragging: monitor.isDragging(),
    }),
  })
  const [, drop] = useDrop({
    accept: 'mob',
    canDrop: () => true,
    drop: (item) => {
      if (!item.hasOwnProperty('originalIndex')) {
        hasDrop(card)
      }
    },
    hover({ id: draggedId }) {
      if (!draggedId) return
      if (draggedId !== id) {
        const { index: overIndex } = findCard(id)
        moveCard(draggedId, overIndex)
      }
    },
  })
 
  let style = { opacity: 1}
  if (isDragging && card.type !== 'login') {
    style = { opacity: 0.3}
  }
  if (card.type === 'login') {
    style.height = '100%'
  }
 
  const getCardComponent = () => {
    if (card.type === 'login') {
      if (card.subtype === 'mob-login-1') {
        return (<MobLogin1 card={card} triggerEdit={editCard} editId={editId} onDoubleClick={doubleClickCard} updateConfig={updateConfig} />)
      } else if (card.subtype === 'mob-login-2') {
        return (<MobLogin2 card={card} triggerEdit={editCard} editId={editId} onDoubleClick={doubleClickCard} updateConfig={updateConfig} />)
      }
    }
  }
 
  return (
    <div className="mk-component-card" ref={node => drag(drop(node))} style={style}>
      {getCardComponent()}
      <Icon className="remove-component" title="delete" type="delete" onClick={() => delCard(id)} />
    </div>
  )
}
export default Card