-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathform-util.js
More file actions
172 lines (151 loc) · 4.89 KB
/
form-util.js
File metadata and controls
172 lines (151 loc) · 4.89 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
import { $, $$, downloadBlob } from './dom-utils'
import { addSlash, getFormattedDate } from './util'
import pdfBase from '../certificate.pdf'
import { generatePdf } from './pdf-util'
import { setPreviousFormValue } from './localstorage'
const conditions = {
'#field-firstname': {
length: 1,
},
'#field-lastname': {
length: 1,
},
'#field-birthday': {
pattern: /^([0][1-9]|[1-2][0-9]|30|31)\/([0][1-9]|10|11|12)\/(19[0-9][0-9]|20[0-1][0-9]|2020)/g,
},
'#field-placeofbirth': {
length: 1,
},
'#field-address': {
length: 1,
},
'#field-city': {
length: 1,
},
'#field-zipcode': {
pattern: /\d{5}/g,
},
'#field-datesortie': {
pattern: /\d{4}-\d{2}-\d{2}/g,
},
'#field-heuresortie': {
pattern: /\d{2}:\d{2}/g,
},
}
function validateAriaFields () {
return Object.keys(conditions)
.map((field) => {
const fieldData = conditions[field]
const pattern = fieldData.pattern
const length = fieldData.length
const isInvalidPattern = pattern && !$(field).value.match(pattern)
const isInvalidLength = length && !$(field).value.length
const isInvalid = !!(isInvalidPattern || isInvalidLength)
$(field).setAttribute('aria-invalid', isInvalid)
if (isInvalid) {
$(field).focus()
}
return isInvalid
})
.includes(true)
}
export function setReleaseDateTime (releaseDateInput) {
const loadedDate = new Date()
releaseDateInput.value = getFormattedDate(loadedDate)
}
export function getProfile (formInputs) {
const fields = {}
for (const field of formInputs) {
let value = field.value
if (field.id === 'field-datesortie') {
const dateSortie = field.value.split('-')
value = `${dateSortie[2]}/${dateSortie[1]}/${dateSortie[0]}`
}
fields[field.id.substring('field-'.length)] = value
}
return fields
}
export function getReasons (reasonInputs) {
const reasons = reasonInputs
.filter(input => input.checked)
.map(input => input.value).join(', ')
return reasons
}
export function prepareInputs (formInputs, reasonInputs, reasonFieldset, reasonAlert, snackbar) {
formInputs.forEach((input) => {
const exempleElt = input.parentNode.parentNode.querySelector('.exemple')
const validitySpan = input.parentNode.parentNode.querySelector('.validity')
if (input.placeholder && exempleElt) {
input.addEventListener('input', (event) => {
if (input.value) {
exempleElt.innerHTML = 'ex. : ' + input.placeholder
validitySpan.removeAttribute('hidden')
} else {
exempleElt.innerHTML = ''
}
})
}
})
$('#field-birthday').addEventListener('keyup', function (event) {
event.preventDefault()
const input = event.target
const key = event.keyCode || event.charCode
if (key !== 8 && key !== 46) {
input.value = addSlash(input.value)
}
})
reasonInputs.forEach(radioInput => {
radioInput.addEventListener('change', function (event) {
const isInError = reasonInputs.every(input => !input.checked)
reasonFieldset.classList.toggle('fieldset-error', isInError)
reasonAlert.classList.toggle('hidden', !isInError)
})
})
$('#generate-btn').addEventListener('click', async (event) => {
event.preventDefault()
const reasons = getReasons(reasonInputs)
if (!reasons) {
reasonFieldset.classList.add('fieldset-error')
reasonAlert.classList.remove('hidden')
reasonFieldset.scrollIntoView && reasonFieldset.scrollIntoView()
return
}
const invalid = validateAriaFields()
if (invalid) {
return
}
const profile = getProfile(formInputs)
;[
'address',
'birthday',
'city',
'firstname',
'lastname',
'placeofbirth',
'zipcode',
].forEach(inputName => setPreviousFormValue(inputName, profile[inputName]))
const pdfBlob = await generatePdf(profile, reasons, pdfBase)
const creationInstant = new Date()
const creationDate = creationInstant.toLocaleDateString('fr-CA')
const creationHour = creationInstant
.toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit' })
.replace(':', '-')
downloadBlob(pdfBlob, `attestation-${creationDate}_${creationHour}.pdf`)
snackbar.classList.remove('d-none')
setTimeout(() => snackbar.classList.add('show'), 100)
setTimeout(function () {
snackbar.classList.remove('show')
setTimeout(() => snackbar.classList.add('d-none'), 500)
}, 6000)
})
}
export function prepareForm () {
const formInputs = $$('#form-profile input')
const snackbar = $('#snackbar')
const reasonInputs = [...$$('input[name="field-reason"]')]
const reasonFieldset = $('#reason-fieldset')
const reasonAlert = reasonFieldset.querySelector('.msg-alert')
const releaseDateInput = $('#field-datesortie')
setReleaseDateTime(releaseDateInput)
prepareInputs(formInputs, reasonInputs, reasonFieldset, reasonAlert, snackbar)
}