-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcoupon-codes.jsx
More file actions
241 lines (211 loc) · 6.61 KB
/
coupon-codes.jsx
File metadata and controls
241 lines (211 loc) · 6.61 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* @flow */
// libs
import React, { Component } from 'react';
import { autobind } from 'core-decorators';
import { connect } from 'react-redux';
// components
import ContentBox from 'components/core/content-box';
import RadioButton from 'components/core/radio-button';
import Counter from 'components/core/counter';
import FormField from '../../forms/formfield';
import CodeCreationModal from './code-creation-modal';
import { transitionTo } from 'browserHistory';
// styles
import styles from './styles.css';
// redux
import * as actions from 'modules/coupons/details';
type Props = {
isNew: boolean,
isValid: boolean,
refresh: Function,
codeGeneration: Object,
promotionId: Number,
coupon: Object,
createCoupon: Function,
couponsGenerationSelectBulk: Function,
couponsGenerationSelectSingle: Function,
generateCodes: Function,
persistCoupon: Function,
couponsGenerationShowDialog: Function,
couponsGenerationHideDialog: Function,
couponsGenerationChange: Function,
couponsGenerationReset: Function,
codeIsOfValidLength: Function,
};
type Target = {
name: string,
value: string,
};
class CouponCodes extends Component {
props: Props;
get singleCouponFormPart() {
if (this.props.codeGeneration.bulk !== false) {
return null;
}
return (
<div styleName="form-subset">
<div className="fc-form-field" styleName="form-group">
<input
type="text"
styleName="full-width-field"
name="singleCode"
value={this.props.codeGeneration.singleCode}
onChange={this.handleFormChange}
/>
<div styleName="field-comment">
Coupon codes must be unique and are <i>not</i> case sensative.
</div>
</div>
</div>
);
}
@autobind
handleFormChange({target}: {target: Target}): void {
this.props.couponsGenerationChange(target.name, target.value);
}
@autobind
handleCounterChange(name, num): void {
this.props.couponsGenerationChange(name, num);
}
@autobind
handleGenerateBulkClick(): void {
if (this.codeIsOfValidLength()) {
let willBeCoupon = this.props.isNew ? this.props.createCoupon() : Promise.resolve();
if (willBeCoupon == null) return;
willBeCoupon.then(() => {
this.props.couponsGenerationShowDialog();
});
}
}
@autobind
closeDialog(): void {
this.props.couponsGenerationHideDialog();
}
@autobind
handleConfirmOfCodeGeneration(): void {
const { codesPrefix, codesLength, codesQuantity } = this.props.codeGeneration;
this.props.generateCodes(codesPrefix, codesLength, codesQuantity).then(() => {
this.props.couponsGenerationReset();
}).then(() => {
this.props.refresh();
transitionTo('promotion-coupons', {promotionId: this.props.promotionId});
});
}
codeIsOfValidLength(): boolean {
return this.props.codeIsOfValidLength();
}
get generateCodesDisabled(): boolean {
return !(this.props.codeGeneration.codesPrefix && this.codeIsOfValidLength());
}
get guessProbability(): number {
const quantity = this.props.codeGeneration.codesQuantity;
const length = this.props.codeGeneration.codesLength;
const numberOfVariants = Math.pow(10, length);
return Math.round((quantity / numberOfVariants) * 100);
}
get codeLengthValidationError() {
const message =
'Cannot guarantee uniqueness for the required quantity of codes. Please choose a longer character length.';
return <div className="fc-form-field-error">{message}</div>;
}
get bulkCouponFormPart() {
if (this.props.codeGeneration.bulk !== true) {
return null;
}
const { codesQuantity } = this.props.codeGeneration;
return (
<div styleName="form-subset">
<div styleName="form-group" className="fc-coupon-inline-row">
<FormField label="Quantity">
<div>
<Counter
counterId="fct-codes-quantity-counter"
id="codesQuantity"
name="codesQuantity"
value={codesQuantity}
onChange={(value) => this.handleCounterChange('codesQuantity', value)}
min={1}
/>
</div>
</FormField>
</div>
<div styleName="form-group" className="fc-coupon-inline-row">
<FormField label="Code Prefix">
<div>
<input
styleName="full-width-field"
value={this.props.codeGeneration.codesPrefix}
onChange={this.handleFormChange}
type="text"
name="codesPrefix"
/>
</div>
</FormField>
</div>
<div styleName="form-group" className="fc-coupon-inline-row">
<FormField label="Code Character Length" >
<div>
<Counter
counterId="fct-code-length-counter"
id="codesLength"
name="codesLength"
value={this.props.codeGeneration.codesLength}
onChange={(value) => this.handleCounterChange('codesLength', value)}
min={1}
/>
</div>
</FormField>
{!this.codeIsOfValidLength() && this.codeLengthValidationError}
<div styleName="field-comment">
Excludes prefix
</div>
</div>
</div>
);
}
@autobind
handleSingleSelect(): void {
this.props.couponsGenerationSelectSingle();
}
@autobind
handleBulkSelect(): void {
this.props.couponsGenerationSelectBulk();
}
render() {
return (
<ContentBox title="Coupon Code">
<div>
<RadioButton
id="singleCouponCodeRadio"
label="Single coupon code"
checked={this.props.codeGeneration.bulk === false}
onChange={this.handleSingleSelect}
/>
</div>
{this.singleCouponFormPart}
<div>
<RadioButton
id="bulkCouponCodeRadio"
label="Bulk generate coupon codes"
checked={this.props.codeGeneration.bulk === true}
onChange={this.handleBulkSelect}
/>
</div>
{this.bulkCouponFormPart}
<CodeCreationModal
probability={this.guessProbability}
isVisible={this.props.codeGeneration.isDialogVisible}
cancelAction={this.closeDialog}
confirmAction={this.handleConfirmOfCodeGeneration}
/>
</ContentBox>
);
}
}
const mergeProps = (stateProps, dispatchProps, ownProps) => {
return {
...dispatchProps,
...ownProps
};
};
export default connect(null, actions, mergeProps)(CouponCodes);