ShareLinkForm.jsx 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { withTranslation } from 'react-i18next';
  4. import dateFnsFormat from 'date-fns/format';
  5. import parse from 'date-fns/parse';
  6. import { isInteger } from 'core-js/fn/number';
  7. import { withUnstatedContainers } from './UnstatedUtils';
  8. import { toastSuccess, toastError } from '../util/apiNotification';
  9. import AppContainer from '../services/AppContainer';
  10. import PageContainer from '../services/PageContainer';
  11. class ShareLinkForm extends React.Component {
  12. constructor(props) {
  13. super(props);
  14. this.state = {
  15. expirationType: 'unlimited',
  16. numberOfDays: '7',
  17. description: '',
  18. customExpirationDate: dateFnsFormat(new Date(), 'yyyy-MM-dd'),
  19. customExpirationTime: dateFnsFormat(new Date(), 'hh:mm'),
  20. };
  21. this.handleChangeExpirationType = this.handleChangeExpirationType.bind(this);
  22. this.handleChangeNumberOfDays = this.handleChangeNumberOfDays.bind(this);
  23. this.handleChangeDescription = this.handleChangeDescription.bind(this);
  24. this.handleIssueShareLink = this.handleIssueShareLink.bind(this);
  25. }
  26. /**
  27. * change expirationType
  28. * @param {string} expirationType
  29. */
  30. handleChangeExpirationType(expirationType) {
  31. this.setState({ expirationType });
  32. }
  33. /**
  34. * change numberOfDays
  35. * @param {string} numberOfDays
  36. */
  37. handleChangeNumberOfDays(numberOfDays) {
  38. this.setState({ numberOfDays });
  39. }
  40. /**
  41. * change description
  42. * @param {string} description
  43. */
  44. handleChangeDescription(description) {
  45. this.setState({ description });
  46. }
  47. /**
  48. * change customExpirationDate
  49. * @param {date} customExpirationDate
  50. */
  51. handleChangeCustomExpirationDate(customExpirationDate) {
  52. this.setState({ customExpirationDate });
  53. }
  54. /**
  55. * change customExpirationTime
  56. * @param {date} customExpirationTime
  57. */
  58. handleChangeCustomExpirationTime(customExpirationTime) {
  59. this.setState({ customExpirationTime });
  60. }
  61. /**
  62. * Generate expiredAt by expirationType
  63. */
  64. generateExpired() {
  65. const { t } = this.props;
  66. const { expirationType } = this.state;
  67. let expiredAt;
  68. if (expirationType === 'unlimited') {
  69. return null;
  70. }
  71. if (expirationType === 'numberOfDays') {
  72. if (!isInteger(Number(this.state.numberOfDays))) {
  73. throw new Error(t('share_links.Invalid_Number_of_Date'));
  74. }
  75. const date = new Date();
  76. date.setDate(date.getDate() + Number(this.state.numberOfDays));
  77. expiredAt = date;
  78. }
  79. if (expirationType === 'custom') {
  80. const { customExpirationDate, customExpirationTime } = this.state;
  81. expiredAt = parse(`${customExpirationDate}T${customExpirationTime}`, "yyyy-MM-dd'T'HH:mm", new Date());
  82. }
  83. return expiredAt;
  84. }
  85. closeForm() {
  86. const { onCloseForm } = this.props;
  87. if (onCloseForm == null) {
  88. return;
  89. }
  90. onCloseForm();
  91. }
  92. async handleIssueShareLink() {
  93. const {
  94. t, appContainer, pageContainer,
  95. } = this.props;
  96. const { pageId } = pageContainer.state;
  97. const { description } = this.state;
  98. let expiredAt;
  99. try {
  100. expiredAt = this.generateExpired();
  101. }
  102. catch (err) {
  103. return toastError(err);
  104. }
  105. try {
  106. await appContainer.apiv3Post('/share-links/', { relatedPage: pageId, expiredAt, description });
  107. this.closeForm();
  108. toastSuccess(t('toaster.issue_share_link'));
  109. }
  110. catch (err) {
  111. toastError(err);
  112. }
  113. }
  114. renderExpirationTypeOptions() {
  115. const { expirationType } = this.state;
  116. const { t } = this.props;
  117. return (
  118. <div className="form-group row">
  119. <label htmlFor="inputDesc" className="col-md-5 col-form-label">{t('share_links.expire')}</label>
  120. <div className="col-md-7">
  121. <div className="custom-control custom-radio form-group ">
  122. <input
  123. type="radio"
  124. className="custom-control-input"
  125. id="customRadio1"
  126. name="expirationType"
  127. value="customRadio1"
  128. checked={expirationType === 'unlimited'}
  129. onChange={() => { this.handleChangeExpirationType('unlimited') }}
  130. />
  131. <label className="custom-control-label" htmlFor="customRadio1">{t('share_links.Unlimited')}</label>
  132. </div>
  133. <div className="custom-control custom-radio form-group">
  134. <input
  135. type="radio"
  136. className="custom-control-input"
  137. id="customRadio2"
  138. value="customRadio2"
  139. checked={expirationType === 'numberOfDays'}
  140. onChange={() => { this.handleChangeExpirationType('numberOfDays') }}
  141. name="expirationType"
  142. />
  143. <label className="custom-control-label" htmlFor="customRadio2">
  144. <div className="row align-items-center m-0">
  145. <input
  146. type="number"
  147. min="1"
  148. className="col-4"
  149. name="expirationType"
  150. value={this.state.numberOfDays}
  151. onFocus={() => { this.handleChangeExpirationType('numberOfDays') }}
  152. onChange={e => this.handleChangeNumberOfDays(Number(e.target.value))}
  153. />
  154. <span className="col-auto">{t('share_links.Days')}</span>
  155. </div>
  156. </label>
  157. </div>
  158. <div className="custom-control custom-radio form-group text-nowrap mb-0">
  159. <input
  160. type="radio"
  161. className="custom-control-input"
  162. id="customRadio3"
  163. name="expirationType"
  164. value="customRadio3"
  165. checked={expirationType === 'custom'}
  166. onChange={() => { this.handleChangeExpirationType('custom') }}
  167. />
  168. <label className="custom-control-label" htmlFor="customRadio3">
  169. {t('share_links.Custom')}
  170. </label>
  171. <div className="d-inline-flex flex-wrap">
  172. <input
  173. type="date"
  174. className="ml-3 mb-2"
  175. name="customExpirationDate"
  176. value={this.state.customExpirationDate}
  177. onFocus={() => { this.handleChangeExpirationType('custom') }}
  178. onChange={e => this.handleChangeCustomExpirationDate(e.target.value)}
  179. />
  180. <input
  181. type="time"
  182. className="ml-3 mb-2"
  183. name="customExpiration"
  184. value={this.state.customExpirationTime}
  185. onFocus={() => { this.handleChangeExpirationType('custom') }}
  186. onChange={e => this.handleChangeCustomExpirationTime(e.target.value)}
  187. />
  188. </div>
  189. </div>
  190. </div>
  191. </div>
  192. );
  193. }
  194. renderDescriptionForm() {
  195. const { t } = this.props;
  196. return (
  197. <div className="form-group row">
  198. <label htmlFor="inputDesc" className="col-md-5 col-form-label">{t('share_links.description')}</label>
  199. <div className="col-md-4">
  200. <input
  201. type="text"
  202. className="form-control"
  203. id="inputDesc"
  204. placeholder={t('share_links.enter_desc')}
  205. value={this.state.description}
  206. onChange={e => this.handleChangeDescription(e.target.value)}
  207. />
  208. </div>
  209. </div>
  210. );
  211. }
  212. render() {
  213. const { t } = this.props;
  214. return (
  215. <div className="share-link-form p-3">
  216. <h3 className="grw-modal-head pb-2"> { t('share_links.share_settings') }</h3>
  217. <div className=" p-3">
  218. {this.renderExpirationTypeOptions()}
  219. {this.renderDescriptionForm()}
  220. <button type="button" className="btn btn-primary d-block mx-auto px-5" onClick={this.handleIssueShareLink}>
  221. {t('share_links.Issue')}
  222. </button>
  223. </div>
  224. </div>
  225. );
  226. }
  227. }
  228. /**
  229. * Wrapper component for using unstated
  230. */
  231. const ShareLinkFormWrapper = withUnstatedContainers(ShareLinkForm, [AppContainer, PageContainer]);
  232. ShareLinkForm.propTypes = {
  233. t: PropTypes.func.isRequired, // i18next
  234. appContainer: PropTypes.instanceOf(AppContainer).isRequired,
  235. pageContainer: PropTypes.instanceOf(PageContainer).isRequired,
  236. onCloseForm: PropTypes.func,
  237. export default withTranslation()(ShareLinkFormWrapper);