ShortenerOptions.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import React, { Component } from 'react';
  2. import PropTypes from 'prop-types';
  3. import styled from 'styled-components';
  4. import Checkbox from '../Checkbox';
  5. import TextInput from '../TextInput';
  6. import { fadeIn } from '../../helpers/animations';
  7. const Wrapper = styled.div`
  8. position: absolute;
  9. top: 74px;
  10. left: 0;
  11. display: flex;
  12. flex-direction: column;
  13. align-self: flex-start;
  14. justify-content: flex-start;
  15. z-index: 2;
  16. @media only screen and (max-width: 448px) {
  17. width: 100%;
  18. top: 56px;
  19. }
  20. `;
  21. const CheckboxWrapper = styled.div`
  22. display: flex;
  23. @media only screen and (max-width: 448px) {
  24. justify-content: center;
  25. }
  26. `;
  27. const InputWrapper = styled.div`
  28. display: flex;
  29. align-items: center;
  30. @media only screen and (max-width: 448px) {
  31. flex-direction: column;
  32. align-items: flex-start;
  33. > * {
  34. margin-bottom: 16px;
  35. }
  36. }
  37. `;
  38. const Label = styled.label`
  39. font-size: 18px;
  40. margin-right: 16px;
  41. animation: ${fadeIn} 0.5s ease-out;
  42. @media only screen and (max-width: 448px) {
  43. font-size: 14px;
  44. margin-right: 8px;
  45. }
  46. `;
  47. class ShortenerOptions extends Component {
  48. constructor() {
  49. super();
  50. this.state = {
  51. customurlCheckbox: false,
  52. passwordCheckbox: false,
  53. };
  54. this.handleCheckbox = this.handleCheckbox.bind(this);
  55. }
  56. shouldComponentUpdate(nextProps, nextState) {
  57. const { customurlCheckbox, passwordCheckbox } = this.state;
  58. return (
  59. this.props.isAuthenticated !== nextProps.isAuthenticated ||
  60. customurlCheckbox !== nextState.customurlCheckbox ||
  61. this.props.domain !== nextProps.domain ||
  62. passwordCheckbox !== nextState.passwordCheckbox
  63. );
  64. }
  65. handleCheckbox(e) {
  66. e.preventDefault();
  67. if (!this.props.isAuthenticated) {
  68. return this.props.setShortenerFormError('Please login or sign up to use this feature.');
  69. }
  70. const type = e.target.id;
  71. return this.setState({ [type]: !this.state[type] });
  72. }
  73. render() {
  74. const { customurlCheckbox, passwordCheckbox } = this.state;
  75. const { isAuthenticated, domain } = this.props;
  76. const customUrlInput = customurlCheckbox && (
  77. <div>
  78. <Label htmlFor="customurl">{domain || window.location.hostname}/</Label>
  79. <TextInput id="customurl" type="text" placeholder="custom name" small />
  80. </div>
  81. );
  82. const passwordInput = passwordCheckbox && (
  83. <div>
  84. <Label htmlFor="customurl">password:</Label>
  85. <TextInput id="password" type="password" placeholder="password" small />
  86. </div>
  87. );
  88. return (
  89. <Wrapper isAuthenticated={isAuthenticated}>
  90. <CheckboxWrapper>
  91. <Checkbox
  92. id="customurlCheckbox"
  93. name="customurlCheckbox"
  94. label="Set custom URL"
  95. checked={this.state.customurlCheckbox}
  96. onClick={this.handleCheckbox}
  97. />
  98. <Checkbox
  99. id="passwordCheckbox"
  100. name="passwordCheckbox"
  101. label="Set password"
  102. checked={this.state.passwordCheckbox}
  103. onClick={this.handleCheckbox}
  104. />
  105. </CheckboxWrapper>
  106. <InputWrapper>
  107. {customUrlInput}
  108. {passwordInput}
  109. </InputWrapper>
  110. </Wrapper>
  111. );
  112. }
  113. }
  114. ShortenerOptions.propTypes = {
  115. isAuthenticated: PropTypes.bool.isRequired,
  116. domain: PropTypes.string.isRequired,
  117. setShortenerFormError: PropTypes.func.isRequired,
  118. };
  119. export default ShortenerOptions;