RequestPage.tsx 11.1 KB
Newer Older
Oleksii Savran's avatar
Oleksii Savran committed
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import React from 'react';

import { withTranslation, WithTranslation } from 'react-i18next';
import { RouteComponentProps } from 'react-router-dom';
import ErrorDisplay from 'ui/ErrorDisplay';
import { ApiInfoService, RequestService } from '@genesys/client/service';
import { LocalStorageCart } from 'utilities';
import EasySMTAUserData from '@genesys/client/model/request/EasySMTAUserData';
import RequestInfo from '@genesys/client/model/request/RequestInfo';
import CaptchaInput from './CaptchaInput';

interface IRequestPageState {
  apiError: string | Error;
  requestInfo: Partial<RequestInfo>;
  userData: Partial<EasySMTAUserData>;
  captcha: string;
  captchaSiteKey: string;
}

interface IRequestPageProps extends WithTranslation, RouteComponentProps {}

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

    this.state = {
      apiError: null,
      requestInfo: { internalRequest: true },
      userData: {
        // pid: 'Internal',
        // type: 'in',
      },
      captcha: null,
      captchaSiteKey: null,
    }
  }

  public componentDidMount() {
    ApiInfoService
      .apiInfo()
      .then((data) => {
        if (data.captchaSiteKey) {
          return this.setState({ captchaSiteKey: data.captchaSiteKey });
        }
        this.setState({ apiError: 'Api info call failed' });
      })
      .catch((e) => {
        console.log('Api info call failed: ', e);
        this.setState({ apiError: 'Api info call failed' });
      });
  }

  private onRequestDataChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    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;
    }
    console.log('submit: ', UUIDs, info);
    RequestService
      .initiateRequest(UUIDs, info as RequestInfo, captcha)
      .then((data) => {
        console.log('request submitted', data);
        // clear cart
      })
      .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;
    const { apiError, requestInfo, userData, captcha, captchaSiteKey } = this.state;
    const { internalRequest } = requestInfo;
    const getValueRequest = (name: string) => requestInfo[name] || '';
    const getValueUser = (name: string) => userData[name] || '';

    return (
      <>
        <h1>{ t('request.title') }</h1>
        <form onSubmit={ this.onSubmit }>
          { 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>
            </>
          ) }
          <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') }
            />
          </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
                />
                <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 }
                />
                <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 }
              />
              <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 }
              />
              <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>
            <input
              id="notes"
              className="form-control"
              name="notes"
              type="text"
              onChange={ this.onRequestDataChange }
              value={ getValueRequest('notes') }
            />
          </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>
          <CaptchaInput onChange={ this.onCaptchaChange } captchaClientKey={ captchaSiteKey }/>
          { apiError && <ErrorDisplay error={ apiError }/> }
          <button className="btn btn-primary mt-3" type="submit" disabled={ !captcha }>{ t('request.submit') }</button>
        </form>
      </>
    );
  };
}

export default withTranslation()(RequestPage)