Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enregistrer et restaurer automatiquement les données du formulaire (avec chiffrement) #58

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"bootstrap": "^4.5.3",
"pdf-lib": "^1.11.2",
"qrcode": "^1.4.4",
"remove-accents": "^0.4.2"
"remove-accents": "^0.4.2",
"secure-ls": "^1.2.6"
},
"browserslist": [
"last 5 versions"
Expand Down
12 changes: 10 additions & 2 deletions src/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ p {
transform: translateY(-2px);
}

#form-profile #formgroup-storedata {
user-select: none;
}

@media (prefers-color-scheme: dark) {
#form-profile .form-radio-label .form-check-label {
color: #ddd;
Expand Down Expand Up @@ -352,6 +356,10 @@ input[type=number] {
border-radius: 0.5em;
}

.delete-data-link {
text-align: center;
}

.btn-attestation:hover {
background-color: #3031C1;
}
Expand Down Expand Up @@ -581,7 +589,7 @@ input[type=number] {
}
}

#snackbar {
#snackbar, #snackbar-cleardata {
min-width: 250px;
color: #fff;
text-align: center;
Expand All @@ -598,7 +606,7 @@ input[type=number] {
transition: all 0.5s ease-in-out;
}

#snackbar.show {
#snackbar.show, #snackbar-cleardata.show {
opacity: 1;
}

Expand Down
41 changes: 34 additions & 7 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ <h1 class="flex flex-wrap">
</span>
</h1>
<p class="text-desc">
En application du décret n°2020-1310 du 29 octobre 2020 prescrivant les mesures générales
nécessaires pour faire face à l'épidémie de Covid19 dans le cadre de l'état d'urgence sanitaire
En application du décret n°2020-1310 du 29 octobre 2020 prescrivant les mesures générales
nécessaires pour faire face à l'épidémie de Covid19 dans le cadre de l'état d'urgence sanitaire
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
nécessaires pour faire face à l'épidémie de Covid19 dans le cadre de l'état d'urgence sanitaire
nécessaires pour faire face à lépidémie de Covid19 dans le cadre de létat d'urgence sanitaire

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Je pense que cette suggestion de modification de caractère mérite une PR séparée car il n'y a pas de lien avec la fonctionnalité apportée par l'actuelle.

</p>

</div>
Expand All @@ -59,16 +59,43 @@ <h1 class="flex flex-wrap">
id="alert-facebook"
></p>

<div class="wrapper">
<form id="form-profile" accept-charset="UTF-8"></form>
<p class="text-center mt-5">
<button type="button" id="generate-btn" class="btn btn-primary btn-attestation"><span ><i class="fa fa-file-pdf inline-block mr-1"></i> Générer mon attestation</span></button>
</p>
<div class="wrapper">
<form id="form-profile" accept-charset="UTF-8"></form>
<p class="text-center mt-5">
<button type="button" id="generate-btn" class="btn btn-primary btn-attestation">
<span>
<i class="fa fa-file-pdf inline-block mr-1"></i>
Générer mon attestation
</span>
</button>
</p>

<div class="bg-primary d-none" id="snackbar">
L'attestation est téléchargée sur votre appareil.
</div>

<div id="formgroup-storedata"
class="form-check text-center">
<input class="form-check-input"
type="checkbox"
name="storedata"
id="field-storedata"
value="storedata">
<label class="form-check-label"
for="field-storedata">
Stocker mes données sur l'appareil afin de remplir automatiquement mes futures attestations
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Stocker mes données sur l'appareil afin de remplir automatiquement mes futures attestations
Stocker mes données sur lappareil afin de remplir automatiquement mes futures attestations

</label>
</div>

<a id="cleardata"
class="center delete-data-link"
href="javascript:void(0)">Effacer les données du formulaire</a>
<div class="bg-primary d-none"
id="snackbar-cleardata">
Les données du formulaire ont été effacées.
</div>
</div>


<div id="footnotes">
<p id="footnote1">
Expand Down
90 changes: 88 additions & 2 deletions src/js/form-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import { $, $$, downloadBlob } from './dom-utils'
import { addSlash, getFormattedDate } from './util'
import pdfBase from '../certificate.pdf'
import { generatePdf } from './pdf-util'
import SecureLS from 'secure-ls'

const secureLS = new SecureLS({ encodingType: 'aes' })
const clearDataSnackbar = $('#snackbar-cleardata')
const storeDataInput = $('#field-storedata')

