Advertisement
dfghgfhplkjbv

src/components/Subscribe/SubscibeMain.js

Mar 1st, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. import React, { Component } from 'react'
  2. import addToMailchimp from 'gatsby-plugin-mailchimp'
  3. import PropTypes from 'prop-types'
  4. import classNames from 'classnames'
  5. import { formatMessage } from 'src/translations'
  6. import styles from './SubscribeMain.module.scss'
  7. import Img from 'gatsby-image'
  8.  
  9. class SubscribeMain extends Component {
  10. state = {
  11. email: '',
  12. status: null,
  13. detail: null,
  14. loading: false,
  15. error: null,
  16. }
  17.  
  18. handleSubmit = (e) => {
  19. const { email } = this.state
  20. this.setState({
  21. loading: true,
  22. error: null,
  23. })
  24.  
  25. e.preventDefault()
  26. addToMailchimp(email)
  27. .then((data) => {
  28. this.setState(() => {
  29. return {
  30. status: data.result,
  31. }
  32. })
  33. })
  34. .then(() => {
  35. this.setState({ email: '', loading: false })
  36. })
  37. .catch((error) => {
  38. this.setState({ error })
  39. })
  40. }
  41. handleOnChange = (event) => {
  42. this.setState({ email: event.target.value })
  43. }
  44.  
  45. render() {
  46. const { status, loading } = this.state
  47. const { style, locale } = this.props
  48.  
  49. return (
  50. <div className={styles.wrap} style={style}>
  51. <div className={styles.root}>
  52. <div className={styles.image}>
  53. <Img fluid={this.props.subscribe.childImageSharp.fluid} alt="" />
  54. </div>
  55. <div className={styles.content}>
  56. <h1 className={styles.title}>{formatMessage(locale, 'subscribeTitle')}</h1>
  57. <div>
  58. <p className={styles.description}>{formatMessage(locale, 'subscribeDescription')}</p>
  59. <div>
  60. <label>
  61. <form method="post" action="" className={styles.form}>
  62. <div className={styles.formInnerContainer}>
  63. {status && (
  64. <span
  65. className={styles.status}
  66. style={status === 'success' ? { color: 'green' } : { color: 'red' }}
  67. >
  68. {status === 'success'
  69. ? locale === 'en'
  70. ? 'Success!'
  71. : 'Успешно!'
  72. : locale === 'en'
  73. ? 'Something went wrong!'
  74. : 'Ошибка'}
  75. </span>
  76. )}
  77. <input
  78. type="email"
  79. name=""
  80. placeholder={formatMessage(locale, 'email')}
  81. className={classNames(styles.input, {
  82. [styles.success]: status === 'success',
  83. [styles.error]: status === 'error',
  84. })}
  85. value={this.state.email}
  86. onChange={this.handleOnChange}
  87. disabled={status === 'success'}
  88. />
  89. </div>
  90.  
  91. {loading ? (
  92. <button className={styles.button} onClick={this.handleSubmit}>
  93. {formatMessage(locale, 'loading')}
  94. </button>
  95. ) : (
  96. <button className={styles.button} onClick={this.handleSubmit}>
  97. {formatMessage(locale, 'subsribeButton')}
  98. </button>
  99. )}
  100. </form>
  101. </label>
  102. <div className={styles.under}>{formatMessage(locale, 'subscribeUnder')}</div>
  103. </div>
  104. </div>
  105. </div>
  106. </div>
  107. </div>
  108. )
  109. }
  110. }
  111.  
  112. SubscribeMain.propTypes = {
  113. locale: PropTypes.string,
  114. }
  115.  
  116. export default SubscribeMain
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement