2019-08-29 11:22:09 +00:00
|
|
|
import React, { useState, useLayoutEffect, MutableRefObject } from 'react';
|
|
|
|
|
import { useDrag, DragSourceMonitor, DragPreviewImage } from 'react-dnd'
|
|
|
|
|
import blankImage from "../../assets/images/blank.png"
|
2019-09-06 09:30:22 +00:00
|
|
|
import { WidgetCardProps } from '../../widgets/BaseWidget'
|
2019-09-05 17:47:50 +00:00
|
|
|
import styled from 'styled-components';
|
2019-08-29 11:22:09 +00:00
|
|
|
import { generateReactKey } from "../../utils/generators"
|
2019-08-26 12:41:21 +00:00
|
|
|
|
2019-08-29 11:22:09 +00:00
|
|
|
|
2019-09-06 09:30:22 +00:00
|
|
|
type CardProps = {
|
|
|
|
|
details: WidgetCardProps;
|
2019-08-26 12:41:21 +00:00
|
|
|
}
|
|
|
|
|
|
2019-08-29 11:22:09 +00:00
|
|
|
export const Wrapper = styled.div`
|
2019-08-26 12:41:21 +00:00
|
|
|
padding: 10px 5px 10px 5px;
|
2019-09-06 09:30:22 +00:00
|
|
|
border-radius: ${props => props.theme.radii[1]}px;
|
2019-09-05 17:47:50 +00:00
|
|
|
background: ${props => props.theme.colors.paneCard};
|
|
|
|
|
border: 1px solid ${props=> props.theme.colors.paneCard};
|
2019-09-06 09:30:22 +00:00
|
|
|
color: ${props => props.theme.colors.textOnDarkBG};
|
|
|
|
|
& > div {
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: center;
|
|
|
|
|
}
|
2019-08-26 12:41:21 +00:00
|
|
|
&:hover{
|
|
|
|
|
background: #fff;
|
|
|
|
|
cursor: grab;
|
2019-09-06 09:30:22 +00:00
|
|
|
color: ${props => props.theme.colors.textDefault}
|
|
|
|
|
}
|
|
|
|
|
& i {
|
|
|
|
|
font-family: ${props => props.theme.fonts[2]};
|
|
|
|
|
font-size: ${props => props.theme.fontSizes[7]}px;
|
2019-08-26 12:41:21 +00:00
|
|
|
}
|
|
|
|
|
`;
|
2019-09-06 09:30:22 +00:00
|
|
|
|
2019-08-29 11:22:09 +00:00
|
|
|
export const IconLabel = styled.h5`
|
2019-08-26 12:41:21 +00:00
|
|
|
text-align: center;
|
|
|
|
|
margin: 0;
|
|
|
|
|
text-transform: uppercase;
|
2019-09-06 09:30:22 +00:00
|
|
|
font-weight: ${props => props.theme.fontWeights[1]};
|
2019-08-26 12:41:21 +00:00
|
|
|
flex-shrink: 1;
|
2019-09-06 09:30:22 +00:00
|
|
|
font-size: ${props => props.theme.fontSizes[1]}px;
|
2019-09-06 11:40:00 +00:00
|
|
|
line-height: ${props => props.theme.lineHeights[1]}px;
|
2019-08-26 12:41:21 +00:00
|
|
|
`;
|
|
|
|
|
|
2019-08-29 11:22:09 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
2019-09-06 09:30:22 +00:00
|
|
|
const WidgetCard = (props: CardProps) => {
|
2019-08-29 11:22:09 +00:00
|
|
|
const [initialOffset, setInitialOffset] = useState({ x: 0, y: 0})
|
|
|
|
|
|
|
|
|
|
const [{ isDragging }, drag, preview] = useDrag({
|
|
|
|
|
item: { type: props.details.widgetType, widget: props.details, key: generateReactKey(), initialOffset},
|
2019-08-26 12:41:21 +00:00
|
|
|
collect: (monitor: DragSourceMonitor) => ({
|
|
|
|
|
isDragging: monitor.isDragging(),
|
|
|
|
|
}),
|
|
|
|
|
})
|
2019-08-29 11:22:09 +00:00
|
|
|
const card: MutableRefObject<HTMLDivElement | null> = React.useRef(null)
|
|
|
|
|
useLayoutEffect(()=> {
|
|
|
|
|
const el = card.current
|
|
|
|
|
if (el) {
|
|
|
|
|
const rect = el.getBoundingClientRect()
|
|
|
|
|
setInitialOffset({
|
|
|
|
|
x: Math.ceil(rect.left),
|
|
|
|
|
y: Math.ceil(rect.top)
|
|
|
|
|
})
|
|
|
|
|
}
|
2019-09-06 09:30:22 +00:00
|
|
|
}, [setInitialOffset]);
|
2019-08-26 12:41:21 +00:00
|
|
|
return (
|
2019-08-29 11:22:09 +00:00
|
|
|
<React.Fragment >
|
|
|
|
|
<DragPreviewImage connect={preview} src={blankImage} />
|
2019-09-06 09:30:22 +00:00
|
|
|
<Wrapper ref={drag}>
|
|
|
|
|
<div ref={card}>
|
|
|
|
|
<i className={props.details.icon} />
|
|
|
|
|
<IconLabel>{props.details.label}</IconLabel>
|
|
|
|
|
</div>
|
|
|
|
|
</Wrapper>
|
2019-08-29 11:22:09 +00:00
|
|
|
</React.Fragment>
|
2019-08-26 12:41:21 +00:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default WidgetCard
|