Skip to content

Commit 986db1a

Browse files
committed
feat: memoriser certains champs dans le localstorage
1 parent bdad4c9 commit 986db1a

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/js/form-util.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { $, $$, downloadBlob } from './dom-utils'
22
import { addSlash, getFormattedDate } from './util'
33
import pdfBase from '../certificate.pdf'
44
import { generatePdf } from './pdf-util'
5+
import { setPreviousFormValue } from './localstorage'
56

67
const conditions = {
78
'#field-firstname': {
@@ -127,9 +128,19 @@ export function prepareInputs (formInputs, reasonInputs, reasonFieldset, reasonA
127128
return
128129
}
129130

130-
console.log(getProfile(formInputs), reasons)
131+
const profile = getProfile(formInputs)
131132

132-
const pdfBlob = await generatePdf(getProfile(formInputs), reasons, pdfBase)
133+
;[
134+
'address',
135+
'birthday',
136+
'city',
137+
'firstname',
138+
'lastname',
139+
'placeofbirth',
140+
'zipcode',
141+
].forEach(inputName => setPreviousFormValue(inputName, profile[inputName]))
142+
143+
const pdfBlob = await generatePdf(profile, reasons, pdfBase)
133144

134145
const creationInstant = new Date()
135146
const creationDate = creationInstant.toLocaleDateString('fr-CA')

src/js/form.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import '../css/main.css'
55
import formData from '../form-data.json'
66

77
import { $, appendTo, createElement } from './dom-utils'
8+
import { getPreviousFormValue } from './localstorage'
89

910
const createTitle = () => {
1011
const h2 = createElement('h2', { className: 'titre-2', innerHTML: 'Remplissez en ligne votre déclaration numérique : ' })
@@ -51,6 +52,7 @@ const createFormGroup = ({
5152
placeholder,
5253
required: true,
5354
type,
55+
value: getPreviousFormValue(name),
5456
}
5557

5658
const input = createElement('input', inputAttrs)

src/js/localstorage.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const localStorage = window.localStorage || {
2+
getItem: () => null,
3+
setItem: () => undefined,
4+
}
5+
6+
export function getPreviousFormValue(name) {
7+
return localStorage.getItem(`form-value-${name}`)
8+
}
9+
10+
export function setPreviousFormValue(name, value) {
11+
localStorage.setItem(`form-value-${name}`, value)
12+
}

0 commit comments

Comments
 (0)