Advertisement
dfghgfhplkjbv

src/components/Subscribe/SubscribeMain.js

Mar 1st, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 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. console.log(this.props)
  49.  
  50. return (
  51. <div className={styles.wrap} style={style}>
  52. <div className={styles.root}>
  53. <div className={styles.image}>
  54. <Img fluid={this.props.subscribe.childImageSharp.fluid} alt="" />
  55. </div>
  56. <div className={styles.content}>
  57. <h1 className={styles.title}>{formatMessage(locale, 'subscribeTitle')}</h1>
  58. <div>
  59. <p className={styles.description}>{formatMessage(locale, 'subscribeDescription')}</p>
  60. <div>
  61. <label>
  62. <form method="post" action="" className={styles.form}>
  63. <div className={styles.formInnerContainer}>
  64. {status && (
  65. <span
  66. className={styles.status}
  67. style={status === 'success' ? { color: 'green' } : { color: 'red' }}
  68. >
  69. {status === 'success'
  70. ? locale === 'en'
  71. ? 'Success!'
  72. : 'Успешно!'
  73. : locale === 'en'
  74. ? 'Something went wrong!'
  75. : 'Ошибка'}
  76. </span>
  77. )}
  78. <input
  79. type="email"
  80. name=""
  81. placeholder={formatMessage(locale, 'email')}
  82. className={classNames(styles.input, {
  83. [styles.success]: status === 'success',
  84. [styles.error]: status === 'error',
  85. })}
  86. value={this.state.email}
  87. onChange={this.handleOnChange}
  88. disabled={status === 'success'}
  89. />
  90. </div>
  91.  
  92. {loading ? (
  93. <button className={styles.button} onClick={this.handleSubmit}>
  94. {formatMessage(locale, 'loading')}
  95. </button>
  96. ) : (
  97. <button className={styles.button} onClick={this.handleSubmit}>
  98. {formatMessage(locale, 'subsribeButton')}
  99. </button>
  100. )}
  101. </form>
  102. </label>
  103. <div className={styles.under}>{formatMessage(locale, 'subscribeUnder')}</div>
  104. </div>
  105. </div>
  106. </div>
  107. </div>
  108. </div>
  109. )
  110. }
  111. }
  112.  
  113. SubscribeMain.propTypes = {
  114. locale: PropTypes.string,
  115. }
  116.  
  117. export default SubscribeMain
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement