RequestPage.tsx 11.1 KB
Newer Older
Oleksii Savran's avatar
Oleksii Savran committed
1
2
3
4
import React from 'react';

import { withTranslation, WithTranslation } from 'react-i18next';
import { RouteComponentProps } from 'react-router-dom';
Oleksii Savran's avatar
Oleksii Savran committed
5
import ErrorDisplay from 'ui/common/ErrorDisplay';
6
import { RequestService } from '@genesys/client/service';
Oleksii Savran's avatar
Oleksii Savran committed
7
8
9
import { LocalStorageCart } from 'utilities';
import EasySMTAUserData from '@genesys/client/model/request/EasySMTAUserData';
import RequestInfo from '@genesys/client/model/request/RequestInfo';
Oleksii Savran's avatar
Oleksii Savran committed
10
import CaptchaInput from 'ui/common/CaptchaInput';
Oleksii Savran's avatar
Oleksii Savran committed
11
12
13
14
15
16

interface IRequestPageState {
  apiError: string | Error;
  requestInfo: Partial<RequestInfo>;
  userData: Partial<EasySMTAUserData>;
  captcha: string;
Oleksii Savran's avatar
Oleksii Savran committed
17
  success: boolean;
Oleksii Savran's avatar
Oleksii Savran committed
18
19
20
21
22
23
24
25
26
27
}

interface IRequestPageProps extends WithTranslation, RouteComponentProps {}