const conditions = {
'#field-firstname': {
Expand Down Expand Up @@ -55,6 +60,46 @@ function validateAriaFields () {
.includes(true)
}

function updateSecureLS (formInputs, reasonInputs) {
if (wantDataToBeStored() === true) {
secureLS.set('profile', getProfile(formInputs))
secureLS.set('reason', getReasonsObject(reasonInputs))
} else {
clearSecureLS()
}
}

function clearSecureLS () {
secureLS.clear()
}

function clearForm () {
const formProfile = $('#form-profile')
formProfile.reset()
storeDataInput.checked = false
}

function setCurrentDate (releaseDateInput, releaseTimeInput) {
const currentDate = new Date()

releaseDateInput.value = getFormattedDate(currentDate)
releaseTimeInput.value = currentDate.toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit' })
}

function showSnackbar (snackbarToShow, showDuration = 6000) {
snackbarToShow.classList.remove('d-none')
setTimeout(() => snackbarToShow.classList.add('show'), 100)

setTimeout(function () {
snackbarToShow.classList.remove('show')
setTimeout(() => snackbarToShow.classList.add('d-none'), 500)
}, showDuration)
}

export function wantDataToBeStored () {
return storeDataInput.checked
}

export function setReleaseDateTime (releaseDateInput) {
const loadedDate = new Date()
releaseDateInput.value = getFormattedDate(loadedDate)
Expand Down Expand Up @@ -90,8 +135,39 @@ export function getReasons (reasonInputs) {
return reasons
}

export function prepareInputs (formInputs, reasonInputs, reasonFieldset, reasonAlert, snackbar) {
export function getReasonsObject (reasonInputs) {
return reasonInputs
.filter((reason) => reason.checked)
.reduce((map, reason) => {
map[reason.value] = reason.checked
return map
}, {})
}

export function prepareInputs (formInputs, reasonInputs, reasonFieldset, reasonAlert, snackbar, releaseDateInput, releaseTimeInput) {
const lsProfile = secureLS.get('profile')
const lsReason = secureLS.get('reason')
const currentDate = new Date()
const formattedDate = getFormattedDate(currentDate)
const formattedTime = currentDate.toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit' })

// Continue to store data if already stored
storeDataInput.checked = lsReason || lsProfile

formInputs.forEach((input) => {
switch (input.name) {
case 'datesortie':
input.value = formattedDate
break
case 'heuresortie':
input.value = formattedTime
break
case 'field-reason':
if (lsReason) input.checked = lsReason[input.value]
break
default:
if (lsProfile) input.value = lsProfile[input.name]
}
const exempleElt = input.parentNode.parentNode.querySelector('.exemple')
const validitySpan = input.parentNode.parentNode.querySelector('.validity')
if (input.placeholder && exempleElt) {
Expand Down Expand Up @@ -123,6 +199,13 @@ export function prepareInputs (formInputs, reasonInputs, reasonFieldset, reasonA
})
})

$('#cleardata').addEventListener('click', () => {
clearSecureLS()
clearForm()
setCurrentDate(releaseDateInput, releaseTimeInput)
showSnackbar(clearDataSnackbar, 1200)
})

$('#generate-btn').addEventListener('click', async (event) => {
event.preventDefault()

Expand All @@ -139,6 +222,8 @@ export function prepareInputs (formInputs, reasonInputs, reasonFieldset, reasonA
return
}

updateSecureLS(formInputs, reasonInputs)

const pdfBlob = await generatePdf(getProfile(formInputs), reasons, pdfBase)

const creationInstant = new Date()
Expand Down Expand Up @@ -166,6 +251,7 @@ export function prepareForm () {
const reasonFieldset = $('#reason-fieldset')
const reasonAlert = reasonFieldset.querySelector('.msg-alert')
const releaseDateInput = $('#field-datesortie')
const releaseTimeInput = $('#field-heuresortie')
setReleaseDateTime(releaseDateInput)
prepareInputs(formInputs, reasonInputs, reasonFieldset, reasonAlert, snackbar)
prepareInputs(formInputs, reasonInputs, reasonFieldset, reasonAlert, snackbar, releaseDateInput, releaseTimeInput)
}
10 changes: 0 additions & 10 deletions src/js/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ const createTitle = () => {
const p = createElement('p', { className: 'msg-info', innerHTML: 'Tous les champs sont obligatoires.' })
return [h2, p]
}
// createElement('div', { className: 'form-group' })

const getCurrentTime = () => {
const date = new Date()
return date.toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit' })
}

const createFormGroup = ({
autocomplete = false,
Expand Down Expand Up @@ -60,10 +54,6 @@ const createFormGroup = ({

const input = createElement('input', inputAttrs)

if (name === 'heuresortie') {
input.value = getCurrentTime()
}

const validityAttrs = {
className: 'validity',
}
Expand Down