ShareLinkForm.jsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import React from 'react';
  2. import { withTranslation } from 'react-i18next';
  3. import dateFnsFormat from 'date-fns/format';
  4. import { createSubscribedElement } from './UnstatedUtils';
  5. import AppContainer from '../services/AppContainer';
  6. import PageContainer from '../services/PageContainer';
  7. class ShareLinkForm extends React.Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. expirationType: 'unlimited',
  12. numberOfDays: 7,
  13. description: '',
  14. customExpiration: dateFnsFormat(new Date(), 'yyyy-MM-ddThh:mm:s'),
  15. };
  16. this.handleChangeExpirationType = this.handleChangeExpirationType.bind(this);
  17. this.handleChangeNumberOfDays = this.handleChangeNumberOfDays.bind(this);
  18. this.handleChangeDescription = this.handleChangeDescription.bind(this);
  19. this.handleIssueShareLink = this.handleIssueShareLink.bind(this);
  20. }
  21. /**
  22. * change expirationType
  23. * @param {string} expirationType
  24. */
  25. handleChangeExpirationType(expirationType) {
  26. this.setState({ expirationType });
  27. }
  28. /**
  29. * change numberOfDays
  30. * @param {number} numberOfDays
  31. */
  32. handleChangeNumberOfDays(numberOfDays) {
  33. this.setState({ numberOfDays });
  34. }
  35. /**
  36. * change description
  37. * @param {string} description
  38. */
  39. handleChangeDescription(description) {
  40. this.setState({ description });
  41. }
  42. /**
  43. * change customExpiration
  44. * @param {date} customExpiration
  45. */
  46. handleChangeCustomExpiration(customExpiration) {
  47. this.setState({ customExpiration });
  48. }
  49. handleIssueShareLink() {
  50. // use these options
  51. console.log(this.state);
  52. console.log('発行する!');
  53. }
  54. renderExpirationTypeOptions() {
  55. const { expirationType } = this.state;
  56. return (
  57. <div className="form-group">
  58. <div className="custom-control custom-radio offset-4 mb-2">
  59. <input
  60. type="radio"
  61. className="custom-control-input"
  62. id="customRadio1"
  63. name="expirationType"
  64. value="customRadio1"
  65. checked={expirationType === 'unlimited'}
  66. onChange={() => { this.handleChangeExpirationType('unlimited') }}
  67. />
  68. <label className="custom-control-label" htmlFor="customRadio1">Unlimited</label>
  69. </div>
  70. <div className="custom-control custom-radio offset-4 mb-2">
  71. <input
  72. type="radio"
  73. className="custom-control-input"
  74. id="customRadio2"
  75. value="customRadio2"
  76. checked={expirationType === 'numberOfDays'}
  77. onChange={() => { this.handleChangeExpirationType('numberOfDays') }}
  78. name="expirationType"
  79. />
  80. <label className="custom-control-label" htmlFor="customRadio2">
  81. <div className="row align-items-center m-0">
  82. <input
  83. type="number"
  84. min="1"
  85. className="col-4"
  86. name="expirationType"
  87. value={this.state.numberOfDays}
  88. onFocus={() => { this.handleChangeExpirationType('numberOfDays') }}
  89. onChange={e => this.handleChangeNumberOfDays(Number(e.target.value))}
  90. />
  91. <span className="col-auto">Days</span>
  92. </div>
  93. </label>
  94. </div>
  95. <div className="custom-control custom-radio offset-4 mb-2">
  96. <input
  97. type="radio"
  98. className="custom-control-input"
  99. id="customRadio3"
  100. name="expirationType"
  101. value="customRadio3"
  102. checked={expirationType === 'custom'}
  103. onChange={() => { this.handleChangeExpirationType('custom') }}
  104. />
  105. <label className="custom-control-label" htmlFor="customRadio3">
  106. Custom
  107. </label>
  108. <input
  109. type="datetime-local"
  110. className="ml-3"
  111. name="customExpiration"
  112. value={this.state.customExpiration}
  113. onFocus={() => { this.handleChangeExpirationType('custom') }}
  114. onChange={e => this.handleChangeCustomExpiration(e.target.value)}
  115. />
  116. </div>
  117. </div>
  118. );
  119. }
  120. renderDescriptionForm() {
  121. return (
  122. <div className="form-group row">
  123. <label htmlFor="inputDesc" className="col-md-4 col-form-label">Description</label>
  124. <div className="col-md-4">
  125. <input
  126. type="text"
  127. className="form-control"
  128. id="inputDesc"
  129. placeholder="Enter description"
  130. value={this.state.description}
  131. onChange={e => this.handleChangeDescription(e.target.value)}
  132. />
  133. </div>
  134. </div>
  135. );
  136. }
  137. render() {
  138. return (
  139. <div className="share-link-form border p-3">
  140. <h4>Expiration Date</h4>
  141. {this.renderExpirationTypeOptions()}
  142. <hr />
  143. {this.renderDescriptionForm()}
  144. <div className="text-right">
  145. <button type="button" className="btn btn-primary" onClick={this.handleIssueShareLink}>
  146. Issue
  147. </button>
  148. </div>
  149. </div>
  150. );
  151. }
  152. }
  153. const ShareLinkFormWrapper = (props) => {
  154. return createSubscribedElement(ShareLinkForm, props, [AppContainer, PageContainer]);
  155. };
  156. export default withTranslation()(ShareLinkFormWrapper);