2020-03-27 09:02:11 +00:00
|
|
|
import React, { useState } from "react";
|
2019-09-09 10:30:22 +00:00
|
|
|
import { useDrag, DragSourceMonitor, DragPreviewImage } from "react-dnd";
|
2019-11-25 05:07:27 +00:00
|
|
|
import blankImage from "assets/images/blank.png";
|
|
|
|
|
import { WidgetCardProps } from "widgets/BaseWidget";
|
2019-09-09 10:30:22 +00:00
|
|
|
import styled from "styled-components";
|
2019-11-25 05:07:27 +00:00
|
|
|
import { WidgetIcons } from "icons/WidgetIcons";
|
2020-01-20 09:00:37 +00:00
|
|
|
import {
|
|
|
|
|
useWidgetDragResize,
|
|
|
|
|
useShowPropertyPane,
|
|
|
|
|
} from "utils/hooks/dragResizeHooks";
|
2020-03-03 07:02:53 +00:00
|
|
|
import AnalyticsUtil from "utils/AnalyticsUtil";
|
2020-03-27 09:02:11 +00:00
|
|
|
import { generateReactKey } from "utils/generators";
|
2019-08-29 11:22:09 +00:00
|
|
|
|
2019-09-06 09:30:22 +00:00
|
|
|
type CardProps = {
|
|
|
|
|
details: WidgetCardProps;
|
2019-09-09 10:30:22 +00:00
|
|
|
};
|
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};
|
2019-09-09 10:30:22 +00:00
|
|
|
border: 1px solid ${props => props.theme.colors.paneCard};
|
2019-09-06 09:30:22 +00:00
|
|
|
color: ${props => props.theme.colors.textOnDarkBG};
|
2019-11-13 11:34:11 +00:00
|
|
|
height: 72px;
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: center;
|
2019-09-06 09:30:22 +00:00
|
|
|
& > div {
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: center;
|
|
|
|
|
}
|
2019-09-09 10:30:22 +00:00
|
|
|
&:hover {
|
2019-08-26 12:41:21 +00:00
|
|
|
background: #fff;
|
|
|
|
|
cursor: grab;
|
2019-09-09 10:30:22 +00:00
|
|
|
color: ${props => props.theme.colors.textDefault};
|
2019-09-26 11:11:28 +00:00
|
|
|
svg {
|
|
|
|
|
path {
|
|
|
|
|
fill: ${props => props.theme.colors.textDefault};
|
|
|
|
|
}
|
2019-09-27 08:08:31 +00:00
|
|
|
rect {
|
|
|
|
|
stroke: ${props => props.theme.colors.textDefault};
|
|
|
|
|
}
|
2019-09-26 11:11:28 +00:00
|
|
|
}
|
2019-09-06 09:30:22 +00:00
|
|
|
}
|
|
|
|
|
& 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) => {
|
2020-01-20 09:00:37 +00:00
|
|
|
const { setIsDragging } = useWidgetDragResize();
|
2020-03-27 09:02:11 +00:00
|
|
|
// Generate a new widgetId which can be used in the future for this widget.
|
|
|
|
|
const [widgetId, setWidgetId] = useState(generateReactKey());
|
2020-01-20 09:00:37 +00:00
|
|
|
const showPropertyPane = useShowPropertyPane();
|
2019-09-17 15:09:55 +00:00
|
|
|
const [, drag, preview] = useDrag({
|
2020-03-27 09:02:11 +00:00
|
|
|
item: { ...props.details, widgetId },
|
2019-08-26 12:41:21 +00:00
|
|
|
collect: (monitor: DragSourceMonitor) => ({
|
|
|
|
|
isDragging: monitor.isDragging(),
|
|
|
|
|
}),
|
2020-01-20 09:00:37 +00:00
|
|
|
begin: () => {
|
2020-03-03 07:02:53 +00:00
|
|
|
AnalyticsUtil.logEvent("WIDGET_CARD_DRAG", {
|
|
|
|
|
widgetType: props.details.type,
|
|
|
|
|
widgetName: props.details.widgetCardName,
|
|
|
|
|
});
|
2020-01-20 09:00:37 +00:00
|
|
|
showPropertyPane && showPropertyPane(undefined);
|
|
|
|
|
setIsDragging && setIsDragging(true);
|
|
|
|
|
},
|
2020-03-11 13:59:46 +00:00
|
|
|
end: (widget, monitor) => {
|
2020-03-03 07:02:53 +00:00
|
|
|
AnalyticsUtil.logEvent("WIDGET_CARD_DROP", {
|
|
|
|
|
widgetType: props.details.type,
|
|
|
|
|
widgetName: props.details.widgetCardName,
|
2020-03-11 13:59:46 +00:00
|
|
|
didDrop: monitor.didDrop(),
|
2020-03-03 07:02:53 +00:00
|
|
|
});
|
2020-03-27 09:02:11 +00:00
|
|
|
// We've finished dragging, generate a new widgetId to be used for next drag.
|
|
|
|
|
setWidgetId(generateReactKey());
|
2020-01-21 07:14:47 +00:00
|
|
|
setIsDragging && setIsDragging(false);
|
|
|
|
|
},
|
2019-09-09 10:30:22 +00:00
|
|
|
});
|
2019-09-26 11:11:28 +00:00
|
|
|
|
|
|
|
|
const iconType: string = props.details.type;
|
|
|
|
|
const Icon = WidgetIcons[iconType]();
|
2020-02-25 07:23:59 +00:00
|
|
|
const className = `t--widget-card-draggable-${props.details.type
|
|
|
|
|
.split("_")
|
|
|
|
|
.join("")
|
|
|
|
|
.toLowerCase()}`;
|
2019-08-26 12:41:21 +00:00
|
|
|
return (
|
2019-09-09 10:30:22 +00:00
|
|
|
<React.Fragment>
|
2019-08-29 11:22:09 +00:00
|
|
|
<DragPreviewImage connect={preview} src={blankImage} />
|
2020-02-25 07:23:59 +00:00
|
|
|
<Wrapper ref={drag} className={className}>
|
2019-09-17 10:09:00 +00:00
|
|
|
<div>
|
2019-09-26 11:11:28 +00:00
|
|
|
{Icon}
|
2019-09-26 11:37:09 +00:00
|
|
|
<IconLabel>{props.details.widgetCardName}</IconLabel>
|
2019-09-06 09:30:22 +00:00
|
|
|
</div>
|
|
|
|
|
</Wrapper>
|
2019-08-29 11:22:09 +00:00
|
|
|
</React.Fragment>
|
2019-09-09 10:30:22 +00:00
|
|
|
);
|
|
|
|
|
};
|
2019-08-26 12:41:21 +00:00
|
|
|
|
2019-09-09 10:30:22 +00:00
|
|
|
export default WidgetCard;
|