Error.tsx 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import React, { FC } from 'react';
  2. import { connect } from 'react-redux';
  3. import styled, { css } from 'styled-components';
  4. import { prop } from 'styled-tools';
  5. import { fadeIn } from '../helpers/animations';
  6. interface Message {
  7. bottom?: number;
  8. left?: number;
  9. }
  10. interface Props extends Message {
  11. error: any;
  12. type: string;
  13. }
  14. const ErrorMessage = styled.p<Message>`
  15. content: '';
  16. position: absolute;
  17. right: 36px;
  18. bottom: ${prop('bottom', -64)}px;
  19. display: block;
  20. font-size: 14px;
  21. color: red;
  22. animation: ${fadeIn} 0.3s ease-out;
  23. @media only screen and (max-width: 768px) {
  24. right: 8px;
  25. bottom: -40px;
  26. font-size: 12px;
  27. }
  28. ${({ left }) =>
  29. left > -1 &&
  30. css`
  31. right: auto;
  32. left: ${left}px;
  33. `};
  34. `;
  35. const Error: FC<Props> = ({ bottom, error, left, type }) => {
  36. const message = error[type] && (
  37. <ErrorMessage left={left} bottom={bottom}>
  38. {error[type]}
  39. </ErrorMessage>
  40. );
  41. return <div>{message}</div>;
  42. };
  43. Error.defaultProps = {
  44. bottom: -64,
  45. left: -1,
  46. };
  47. const mapStateToProps = ({ error }) => ({ error });
  48. export default connect(mapStateToProps)(Error);