UNPKG

6.26 kBJavaScriptView Raw
1import React from 'react'
2
3import { modes as modeConstants } from '../data/constants'
4
5export const modeToLabels = {
6 GIFT_OR_SUB: 'Gift or subcribers',
7 SUB_ONLY: 'Subscribers only',
8 FREE: 'Free'
9}
10
11const customMessageToLabels = {
12 true: 'True (on)',
13 false: 'False (off)'
14}
15
16const apiConstants = {
17 CREDIT_INFO: 'Credit info',
18 GIFT: 'Gift',
19 NON_GIFT: 'Non-gift'
20}
21
22const apiConstantToFunctionNames = {
23 CREDIT_INFO: 'creditInfo',
24 GIFT: 'gift',
25 NON_GIFT: 'nonGift'
26}
27
28const responseConstants = {
29 GOOD_RESPONSE: 'Good response',
30 UNEXPECTED_RESPONSE: 'Unexpected response',
31 ERROR: 'Error'
32}
33
34export default class extends React.Component {
35
36 constructor (props) {
37 super(props)
38 const state = {}
39 // set defaults
40 state.mode = Object.keys(modeConstants)[0]
41 state.customMessage = false
42 state.credit = 10
43 state.monthlyAllowance = 10
44 state.responseTime = 0
45 Object.keys(apiConstants).forEach(api => {
46 state[api] = Object.keys(responseConstants)[0]
47 })
48 this.state = state
49
50 this.update()
51 }
52
53 getResponse (api, responseType, credit, monthlyAllowance, responseTime) {
54 let json
55 switch (responseConstants[responseType]) {
56 case responseConstants.GOOD_RESPONSE:
57 if (apiConstants[api] === apiConstants.CREDIT_INFO)
58 json = { credits: { allowance: monthlyAllowance, remainingCredits: credit }}
59 else
60 json = {results: [{recipient: 'test-forward@ftqa.org', success: true, message: 'ok'}]}
61 return () => new Promise((resolve) => setTimeout(() => resolve({json: () => json}), responseTime))
62
63 case responseConstants.UNEXPECTED_RESPONSE:
64 json = {error: 'oops'}
65 return () => new Promise((resolve) => setTimeout(() => resolve({json: () => json}), responseTime))
66
67 case responseConstants.ERROR:
68 return () => new Promise((resolve, reject) => setTimeout(() => reject(), responseTime))
69
70 }
71 }
72
73 createMockApis () {
74 const apiResponses = {}
75 Object.keys(apiConstants).forEach(api => {
76 apiResponses[apiConstantToFunctionNames[api]] = this.getResponse(api, this.state[api], this.state.credit, this.state.monthlyAllowance, this.state.responseTime)
77 })
78 return apiResponses
79 }
80
81 onApiResponseChange (api, response) {
82 const partialState = {}
83 partialState[api] = response
84 this.setState(partialState, () => {
85 this.update()
86 })
87 }
88
89 onCreditChange (credit) {
90 this.setState({credit: credit}, () => {
91 this.update()
92 })
93 }
94
95 onMonthlyAllowanceChange (allowance) {
96 this.setState({monthlyAllowance: allowance}, () => {
97 this.update()
98 })
99 }
100
101 onResponseTimeChange (time) {
102 this.setState({responseTime: time}, () => {
103 // update the mock functions
104 this.update()
105 })
106 }
107
108 onModeChange (mode) {
109 this.setState({mode: mode}, () => {
110 this.update()
111 })
112 }
113
114 onCustomMessageOptionChange (customMessageOption) {
115 this.setState({customMessage: customMessageOption}, () => {
116 this.update();
117 });
118 }
119
120 update () {
121 this.props.onConfigChange(this.state.mode, this.createMockApis(), this.state.customMessage)
122 }
123
124 render () {
125
126 const modeOptions = Object.keys(modeConstants).map(mode => {
127 const id = `demos__mode--${mode}`
128 return (
129 <div key={mode} className="o-forms-group">
130 <input type="radio" className="o-forms-radio" id={id}
131 checked={this.state.mode === mode}
132 onChange={() => this.onModeChange(mode)}/>
133 <label className="o-forms-label" htmlFor={id}>
134 {modeToLabels[mode]}
135 </label>
136 </div>
137 )
138 })
139
140 const modes = (
141 <div>
142 <h3>Article type</h3>
143 {modeOptions}
144 </div>
145 )
146
147 const customMessageOptions = [true, false].map(customMessageOption => {
148 const id = `demos__custom-message--${customMessageOption}`
149 return (
150 <div key={customMessageOption} className="o-forms-group">
151 <input type="radio" className="o-forms-radio" id={id}
152 checked={this.state.customMessage === customMessageOption}
153 onChange={() => this.onCustomMessageOptionChange(customMessageOption)}/>
154 <label className="o-forms-label" htmlFor={id}>
155 {customMessageToLabels[customMessageOption]}
156 </label>
157 </div>
158 )
159 })
160
161 const customMessage = (
162 <div>
163 <h3>Custom Message flag</h3>
164 {customMessageOptions}
165 </div>
166 )
167
168 const apiResponses = Object.keys(apiConstants).map(api => {
169 const responses = Object.keys(responseConstants).map(response => {
170 const id = `demos__${api}--${response}`
171 return (
172 <div key={id} className="o-forms-group">
173 <input type="radio" className="o-forms-radio" id={id}
174 checked={this.state[api] === response}
175 onChange={() => this.onApiResponseChange(api, response)}/>
176 <label className="o-forms-label" htmlFor={id}>
177 {responseConstants[response]}
178 </label>
179 </div>
180 )
181 })
182 return (
183 <div key={api}>
184 <h3>{apiConstants[api]}</h3>
185 {responses}
186 </div>
187 )
188 })
189
190 const credit = (
191 <div className="o-forms-group">
192 <label htmlFor="demos__credit" className="o-forms-label">Gift credit</label>
193 <input type="number" className="o-forms-text" id="demos__credit"
194 value={this.state.credit} onChange={(event) => this.onCreditChange(parseInt(event.target.value, 10))}></input>
195 </div>
196 )
197
198 const monthlyAllowance = (
199 <div className="o-forms-group">
200 <label htmlFor="demos__allowance" className="o-forms-label">Monthly credit allowance</label>
201 <input type="number" className="o-forms-text" id="demos__allowance"
202 value={this.state.monthlyAllowance} onChange={(event) => this.onMonthlyAllowanceChange(parseInt(event.target.value, 10))}></input>
203 </div>
204 )
205
206 const responseTime = (
207 <div className="o-forms-group">
208 <label htmlFor="demos__response-time" className="o-forms-label">Response time (ms)</label>
209 <input type="number" className="o-forms-text" id="demos__response-time"
210 value={this.state.responseTime} onChange={(event) => this.onResponseTimeChange(parseInt(event.target.value, 10))}></input>
211 </div>
212 )
213
214 return (
215 <div>
216 <h2 className="demos__config-title">Configure demo</h2>
217 <p><em>See in JS console for all the tracking that happens</em></p>
218 {modes}
219 {customMessage}
220 {credit}
221 {monthlyAllowance}
222 {responseTime}
223 {apiResponses}
224 </div>
225 )
226
227 }
228
229}