withTitle.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import styled from 'styled-components';
  4. import formatDate from 'date-fns/format';
  5. const Wrapper = styled.div`
  6. flex: 1 1 50%;
  7. display: flex;
  8. flex-direction: column;
  9. align-items: stretch;
  10. @media only screen and (max-width: 768px) {
  11. flex-basis: 100%;
  12. }
  13. `;
  14. const Title = styled.h3`
  15. margin-bottom: 12px;
  16. font-size: 24px;
  17. font-weight: 300;
  18. @media only screen and (max-width: 768px) {
  19. font-size: 18px;
  20. }
  21. `;
  22. const SubTitle = styled.span`
  23. margin-bottom: 32px;
  24. font-size: 13px;
  25. font-weight: 300;
  26. color: #aaa;
  27. @media only screen and (max-width: 768px) {
  28. font-size: 11px;
  29. }
  30. `;
  31. const Count = styled.span`
  32. font-weight: bold;
  33. border-bottom: 1px dotted #999;
  34. `;
  35. const withTitle = ChartComponent => {
  36. function WithTitle(props) {
  37. return (
  38. <Wrapper>
  39. <Title>
  40. {props.periodText && <Count>{props.data.reduce((sum, view) => sum + view, 0)}</Count>}
  41. {props.periodText ? ` tracked clicks in ${props.periodText}` : props.title}.
  42. </Title>
  43. {props.periodText && props.updatedAt && (
  44. <SubTitle>Last update in {formatDate(props.updatedAt, 'dddd, hh:mm aa')}.</SubTitle>
  45. )}
  46. <ChartComponent {...props} />
  47. </Wrapper>
  48. );
  49. }
  50. WithTitle.propTypes = {
  51. data: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.number, PropTypes.object])).isRequired,
  52. periodText: PropTypes.string,
  53. title: PropTypes.string,
  54. updatedAt: PropTypes.string.isRequired,
  55. };
  56. WithTitle.defaultProps = {
  57. title: '',
  58. periodText: '',
  59. };
  60. return WithTitle;
  61. };
  62. export default withTitle;