Skip to content

Commit

Permalink
Tech: encore du fun avec les modules ES6
Browse files Browse the repository at this point in the history
  • Loading branch information
ronnix committed Sep 11, 2020
1 parent d706fc8 commit d25df62
Show file tree
Hide file tree
Showing 44 changed files with 454 additions and 595 deletions.
4 changes: 1 addition & 3 deletions incidence.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,7 @@ def generate():
data = retrieve_data(DATA_URL)
by_departement = aggregate_by_departement(data)
by_week = aggregate_by_week(by_departement)
open(DATA_DIR / "incidence.js", "w").write(
f"const incidence = {by_week}\nmodule.exports = {{incidence}}"
)
open(DATA_DIR / "incidence.js", "w").write(f"export default {by_week}\n")


@wrap
Expand Down
4 changes: 1 addition & 3 deletions prefectures.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def retrieve_data(url):
def generate():
data = retrieve_data(DATA_URL)
prefectures = {item["codeDepartement"]: item["pagePrefecture"] for item in data}
open(DATA_DIR / "prefectures.js", "w").write(
f"const prefectures = {prefectures}\nmodule.exports = {{prefectures}}"
)
open(DATA_DIR / "prefectures.js", "w").write(f"export default {prefectures}\n")


@wrap
Expand Down
37 changes: 15 additions & 22 deletions src/scripts/actions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ICS } from './ics'
import affichage from './affichage'
import pagination from './pagination'
import ICS from './ics.js'
import { hideElement, showElement } from './affichage.js'
import { getCurrentPageName } from './pagination.js'

function bindCalendar(element, profil) {
export function bindCalendar(element, profil) {
const ics = new ICS(navigator.appVersion)
const duration = 1 // heures bloquées sur le calendrier.
const urlSuivi = 'https://mesconseilscovid.sante.gouv.fr/#suiviintroduction'
Expand Down Expand Up @@ -42,7 +42,7 @@ function bindCalendar(element, profil) {
})
}

