-
-
Notifications
You must be signed in to change notification settings - Fork 126
/
RequestGeneratorBuilder.js
503 lines (447 loc) · 20.4 KB
/
RequestGeneratorBuilder.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
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
import preact from 'preact';
import { IntlProvider, MarkupText } from 'preact-i18n';
import t, { t_r } from '../Utility/i18n';
import Request from '../DataType/Request';
import { defaultFields, trackingFields, REQUEST_ARTICLES, initializeFields, fetchTemplate } from '../Utility/requests';
import RequestLetter from '../Utility/RequestLetter';
import { slugify, PARAMETERS } from '../Utility/common';
import IdData, { ID_DATA_CHANGE_EVENT, ID_DATA_CLEAR_EVENT } from '../Utility/IdData';
import replacer_factory from '../Utility/request-generator-replacers';
import { fetchCompanyDataBySlug } from '../Utility/companies';
import Privacy, { PRIVACY_ACTIONS } from '../Utility/Privacy';
import Modal, { showModal, dismissModal } from './Modal';
import SvaFinder from './SvaFinder';
import { download, clearUrlParameters } from '../Utility/browser';
import Template from 'letter-generator/Template';
import UserRequests from '../my-requests';
export default class RequestGeneratorBuilder extends preact.Component {
constructor(props) {
super(props);
this.state = {
request: new Request(),
suggestion: null,
template_text: '',
batch: [],
blob_url: '',
download_filename: '',
download_active: false,
response_type: '',
response_request: {},
fill_fields: [],
fill_signature: null
};
// If specified in the URL, load a single company…
if (PARAMETERS['company']) this.setCompanyBySlug(PARAMETERS['company']);
// …or multiple ones.
const batch_companies = PARAMETERS['companies'];
if (batch_companies) this.state.batch = batch_companies.split(',');
this.letter = new RequestLetter({}, (blob_url, filename) => {
this.setState({
blob_url: blob_url,
download_filename: filename,
download_active: true
});
});
if (Privacy.isAllowed(PRIVACY_ACTIONS.SAVE_ID_DATA)) this.idData = new IdData();
this.resetInitialConditions();
}
resetInitialConditions = () => {
// We always need to initialize the fields…
initializeFields(this.state.request.id_data).then(res => {
this.setState(prev => {
// …if they haven't already been initialized elsewhere.
if (!this.state.request.id_data) {
prev.request.id_data = res.new_fields;
prev.request.signature = res.signature;
}
const name = res.new_fields.filter(i => i.type === 'name')[0];
const address = res.new_fields.filter(i => i.type === 'address')[0];
if (name) prev.request.custom_data.name = name.value;
if (address) prev.request.custom_data.sender_address = address.value;
return prev;
});
});
const response_to = PARAMETERS['response_to'];
const response_type = PARAMETERS['response_type'];
// We are in batch mode, move to the next company.
if (this.state.batch && this.state.batch.length > 0) this.setCompanyBySlug(this.state.batch.shift());
// This is a response to a previous request (warning or complaint).
else if (Privacy.isAllowed(PRIVACY_ACTIONS.SAVE_MY_REQUESTS) && response_to && response_type) {
new UserRequests().getRequest(response_to).then(request => {
fetchTemplate(this.state.request.language, response_type, null, '').then(text => {
this.setState(prev => {
prev.request.custom_data.content = new Template(text, [], {
request_article: REQUEST_ARTICLES[request.type],
request_date: request.date,
request_recipient_address: request.recipient
}).getText();
if (response_type === 'admonition') {
// This might be useful in the future event though it is not used now. Looking forward to a conversations feature!
prev.request.via = request.via;
prev.request.recipient_address = request.recipient;
}
prev.request.reference = request.reference;
prev.response_type = response_type;
prev.request.type = 'custom';
prev.response_request = request;
return prev;
});
if (response_type === 'admonition' && request.slug) this.setCompanyBySlug(request.slug);
this.renderLetter();
});
});
if (response_type === 'complaint') this.showAuthorityChooser();
}
// This is just a regular ol' request.
else {
fetchTemplate(this.state.request.language, 'access').then(text => {
this.setState({ template_text: text });
this.renderLetter();
});
}
};
componentDidMount() {
if (Privacy.isAllowed(PRIVACY_ACTIONS.SAVE_ID_DATA)) {
const callback = () => {
this.idData.getAll(false).then(fill_fields => this.setState({ fill_fields: fill_fields }));
this.idData.getSignature().then(fill_signature => this.setState({ fill_signature: fill_signature }));
};
window.addEventListener(ID_DATA_CHANGE_EVENT, callback);
window.addEventListener(ID_DATA_CLEAR_EVENT, callback);
}
}
render() {
const replacers = replacer_factory(this);
const children_mapper = c =>
c.nodeName && Object.keys(replacers).includes(c.nodeName.name)
? replacers[c.nodeName.name](c)
: Array.isArray(c.children)
? { ...c, children: c.children.map(children_mapper) }
: c;
const children = this.props.children.map(children_mapper);
return (
<IntlProvider scope="generator" definition={I18N_DEFINITION}>
<div>{children}</div>
</IntlProvider>
);
}
startBatch = companies => {
this.setState({ batch: companies });
if (this.state.batch && this.state.batch.length > 0) this.setCompanyBySlug(this.state.batch.shift());
};
setCompanyBySlug = slug => {
fetchCompanyDataBySlug(slug, company => {
this.setCompany(company);
});
};
setCompany = company => {
fetchTemplate(company['request-language'], this.state.request.type, company).then(text => {
this.setState({ template_text: text });
this.renderLetter();
});
// I would love to have `Request` handle this. But unfortunately that won't work as Preact won't notice the
// state has changed. :(
this.setState(prev => {
prev.request.transport_medium = company['suggested-transport-medium']
? company['suggested-transport-medium']
: company.fax
? 'fax'
: 'letter';
prev.request.recipient_address =
company.name +
(PARAMETERS.response_type !== 'complaint'
? '\n' + t_r('attn', company['request-language'] || LOCALE)
: '') +
'\n' +
company.address +
(prev.request.transport_medium === 'fax'
? '\n' + t_r('by-fax', company['request-language'] || LOCALE) + company['fax']
: '');
const language =
!!company['request-language'] && company['request-language'] !== ''
? company['request-language']
: LOCALE;
// This is not the most elegant thing in the world, but we need to support 'no ID data' requests for
// more than adtech companies. Ideally, this would be another bool in the schema but we can't really
// change that right now because of Typesense. Thus, we have to stick to matching the template for now.
// And I have realized that our current adtech case also applies to pretty much all other 'no ID data'
// requests anyway in that they are either also to tracking companies or those companies at least
// identify the user by the same details (i.e. cookie IDs, device IDs, etc.)
// I couldn't come up with a better name, so we'll just leave them as tracking requests, I guess…
// TODO: Get rid of the `.txt` replacement.
prev.request.is_tracking_request = [
'access-tracking',
'erasure-tracking',
'rectification-tracking'
].includes((company['custom-' + this.state.request.type + '-template'] || '').replace(/\.txt$/, ''));
prev.request.id_data = IdData.mergeFields(
prev.request.id_data,
!!company['required-elements'] && company['required-elements'].length > 0
? company['required-elements']
: prev.request.is_tracking_request
? trackingFields(language)
: defaultFields(language)
);
prev.request.recipient_runs = company.runs || [];
prev.suggestion = company;
prev.request.data_portability = company['suggested-transport-medium'] === 'email';
prev.request.language = company['request-language'] || LOCALE;
return prev;
});
};
handleTypeChange = e => {
this.handleInputChange({ type: e.target.value });
if (e.target.value === 'custom') {
this.letter.clearProps();
return;
}
fetchTemplate(this.state.request.language, this.state.request.type, this.state.suggestion).then(text => {
this.setState({ template_text: text });
this.renderLetter();
});
};
handleInputChange = changed_data => {
this.setState(prev => {
for (const key in changed_data) {
prev.request[key] = changed_data[key];
}
return prev;
});
this.renderLetter();
};
handleTransportMediumChange = e => {
const by_fax_text = t_r('by-fax', this.state.request.language);
this.setState(prev => {
prev.request.transport_medium = e.target.value;
switch (e.target.value) {
case 'fax':
if (prev.suggestion && !prev.request.recipient_address.includes(by_fax_text)) {
prev.request.recipient_address += '\n' + by_fax_text + (prev.suggestion.fax || '');
}
break;
case 'letter': // fallthrough intentional
case 'email':
prev.request.recipient_address = prev.request.recipient_address.replace(
new RegExp('(?:\\r\\n|\\r|\\n)' + by_fax_text + '\\+?[0-9\\s]*', 'gm'),
''
);
break;
}
prev.request.data_portability = e.target.value === 'email';
return prev;
});
this.renderLetter();
};
handleCustomLetterPropertyChange = (e, address_change = false) => {
if (address_change) {
this.setState(prev => {
const att = e.target.getAttribute('name');
prev.request.custom_data.sender_address[att] = e.target.value;
return prev;
});
} else {
this.setState(prev => {
const att = e.target.getAttribute('name');
if (Object.prototype.hasOwnProperty.call(prev.request.custom_data, att))
prev.request.custom_data[att] = e.target.value;
return prev;
});
}
this.renderLetter();
};
handleCustomLetterTemplateChange = e => {
const new_template = e.target.value;
if (new_template !== 'no-template') {
fetchTemplate(this.state.request.language, new_template, null, '').then(text => {
this.setState(prev => {
prev.request.custom_data.content = text;
prev.response_type = new_template;
return prev;
});
this.renderLetter();
});
} else this.setState({ response_type: '' });
};
handleAutocompleteSelected = (e, suggestion, dataset) => {
if (this.state.suggestion) {
const modal = showModal(
<Modal
positiveText={t('new-request', 'generator')}
negativeText={t('override-request', 'generator')}
onNegativeFeedback={e => {
dismissModal(modal);
this.setCompany(suggestion.document);
this.renderLetter();
}}
onPositiveFeedback={e => {
dismissModal(modal);
this.newRequest();
this.setCompany(suggestion.document);
this.renderLetter();
}}
positiveDefault={true}
onDismiss={() => dismissModal(modal)}>
{t('modal-autocomplete-new-request', 'generator')}
</Modal>
);
} else {
this.setCompany(suggestion.document);
this.renderLetter();
}
};
showAuthorityChooser = () => {
const modal = showModal(
<Modal
negativeText={t('cancel', 'generator')}
onNegativeFeedback={() => dismissModal(modal)}
positiveDefault={true}
onDismiss={() => dismissModal(modal)}>
<IntlProvider scope="generator" definition={I18N_DEFINITION}>
<MarkupText id="modal-select-authority" />
</IntlProvider>
<SvaFinder
callback={sva => {
this.setCompany(sva);
fetchTemplate(sva['complaint-language'], 'complaint', null, '').then(text => {
this.setState(prev => {
prev.request.custom_data.content = new Template(text, [], {
request_article: REQUEST_ARTICLES[this.state.response_request.type],
request_date: this.state.response_request.date,
request_recipient_address: this.state.response_request.recipient
}).getText();
});
this.renderLetter();
});
dismissModal(modal);
}}
style="margin-top: 15px;"
/>
</Modal>
);
};
renderLetter = () => {
const sender = this.state.request.custom_data.sender_address;
const sender_address = [
this.state.request.custom_data.name,
sender.street_1,
sender.street_2,
sender.place,
sender.country
];
if (this.state.request.type === 'custom') {
this.letter.setProps({
subject: this.state.request.custom_data.subject,
content: this.state.request.custom_data.content,
signature: { ...this.state.request.signature, name: this.state.request.custom_data.name },
recipient_address: this.state.request.recipient_address,
sender_address: sender_address,
information_block: RequestLetter.makeInformationBlock(this.state.request),
reference_barcode: RequestLetter.barcodeFromText(this.state.request.reference)
});
} else this.letter.setProps(RequestLetter.propsFromRequest(this.state.request, this.state.template_text));
switch (this.state.request.transport_medium) {
case 'fax': // fallthrough intentional
case 'letter':
this.setState({ download_active: false });
this.letter.initiatePdfGeneration(
(this.state.suggestion !== null
? this.state.suggestion.slug
: slugify(this.state.request.recipient_address.split('\n', 1)[0] || 'custom-recipient')) +
'_' +
this.state.request.type +
'_' +
this.state.request.reference +
'.pdf'
);
break;
case 'email': {
this.setState({
blob_url: URL.createObjectURL(
new Blob([this.letter.toEmailString(true)], {
type: 'text/plain'
})
)
});
break;
}
}
};
newRequest() {
if (this.props.newRequestHook) this.props.newRequestHook(this);
// Remove GET parameter-selected company from the URL after the request is finished.
// Also remove warning and complaint GET parameters from the URL after the request is finished.
if (PARAMETERS['company'] || PARAMETERS['response_type'] || PARAMETERS['response_to']) {
clearUrlParameters();
}
this.setState(prev => {
prev.request = new Request();
prev.request.done = false;
prev.suggestion = null;
prev.download_active = false;
prev.blob_url = '';
prev.download_filename = '';
prev.response_type = '';
return prev;
});
this.resetInitialConditions();
}
confirmNewRequest = () => {
const medium = this.state.request.transport_medium;
const modal = showModal(
<Modal
positiveText={[
t(medium === 'email' ? 'send-email-first' : 'download-pdf-first', 'generator'),
<span
style="margin-left: 10px;"
className={'icon icon-' + (medium === 'email' ? 'email' : 'download')}
/>
]}
negativeText={t('new-request', 'generator')}
onNegativeFeedback={e => {
dismissModal(modal);
this.newRequest();
}}
onPositiveFeedback={e => {
if (this.state.blob_url) {
dismissModal(modal);
this.storeRequest();
download(
medium === 'email'
? this.letter.toMailtoLink(this.state.suggestion && this.state.suggestion.email)
: this.state.blob_url,
this.state.download_filename
);
this.newRequest();
}
}}
positiveDefault={true}
onDismiss={() => dismissModal(modal)}>
{t('modal-new-request', 'generator')}
</Modal>
);
};
storeRequest = () => {
if (Privacy.isAllowed(PRIVACY_ACTIONS.SAVE_ID_DATA)) {
this.idData.storeArray(this.state.request.id_data);
this.idData.storeSignature(this.state.request.signature);
}
this.state.request.store(
this.state.suggestion ? this.state.suggestion.slug : undefined,
this.state.response_type
);
};
static get defaultProps() {
return {};
}
}
// If we need to add more placeholders in the future, their names also need to be added to the Webpack MinifyPlugin's
// mangle exclude list.
export class ActionButtonPlaceholder extends preact.Component {}
export class NewRequestButtonPlaceholder extends preact.Component {}
export class CompanySelectorPlaceholder extends preact.Component {}
export class RequestFormPlaceholder extends preact.Component {}
export class DynamicInputContainerPlaceholder extends preact.Component {}
export class SignatureInputPlaceholder extends preact.Component {}
export class RequestTypeChooserPlaceholder extends preact.Component {}
export class RecipientInputPlaceholder extends preact.Component {}
export class TransportMediumChooserPlaceholder extends preact.Component {}