import React from 'react'
|
import { useDrag, useDrop } from 'react-dnd'
|
|
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()}
|
</div>
|
)
|
}
|
export default Card
|