class RequestPage extends React.Component<IRequestPageProps, IRequestPageState> {
  public constructor(props) {
    super(props);

    this.state = {
      apiError: null,
Oleksii Savran's avatar
Oleksii Savran committed
28
      requestInfo: {},
Oleksii Savran's avatar
Oleksii Savran committed
29
      userData: {
Oleksii Savran's avatar
Oleksii Savran committed
30
        // pid: 'Internal', // for now these values are being added to the request in RequestService.initiateRequest method
Oleksii Savran's avatar
Oleksii Savran committed
31
32
33
        // type: 'in',
      },
      captcha: null,
Oleksii Savran's avatar
Oleksii Savran committed
34
      success: false,
Oleksii Savran's avatar
Oleksii Savran committed
35
36
37
    }
  }

Oleksii Savran's avatar
Oleksii Savran committed
38
  private onRequestDataChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
Oleksii Savran's avatar
Oleksii Savran committed
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
    const { name, value } = e.currentTarget as HTMLInputElement;
    this.setState({ requestInfo: { ...this.state.requestInfo, [name]: value } });
  };

  private onUserDataChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const { name, value } = e.currentTarget as HTMLInputElement;
    this.setState({ userData: { ...this.state.userData, [name]: value } });
  };

  private onSubmit = (e) => {
    e.preventDefault();
    const { t } = this.props;
    const { requestInfo, userData, captcha } = this.state;
    const UUIDs = LocalStorageCart.getCartItemsLS();
    if (UUIDs.length === 0) {
      this.setState({ apiError: t('cart.isEmpty') });
      return;
    }
    let info = { ...requestInfo };
    if (requestInfo.internalRequest) {
      info.userData = userData as EasySMTAUserData;
      info.email = null;
    } else {
      info = { ...requestInfo };
      info.email = userData.email;
      // delete info.internalRequest;
    }
Oleksii Savran's avatar
Oleksii Savran committed
66
    // console.log('submit: ', UUIDs, info);
Oleksii Savran's avatar
Oleksii Savran committed
67
68
69
70
    RequestService
      .initiateRequest(UUIDs, info as RequestInfo, captcha)
      .then((data) => {
        console.log('request submitted', data);
Oleksii Savran's avatar
Oleksii Savran committed
71
72
        LocalStorageCart.clearCart();
        this.setState({ success: true });
Oleksii Savran's avatar
Oleksii Savran committed
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
      })
      .catch((e) => {
        console.log('request failed: ', e);
        this.setState({ apiError: (e.data && e.data.error) || e.statusText || e });
      });
  };

  private onCheckboxChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const { checked } = e.currentTarget as HTMLInputElement;
    this.setState({ requestInfo: { ...this.state.requestInfo, internalRequest: checked } })
  };

  private onPurposeChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const { value } = e.currentTarget as HTMLInputElement;
    this.setState({ requestInfo: { ...this.state.requestInfo, purposeType: +value } })
  };

  private onSMTAChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    const { value } = e.currentTarget as HTMLInputElement;
    this.setState({ requestInfo: { ...this.state.requestInfo, preacceptSMTA: !!+value } });
  };

  private onCaptchaChange = (captchaResponse: string) => {
    if (captchaResponse) {
      this.setState({ captcha: captchaResponse });
    }
  };

  public render() {
    const { t } = this.props;
Oleksii Savran's avatar
Oleksii Savran committed
103
    const { apiError, requestInfo, userData, captcha, success } = this.state;
Oleksii Savran's avatar
Oleksii Savran committed
104
105
106
107
    const { internalRequest } = requestInfo;
    const getValueRequest = (name: string) => requestInfo[name] || '';
    const getValueUser = (name: string) => userData[name] || '';

Oleksii Savran's avatar
Oleksii Savran committed
108
109
110
111
112
113
114
115
116
    if (success) {
      return (
        <div>
          <p>{ t('request.successMessage') }</p>
          <p>{ t('request.checkEmail') }</p>
        </div>
      );
    }

Oleksii Savran's avatar
Oleksii Savran committed
117
118
119
120
    return (
      <>
        <h1>{ t('request.title') }</h1>
        <form onSubmit={ this.onSubmit }>
Oleksii Savran's avatar
Oleksii Savran committed
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
          <div className="form-group">
            <label htmlFor="email">
              { t(`${internalRequest ? 'request.userData' : 'request.requestInfo'}.email`) }
            </label>
            <input
              id="email"
              className="form-control"
              name="email"
              type="email"
              onChange={ this.onUserDataChange }
              value={ getValueUser('email') }
              required
            />
          </div>
          <div className="form-group">
            <p><strong>{ t('request.internalExplanation') }</strong></p>
            <div className="form-check">
              <input
                id="internalRequest"
                className="form-check-input"
                name="internalRequest"
                type="checkbox"
                onChange={ this.onCheckboxChange }
                checked={ !!internalRequest }
              />
              <label htmlFor="internalRequest" className="form-check-label">
                { t('request.requestInfo.internal') }
              </label>
            </div>
          </div>
Oleksii Savran's avatar
Oleksii Savran committed
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
          { internalRequest && (
            <>
              <div className="form-group">
                <label htmlFor="name">{ t('request.userData.name') }</label>
                <input
                  className="form-control"
                  id="name"
                  name="name"
                  type="text"
                  onChange={ this.onUserDataChange }
                  value={ getValueUser('name') }
                  required
                />
              </div>
              <div className="form-group">
                <label htmlFor="surname">{ t('request.userData.surname') }</label>
                <input
                  id="surname"
                  name="surname"
                  className="form-control"
                  type="text"
                  onChange={ this.onUserDataChange }
                  value={ getValueUser('surname') }
                  required
                />
              </div>
            </>
          ) }
          { internalRequest && (
            <>
              <div className="form-group">
                <label htmlFor="address">{ t('request.userData.address') }</label>
                <input
                  id="address"
                  className="form-control"
                  name="address"
                  type="text"
                  onChange={ this.onUserDataChange }
                  value={ getValueUser('address') }
                  required
                />
              </div>
              <div className="form-group">
                <label htmlFor="country">{ t('request.userData.country') }</label>
                <input
                  id="country"
                  className="form-control"
                  name="country"
                  type="text"
                  onChange={ this.onUserDataChange }
                  value={ getValueUser('country') }
                  required
                />
              </div>
              <div className="form-group">
                <label htmlFor="telephone">{ t('request.userData.telephone') }</label>
                <input
                  id="telephone"
                  className="form-control"
                  name="telephone"
                  type="text"
                  onChange={ this.onUserDataChange }
                  value={ getValueUser('telephone') }
                />
              </div>
              <div className="form-group">
                <label htmlFor="orgName">{ t('request.userData.orgName') }</label>
                <input
                  id="orgName"
                  className="form-control"
                  name="orgName"
                  type="text"
                  onChange={ this.onUserDataChange }
                  value={ getValueUser('orgName') }
                />
              </div>
            </>
          ) }

          { !internalRequest && (
            <div className="form-group">
              <div>{ t('request.requestInfo.preacceptSMTA.label') }</div>
              <div className="form-check">
                <input
                  id="preacceptSMTAYes"
                  name="preacceptSMTA"
                  type="radio"
                  className="form-check-input"
                  value="1"
                  onChange={ this.onSMTAChange }
                  checked={ requestInfo.preacceptSMTA === true } // there is no default value
Oleksii Savran's avatar
Oleksii Savran committed
242
                  required
Oleksii Savran's avatar
Oleksii Savran committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256
                />
                <label htmlFor="preacceptSMTAYes" className="form-check-label">
                  { t('request.requestInfo.preacceptSMTA.yes') }
                </label>
              </div>
              <div className="form-check">
                <input
                  id="preacceptSMTANo"
                  name="preacceptSMTA"
                  type="radio"
                  className="form-check-input"
                  value="0"
                  onChange={ this.onSMTAChange }
                  checked={ requestInfo.preacceptSMTA === false }
Oleksii Savran's avatar
Oleksii Savran committed
257
                  required
Oleksii Savran's avatar
Oleksii Savran committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
                />
                <label htmlFor="preacceptSMTANo" className="form-check-label">
                  { t('request.requestInfo.preacceptSMTA.no') }
                </label>
              </div>
            </div>
          ) }

          <div className="form-group">
            <div>{ t('request.requestInfo.purposeType.label') }</div>
            <div className="form-check">
              <input
                id="purposeType0"
                name="purposeType"
                type="radio"
                className="form-check-input"
                value="0"
                onChange={ this.onPurposeChange }
                checked={ requestInfo.purposeType === 0 }
Oleksii Savran's avatar
Oleksii Savran committed
277
                required
Oleksii Savran's avatar
Oleksii Savran committed
278
279
280
281
282
283
284
285
286
287
288
289
              />
              <label htmlFor="purposeType0" className="form-check-label">{ t('request.requestInfo.purposeType.0') }</label>
            </div>
            <div className="form-check">
              <input
                id="purposeType1"
                name="purposeType"
                type="radio"
                className="form-check-input"
                value="1"
                onChange={ this.onPurposeChange }
                checked={ requestInfo.purposeType === 1 }
Oleksii Savran's avatar
Oleksii Savran committed
290
                required
Oleksii Savran's avatar
Oleksii Savran committed
291
292
293
294
295
296
297
              />
              <label htmlFor="purposeType1" className="form-check-label">{ t('request.requestInfo.purposeType.1') }</label>
            </div>
          </div>

          <div className="form-group">
            <label htmlFor="notes"><strong>{ t('request.requestInfo.notes') }</strong></label>
Oleksii Savran's avatar
Oleksii Savran committed
298
            <textarea
Oleksii Savran's avatar
Oleksii Savran committed
299
300
301
302
303
              id="notes"
              className="form-control"
              name="notes"
              onChange={ this.onRequestDataChange }
              value={ getValueRequest('notes') }
Oleksii Savran's avatar
Oleksii Savran committed
304
              required
Oleksii Savran's avatar
Oleksii Savran committed
305
306
            />
          </div>
307
          <CaptchaInput onChange={ this.onCaptchaChange } />
Oleksii Savran's avatar
Oleksii Savran committed
308
309
310
311
312
313
314
315
316
          { apiError && <ErrorDisplay error={ apiError }/> }
          <button className="btn btn-primary mt-3" type="submit" disabled={ !captcha }>{ t('request.submit') }</button>
        </form>
      </>
    );
  };
}

export default withTranslation()(RequestPage)