-
Notifications
You must be signed in to change notification settings - Fork 143
/
index.js
208 lines (191 loc) · 5.51 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import React, { Component, Fragment } from 'react'
import { graphql } from 'react-apollo'
import { compose } from 'ramda'
import PropTypes from 'prop-types'
import { injectIntl, intlShape } from 'react-intl'
import { Input, Button } from 'vtex.styleguide'
import { withCssHandles } from 'vtex.css-handles'
import SUBSCRIBE_NEWSLETTER from './mutations/subscribeNewsletter.graphql'
import { formatIOMessage } from 'vtex.native-types'
const EMAIL_REGEX = /^[A-z0-9+_-]+(?:\.[A-z0-9+_-]+)*@(?:[A-z0-9](?:[A-z0-9-]*[A-z0-9])?\.)+[A-z0-9](?:[A-z0-9-]*[A-z0-9])?$/
const CSS_HANDLES = [
'newsletter',
'confirmation',
'container',
'confirmationTitle',
'confirmationText',
'form',
'inputGroup',
'buttonContainer',
'label',
'error',
]
class Newsletter extends Component {
state = {
email: '',
loading: false,
error: null,
success: null,
invalidEmail: false,
}
inputRef = React.createRef()
componentDidMount() {
this.mounted = true
}
componentWillUnmount() {
this.mounted = false
}
safeSetState(...params) {
if (this.mounted) {
this.setState(...params)
}
}
handleChangeEmail = e => {
this.setState({ email: e.target.value })
}
validateEmail = () => {
return EMAIL_REGEX.test(this.state.email)
}
handleSubmit = e => {
e.preventDefault()
if (!this.validateEmail()) {
this.setState({ invalidEmail: true })
if (this.inputRef && this.inputRef.current) {
this.inputRef.current.focus()
}
return
}
this.setState({
invalidEmail: false,
loading: true,
error: null,
success: null,
})
this.props
.subscribeNewsletter({ variables: { email: this.state.email } })
.then(() => {
this.safeSetState({ success: true, loading: false })
})
.catch(e => {
this.safeSetState({ error: true, loading: false })
})
}
render() {
const { hideLabel, intl, submit, label, placeholder, cssHandles } = this.props
const submitText = formatIOMessage({ id: submit, intl })
const labelText = formatIOMessage({ id: label, intl })
const placeholderText = formatIOMessage({ id: placeholder, intl })
const confirmationTitle = formatIOMessage({
id: 'store/newsletter.confirmationTitle',
intl,
})
const confirmationText = formatIOMessage({
id: 'store/newsletter.confirmationText',
intl,
})
const invalidEmailText = formatIOMessage({
id: 'store/newsletter.invalidEmail',
intl,
})
const errorMsg = formatIOMessage({
id: 'store/newsletter.error',
intl,
})
return (
<div
className={`${cssHandles.newsletter} ${
this.state.success ? cssHandles.confirmation : ''
} w-100`}
>
<div className={`${cssHandles.container} mw9 mr-auto ml-auto pv9`}>
{this.state.success ? (
<Fragment>
<div className={`${cssHandles.confirmationTitle} t-heading-3 pb4 tc`}>
{confirmationTitle}
</div>
<div className={`${cssHandles.confirmationText} t-body tc`}>
{confirmationText}
</div>
</Fragment>
) : (
<form className={`${cssHandles.form} mw6 center tc ph5 ph0-ns`}>
<label
className={`${cssHandles.label} t-heading-3 tc ${
hideLabel ? 'dn' : ''
}`}
htmlFor="newsletter-input"
>
{labelText}
</label>
<div className={`${cssHandles.inputGroup} flex-ns pt5`}>
<Input
ref={this.inputRef}
id="newsletter-input"
errorMessage={
this.state.invalidEmail ? invalidEmailText : null
}
placeholder={placeholderText}
name="newsletter"
value={this.state.email}
onChange={this.handleChangeEmail}
/>
<div
className={`${
cssHandles.buttonContainer
} pl4-ns flex-none pt3 pt0-ns`}
>
<Button
variation="primary"
type="submit"
onClick={this.handleSubmit}
isLoading={this.state.loading}
>
{submitText}
</Button>
</div>
</div>
{this.state.error && (
<div className={`${cssHandles.error} c-danger t-body pt5`}>
{errorMsg}
</div>
)}
</form>
)}
</div>
</div>
)
}
}
Newsletter.defaultProps = {
hideLabel: false,
showTerms: false,
}
Newsletter.propTypes = {
hideLabel: PropTypes.bool.isRequired,
showTerms: PropTypes.bool.isRequired,
label: PropTypes.string,
placeholder: PropTypes.string,
submit: PropTypes.string,
subscribeNewsletter: PropTypes.func.isRequired,
intl: intlShape,
}
Newsletter.getSchema = () => {
return {
title: 'admin/editor.newsletter.title',
description: 'admin/editor.newsletter.description',
type: 'object',
properties: {
hideLabel: {
type: 'boolean',
title: 'admin/editor.newsletter.hideLabel',
default: false,
isLayout: true,
},
},
}
}
export default compose(
graphql(SUBSCRIBE_NEWSLETTER, { name: 'subscribeNewsletter' }),
withCssHandles(CSS_HANDLES),
injectIntl
)(Newsletter)