function bindFeedback(component) {
export function bindFeedback(component) {
function opacityTransition(component, delay, callback) {
component.style.transition = `opacity ${delay / 1000}s`
component.style.opacity = '0'
Expand All @@ -56,8 +56,8 @@ function bindFeedback(component) {
const transitionDelay = component.dataset.feedbackTransitionDelay

opacityTransition(component, transitionDelay, (component) => {
affichage.hideElement(component.querySelector('.feedback-question'))
affichage.showElement(component.querySelector('.feedback-form'))
hideElement(component.querySelector('.feedback-question'))
showElement(component.querySelector('.feedback-form'))
component.parentElement.classList.add('js-feedback-submitted')
const form = component.querySelector('.feedback-form form')
form.addEventListener('submit', (event) => {
Expand All @@ -66,16 +66,16 @@ function bindFeedback(component) {
const payload = {
kind: feedback,
message: event.target.elements.message.value,
page: pagination.getCurrentPageName(),
page: getCurrentPageName(),
}
const request = new XMLHttpRequest()
request.open('POST', feedbackHost + '/feedback', true)
request.setRequestHeader('Content-Type', 'application/json')
request.send(JSON.stringify(payload))

opacityTransition(component, transitionDelay, (component) => {
affichage.hideElement(component.querySelector('.feedback-form'))
affichage.showElement(component.querySelector('.feedback-thankyou'))
hideElement(component.querySelector('.feedback-form'))
showElement(component.querySelector('.feedback-thankyou'))
})
})
})
Expand All @@ -91,13 +91,13 @@ function bindFeedback(component) {
})
document.addEventListener('pageChanged', () => {
// Display again the question if the user change page.
affichage.hideElement(component.querySelector('.feedback-form'))
affichage.hideElement(component.querySelector('.feedback-thankyou'))
affichage.showElement(component.querySelector('.feedback-question'))
hideElement(component.querySelector('.feedback-form'))
hideElement(component.querySelector('.feedback-thankyou'))
showElement(component.querySelector('.feedback-question'))
})
}

function bindImpression(element) {
export function bindImpression(element) {
const printButton = element.querySelector('.js-impression')
printButton.addEventListener('click', (event) => {
event.preventDefault()
Expand All @@ -109,7 +109,7 @@ function bindImpression(element) {
})
}

function bindSuppressionTotale(element, app) {
export function bindSuppressionTotale(element, app) {
element.addEventListener('click', (event) => {
event.preventDefault()
if (confirm('Êtes-vous sûr·e de vouloir supprimer tous les profils ?')) {
Expand All @@ -123,10 +123,3 @@ function bindSuppressionTotale(element, app) {
}
})
}

export default {
bindImpression,
bindFeedback,
bindCalendar,
bindSuppressionTotale,
}
30 changes: 9 additions & 21 deletions src/scripts/affichage.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function showMeOrThem(element, profil) {
export function showMeOrThem(element, profil) {
// eslint-disable-next-line no-extra-semi
;[].forEach.call(
element.querySelectorAll('.me'),
Expand All @@ -16,40 +16,40 @@ function showThem(themElement) {
showElement(themElement.nextSibling)
}

function hideElement(element) {
export function hideElement(element) {
element.setAttribute('hidden', '')
element.classList.remove('visible')
}

function showElement(element) {
export function showElement(element) {
element.removeAttribute('hidden')
element.classList.add('visible')
}

function hideSelector(element, selector) {
export function hideSelector(element, selector) {
// eslint-disable-next-line no-extra-semi
;[].forEach.call(element.querySelectorAll(selector), hideElement)
}

function displayElementById(element, id) {
export function displayElementById(element, id) {
var block = element.querySelector('#' + id)
if (!block) return
showElement(block)
}

function displayBlocks(element, blockNames) {
export function displayBlocks(element, blockNames) {
blockNames.forEach(function (block) {
displayElementById(element, block)
})
}

function createElementFromHTML(htmlString) {
export function createElementFromHTML(htmlString) {
var div = document.createElement('div')
div.innerHTML = htmlString.trim()
return div.firstElementChild
}

function escapeHtml(str) {
export function escapeHtml(str) {
return str
.replace(/&/g, '&')
.replace(/</g, '&lt;')
Expand All @@ -59,7 +59,7 @@ function escapeHtml(str) {
.replace(/`/g, '&#x60;')
}

function safeHtml(literals, ...substitutions) {
export function safeHtml(literals, ...substitutions) {
let result = ''

for (let i = 0; i < substitutions.length; i++) {
Expand All @@ -70,15 +70,3 @@ function safeHtml(literals, ...substitutions) {
result += literals[literals.length - 1]
return result
}

export default {
showMeOrThem,
hideElement,
showElement,
hideSelector,
displayElementById,
displayBlocks,
createElementFromHTML,
safeHtml,
escapeHtml,
}
19 changes: 6 additions & 13 deletions src/scripts/algorithme/deconfinement.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var utils = require('../utils.js')
import { joursApres, joursAvant } from '../utils.js'

class AlgorithmeDeconfinement {
export default class AlgorithmeDeconfinement {
constructor(profil, algoOrientation) {
this.profil = profil
this.algoOrientation = algoOrientation
Expand All @@ -12,18 +12,15 @@ class AlgorithmeDeconfinement {
delta = 10
}
const now = new Date()
const finDeQuarantaine = utils.joursApres(
delta,
this.profil.symptomes_start_date
)
const finDeQuarantaine = joursApres(delta, this.profil.symptomes_start_date)
return now > finDeQuarantaine
}

isSuiviRegulier() {
// Au moins une entrée ces dernières 24h + une entrée ces dernières 48h.
const maintenant = utils.joursAvant(0)
const ilYA24h = utils.joursAvant(1)
const ilYA48h = utils.joursAvant(2)
const maintenant = joursAvant(0)
const ilYA24h = joursAvant(1)
const ilYA48h = joursAvant(2)
return (
this.profil.suiviEntre(ilYA24h, maintenant).length >= 1 &&
this.profil.suiviEntre(ilYA48h, ilYA24h).length >= 1
Expand Down Expand Up @@ -63,7 +60,3 @@ class AlgorithmeDeconfinement {
)
}
}

module.exports = {
AlgorithmeDeconfinement,
}
8 changes: 2 additions & 6 deletions src/scripts/algorithme/orientation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const incidence = require('../data/incidence.js').incidence
import incidence from '../data/incidence.js'

class AlgorithmeOrientation {
export default class AlgorithmeOrientation {
constructor(profil) {
this.profil = profil
}
Expand Down Expand Up @@ -298,7 +298,3 @@ class AlgorithmeOrientation {
return blockNames
}
}

module.exports = {
AlgorithmeOrientation,
}
6 changes: 1 addition & 5 deletions src/scripts/algorithme/suivi.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class AlgorithmeSuivi {
export default class AlgorithmeSuivi {
constructor(profil) {
this.profil = profil
this.dernierEtat = profil.dernierEtat()
Expand Down Expand Up @@ -87,7 +87,3 @@ class AlgorithmeSuivi {
return evolutions
}
}

module.exports = {
AlgorithmeSuivi,
}
16 changes: 6 additions & 10 deletions src/scripts/app.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import Profil from './profil'
import { StockageLocal } from './stockage.js'
import Profil from './profil.js'
import StockageLocal from './stockage.js'
import { joursAvant } from './utils.js'

var Router = require('./router.js')
var Updater = require('./updater.js').Updater
import { initRouter } from './router.js'
import Updater from './updater.js'

class App {
export default class App {
constructor() {
this.profil = new Profil()
this.stockage = new StockageLocal()
}
init() {
this.router = Router.initRouter(this)
this.router = initRouter(this)
this.updater = new Updater(this.router)
return this.chargerProfilActuel()
}
Expand Down Expand Up @@ -79,7 +79,3 @@ class App {
this.enregistrerProfilActuel()
}
}

module.exports = {
App,
}
4 changes: 1 addition & 3 deletions src/scripts/data/departements.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const departements = {
export default {
'01': 'Ain',
'02': 'Aisne',
'03': 'Allier',
Expand Down Expand Up @@ -104,5 +104,3 @@ const departements = {
'977': 'Saint-Barthélemy',
'978': 'Saint-Martin',
}

module.exports = { departements }
Loading

0 comments on commit d25df62

Please sign in to comment.