AlertModal.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React from 'react'
  15. import { observer } from 'mobx-react'
  16. import styled from 'styled-components'
  17. import Modal from '@src/components/ui/Modal'
  18. import Button from '@src/components/ui/Button'
  19. import StatusImage from '@src/components/ui/StatusComponents/StatusImage'
  20. import { ThemePalette } from '@src/components/Theme'
  21. import KeyboardManager from '@src/utils/KeyboardManager'
  22. const Wrapper = styled.div<any>`
  23. display: flex;
  24. flex-direction: column;
  25. align-items: center;
  26. justify-content: center;
  27. padding: 48px;
  28. `
  29. const Message = styled.div<any>`
  30. font-size: 18px;
  31. text-align: center;
  32. margin-top: 48px;
  33. `
  34. const ExtraMessage = styled.div<any>`
  35. color: ${ThemePalette.grayscale[4]};
  36. margin: 11px 0 48px 0;
  37. text-align: center;
  38. `
  39. const Buttons = styled.div<any>`
  40. display: flex;
  41. justify-content: ${props => (props.centered ? 'center' : 'space-between')};
  42. width: 100%;
  43. `
  44. type AlertType = 'error' | 'confirmation' | 'loading'
  45. type Props = {
  46. message?: string,
  47. extraMessage?: string,
  48. type?: AlertType,
  49. isOpen?: boolean,
  50. title?: string,
  51. onRequestClose?: () => void,
  52. onConfirmation?: () => void,
  53. }
  54. @observer
  55. class AlertModal extends React.Component<Props> {
  56. static defaultProps = {
  57. type: 'confirmation',
  58. }
  59. id: string | undefined
  60. componentDidMount() {
  61. this.id = `${new Date().getTime().toString()}-${Math.random()}`
  62. KeyboardManager.onEnter(`alert-${this.id}`, () => {
  63. if (this.props.isOpen && this.props.onConfirmation) {
  64. this.props.onConfirmation()
  65. }
  66. }, 2)
  67. }
  68. componentWillUnmount() {
  69. KeyboardManager.removeKeyDown(`alert-${this.id}`)
  70. }
  71. renderDismissButton() {
  72. if (this.props.type !== 'error') {
  73. return null
  74. }
  75. return (
  76. <Buttons centered>
  77. <Button secondary onClick={this.props.onRequestClose} data-test-id="aModal-dismissButton">Dismiss</Button>
  78. </Buttons>
  79. )
  80. }
  81. renderConfirmationButtons() {
  82. if (this.props.type !== 'confirmation') {
  83. return null
  84. }
  85. return (
  86. <Buttons>
  87. <Button secondary onClick={this.props.onRequestClose} data-test-id="aModal-noButton">No</Button>
  88. <Button onClick={this.props.onConfirmation} data-test-id="aModal-yesButton">Yes</Button>
  89. </Buttons>
  90. )
  91. }
  92. render() {
  93. const status = this.props.type === 'loading' ? 'RUNNING' : (this.props.type || 'confirmation')
  94. return (
  95. // eslint-disable-next-line react/jsx-props-no-spreading
  96. <Modal {...this.props} isOpen={this.props.isOpen || false}>
  97. <Wrapper data-test-id="alertModal">
  98. <StatusImage status={status} data-test-id="aModal-status" />
  99. {this.props.message ? <Message data-test-id="aModal-message">{this.props.message}</Message> : null}
  100. {this.props.extraMessage ? <ExtraMessage data-test-id="aModal-extraMessage">{this.props.extraMessage}</ExtraMessage> : null}
  101. {this.renderConfirmationButtons()}
  102. {this.renderDismissButton()}
  103. </Wrapper>
  104. </Modal>
  105. )
  106. }
  107. }
  108. export default AlertModal