Skip to content

Commit

Permalink
merge Ne pas utiliser le localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
bastienmoulia committed Apr 10, 2020
1 parent e2a1b8a commit 85dbbba
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions src/certificate.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,26 +58,18 @@ function setReleaseDateTime () {
releaseTimeInput.value = `${hour}:${minute}`
}

function saveProfile () {
function getProfile () {
const fields = {}
for (const field of $$('#form-profile input')) {
if (field.id === 'field-datesortie') {
var dateSortie = field.value.split('-')
localStorage.setItem(field.id.substring('field-'.length), `${dateSortie[2]}/${dateSortie[1]}/${dateSortie[0]}`)
fields[field.id.substring('field-'.length)] = `${dateSortie[2]}/${dateSortie[1]}/${dateSortie[0]}`
} else {
localStorage.setItem(field.id.substring('field-'.length), field.value)
fields[field.id.substring('field-'.length)] = field.value
}
}
}

function getProfile () {
const fields = {}
for (let i = 0; i < localStorage.length; i++) {
const name = localStorage.key(i)
fields[name] = localStorage.getItem(name)
}
return fields
}

function idealFontSize (font, text, maxWidth, minSize, defaultSize) {
let currentSize = defaultSize
let textWidth = font.widthOfTextAtSize(text, defaultSize)
Expand Down Expand Up @@ -206,11 +198,10 @@ function downloadBase64 (base64, fileName) {
link.click()
}

function getAndSaveReasons () {
function getReasons () {
const values = $$('input[name="field-reason"]:checked')
.map(x => x.value)
.join('-')
localStorage.setItem('reasons', values)
return values
}

Expand Down Expand Up @@ -246,10 +237,8 @@ const snackbar = $('#snackbar')
$('#generate-btn-pdf').addEventListener('click', async event => {
event.preventDefault()

saveProfile()
const reasons = getAndSaveReasons()
const reasons = getReasons()
const pdfBlob = await generatePdf(getProfile(), reasons)
localStorage.clear()
const creationDate = new Date().toLocaleDateString('fr-CA')
const creationHour = new Date().toLocaleTimeString('fr-FR', { hour: '2-digit', minute: '2-digit' }).replace(':', '-')
downloadBlob(pdfBlob, `attestation-${creationDate}_${creationHour}.pdf`)
Expand All @@ -266,10 +255,8 @@ $('#generate-btn-pdf').addEventListener('click', async event => {
$('#generate-btn-image').addEventListener('click', async event => {
event.preventDefault()

saveProfile()
const reasons = getAndSaveReasons()
const reasons = getReasons()
const pdfBlob = await generatePdf(getProfile(), reasons)
localStorage.clear()
pdfjsLib.GlobalWorkerOptions.workerSrc = pdfjsWorker
const reader = new FileReader()

Expand Down

0 comments on commit 85dbbba

Please sign in to comment.