customIgnoreDurationModal.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import React from 'react';
  2. import moment from 'moment';
  3. import {sprintf} from 'sprintf-js';
  4. import {ModalRenderProps} from 'app/actionCreators/modal';
  5. import Alert from 'app/components/alert';
  6. import Button from 'app/components/button';
  7. import ButtonBar from 'app/components/buttonBar';
  8. import {IconWarning} from 'app/icons';
  9. import {t} from 'app/locale';
  10. import {ResolutionStatusDetails} from 'app/types';
  11. const defaultProps = {
  12. label: t('Ignore this issue until \u2026'),
  13. };
  14. type Props = ModalRenderProps & {
  15. onSelected: (details: ResolutionStatusDetails) => void;
  16. } & typeof defaultProps;
  17. type State = {
  18. dateWarning: boolean;
  19. };
  20. export default class CustomIgnoreDurationModal extends React.Component<Props, State> {
  21. static defaultProps = defaultProps;
  22. state: State = {
  23. dateWarning: false,
  24. };
  25. snoozeDateInputRef = React.createRef<HTMLInputElement>();
  26. snoozeTimeInputRef = React.createRef<HTMLInputElement>();
  27. selectedIgnoreMinutes = () => {
  28. const dateStr = this.snoozeDateInputRef.current?.value; // YYYY-MM-DD
  29. const timeStr = this.snoozeTimeInputRef.current?.value; // HH:MM
  30. if (dateStr && timeStr) {
  31. const selectedDate = moment.utc(dateStr + ' ' + timeStr);
  32. if (selectedDate.isValid()) {
  33. const now = moment.utc();
  34. return selectedDate.diff(now, 'minutes');
  35. }
  36. }
  37. return 0;
  38. };
  39. snoozeClicked = () => {
  40. const minutes = this.selectedIgnoreMinutes();
  41. this.setState({
  42. dateWarning: minutes <= 0,
  43. });
  44. if (minutes > 0) {
  45. this.props.onSelected({ignoreDuration: minutes});
  46. }
  47. this.props.closeModal();
  48. };
  49. render() {
  50. // Give the user a sane starting point to select a date
  51. // (prettier than the empty date/time inputs):
  52. const defaultDate = new Date();
  53. defaultDate.setDate(defaultDate.getDate() + 14);
  54. defaultDate.setSeconds(0);
  55. defaultDate.setMilliseconds(0);
  56. const defaultDateVal = sprintf(
  57. '%d-%02d-%02d',
  58. defaultDate.getUTCFullYear(),
  59. defaultDate.getUTCMonth() + 1,
  60. defaultDate.getUTCDate()
  61. );
  62. const defaultTimeVal = sprintf('%02d:00', defaultDate.getUTCHours());
  63. const {Header, Body, Footer, label} = this.props;
  64. return (
  65. <React.Fragment>
  66. <Header>{label}</Header>
  67. <Body>
  68. <form className="form-horizontal">
  69. <div className="control-group">
  70. <h6 className="nav-header">{t('Date')}</h6>
  71. <input
  72. className="form-control"
  73. type="date"
  74. id="snooze-until-date"
  75. defaultValue={defaultDateVal}
  76. ref={this.snoozeDateInputRef}
  77. required
  78. style={{padding: '0 10px'}}
  79. />
  80. </div>
  81. <div className="control-group m-b-1">
  82. <h6 className="nav-header">{t('Time (UTC)')}</h6>
  83. <input
  84. className="form-control"
  85. type="time"
  86. id="snooze-until-time"
  87. defaultValue={defaultTimeVal}
  88. ref={this.snoozeTimeInputRef}
  89. style={{padding: '0 10px'}}
  90. required
  91. />
  92. </div>
  93. </form>
  94. </Body>
  95. {this.state.dateWarning && (
  96. <Alert icon={<IconWarning size="md" />} type="error">
  97. {t('Please enter a valid date in the future')}
  98. </Alert>
  99. )}
  100. <Footer>
  101. <ButtonBar gap={1}>
  102. <Button type="button" priority="default" onClick={this.props.closeModal}>
  103. {t('Cancel')}
  104. </Button>
  105. <Button type="button" priority="primary" onClick={this.snoozeClicked}>
  106. {t('Ignore')}
  107. </Button>
  108. </ButtonBar>
  109. </Footer>
  110. </React.Fragment>
  111. );
  112. }
  113. }