Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
add max date for appointment
Browse files Browse the repository at this point in the history
  • Loading branch information
Pistashe committed Dec 12, 2021
1 parent 541c9e8 commit 086fff9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
9 changes: 9 additions & 0 deletions browser_action/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@

<div class="panel-section-separator"></div>

<div class="panel-formElements-item">
<div>
<input type="date" name="dateMaxSearch" id="dateMax" />
<label for="dateMax">Date maximale de recherche</label>
</div>
</div>

<div class="panel-section-separator"></div>

<div class="panel-formElements-item">
<div>
<input type="radio" name="autoBook" id="disableAutoBook" checked />
Expand Down
9 changes: 9 additions & 0 deletions browser_action/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@
document.getElementById(injectionVaccine).checked = true;
}

function displayDateMaxSearch(dateMaxSearch) {
const date = new Date(dateMaxSearch);
document.getElementById('dateMax').value = date.toISOString().split('T')[0]
}

// Preparation des données
const appStatus = new AppStatus();
const vCLStorage = new VCLocalStorage({
Expand All @@ -118,6 +123,7 @@
appStatus.onAutoBookChange(displayAutoBook);
appStatus.onInjectionTypeChange(displayInjectionType);
appStatus.onInjectionVaccineChange(displayInjectionVaccine);
appStatus.onDateMaxSearchChange(displayDateMaxSearch);

// Initialisation donnée
appStatus.init();
Expand Down Expand Up @@ -150,6 +156,8 @@
appStatus.setInjectionVaccine.bind(appStatus, "modernaInjection");
document.getElementById("pfizerInjection").onclick =
appStatus.setInjectionVaccine.bind(appStatus, "pfizerInjection");
document.getElementById("dateMax").onblur =
appStatus.setDateMaxSearch.bind(appStatus, document.getElementById('dateMax'));

document.getElementById("reset").onclick = () => {
if (
Expand All @@ -169,5 +177,6 @@
displayAutoBook(appStatus.getAutoBook());
displayInjectionType(appStatus.getInjectionType());
displayInjectionVaccine(appStatus.getInjectionVaccine());
displayDateMaxSearch(appStatus.getDateMaxSearch());
displayLocations();
})();
26 changes: 26 additions & 0 deletions commons/AppStatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class AppStatus {
this.injectionType = "fullServiceInjection";
/** @type {'modernaInjection' | 'pfizerInjection'} vaccin d'injection souhaité par le user */
this.injectionVaccine = "pfizerInjection";
this.dateMaxSearch = new Date(2022, 1, 15);
/** @type {(string) => void} callback quand une {@link VaccineLocation} a été ajouté */
this.onLocationAddedCb = (job) => {};
/** @type {(string) => void} callback quand une {@link VaccineLocation} a été supprimée */
Expand All @@ -34,6 +35,7 @@ class AppStatus {
this.onInjectionTypeCb = (newValue) => {};
/** @type {'modernaInjection' | 'pfizerInjection'} callback quand injectionVaccine change de valeur */
this.onInjectionVaccineCb = (newValue) => {};
this.onDateMaxSearchCb = (newValue) => {};

this.onStorageChange = this.onStorageChange.bind(this);
browser.storage.onChanged.addListener(this.onStorageChange);
Expand All @@ -49,6 +51,7 @@ class AppStatus {
autoBook: false,
injectionType: "fullServiceInjection",
injectionVaccine: "pfizerInjection",
dateMaxSearch: new Date(2022, 1, 15),
});

Object.keys(result.locations).forEach((url) => {
Expand All @@ -67,6 +70,9 @@ class AppStatus {

this.injectionVaccine = result.injectionVaccine;
this.onInjectionVaccineCb(this.injectionVaccine);

this.dateMaxSearch = result.dateMaxSearch;
this.onDateMaxSearchCb(this.dateMaxSearch);
}

getLocations() {
Expand Down Expand Up @@ -105,6 +111,10 @@ class AppStatus {
return this.injectionVaccine;
}

getDateMaxSearch() {
return this.dateMaxSearch;
}

/**
* @param {(string) => void} cbAdd callback quand une {@link VaccineLocation} a été ajouté
* @param {(string) => void} cbDelete callback quand une {@link VaccineLocation} a été supprimée
Expand Down Expand Up @@ -141,6 +151,9 @@ class AppStatus {
onInjectionVaccineChange(callback) {
this.onInjectionVaccineCb = callback;
}
onDateMaxSearchChange(callback) {
this.onDateMaxSearchCb = callback;
}

start() {
this.stopped = false;
Expand Down Expand Up @@ -168,6 +181,11 @@ class AppStatus {
browser.storage.sync.set({ injectionType: this.injectionType });
}

setDateMaxSearch(value) {
this.dateMaxSearch = new Date(value.value);
browser.storage.sync.set({ dateMaxSearch: this.dateMaxSearch });
}

/**
* @param {'modernaInjection' | 'pfizerInjection'} value The new injectionVaccine value
*/
Expand All @@ -193,6 +211,8 @@ class AppStatus {
this.onInjectionTypeCb(this.injectionType);
this.injectionVaccine = "pfizerInjection";
this.onInjectionTypeCb(this.injectionVaccine);
this.dateMaxSearch = new Date(2022, 1, 15);
this.onDateMaxSearchCb(this.dateMaxSearch);
}

/**
Expand All @@ -209,6 +229,7 @@ class AppStatus {
this.onAutoBookChangeCb = null;
this.onInjectionTypeCb = null;
this.onInjectionVaccineCb = null;
this.onDateMaxSearchCb = null;
}

/**
Expand Down Expand Up @@ -262,5 +283,10 @@ class AppStatus {

this.onInjectionVaccineCb(this.injectionVaccine);
}
if (change.dateMaxSearch) {
this.dateMaxSearch = change.dateMaxSearch.newValue;

this.onDateMaxSearchCb(this.dateMaxSearch);
}
}
}
39 changes: 34 additions & 5 deletions content_scripts/doctolib/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@
if (currentMonth > selectedMonth) return new Date().getFullYear() + 1;
return new Date().getFullYear();
}
const MONTHS = {
'janv.': 1,
'févr.': 2,
'mars': 3,
'avr.': 4,
'mai': 5,
'juin': 6,
'juil.': 7,
'aout': 8,
'sept.': 9,
'oct.': 10,
'nov.': 11,
'déc.': 12,
};

async function waitTimeout(timeout) {
await new Promise((r) => setTimeout(r, timeout));
Expand Down Expand Up @@ -295,15 +309,19 @@

let running = false;
async function checkAvailability() {
const { locations, stopped, autoBook, injectionType, injectionVaccine } =
const { locations, stopped, autoBook, injectionType, injectionVaccine, dateMaxSearch } =
await browser.storage.sync.get({
locations: {},
stopped: false,
autoBook: false,
injectionType: "fullServiceInjection",
injectionVaccine: "pfizerInjection",
dateMaxSearch: new Date(2022, 1, 15)
});

const dateMaxSearchDate = typeof(dateMaxSearch) === 'string' ? new Date(dateMaxSearch) : dateMaxSearch;
console.log(dateMaxSearchDate)

if (stopped || !locations[url]) {
running = false;
return;
Expand Down Expand Up @@ -421,6 +439,8 @@
const parts = slot.title.match(
/([0-9]+)\.? ([\p{Letter}]+)\.? ([0-9]+:[0-9]+)/u
);
// /([0-9]+) [\p{Letter}]+\.? ([0-9]+:[0-9]+)/gu
// )[0].split(' ');
if (!parts) {
throw new Error(
browser.i18n.getMessage("slotDateFormatNotFound", slot.title)
Expand All @@ -434,18 +454,27 @@
const selectedTime = parts[3];

const date = new Date(
`${selectedMonth} ${selectedDay} ${selectedYear} ${selectedTime}`
);
`${selectedMonth} ${selectedDay} ${selectedYear} ${selectedTime}`);
// console.log(parts)
// const year = parts[1] === 'decembre' ? 2021 : 2022;
// const date = new Date(
// `${MONTHS[parts[1]]} ${parts[0]} ${year} ${
// parts[2]
// }`
// );

const tomorrow = new Date();
tomorrow.setHours(23);
tomorrow.setMinutes(59);
tomorrow.setDate(tomorrow.getDate() + 1);

if (date > tomorrow && date < new Date("2021-05-31T00:20:00"))

if (date > dateMaxSearchDate) {
const formatedDate = dateMaxSearchDate.toLocaleDateString();
throw new Error(
"Pas de créneau dispo d'ici demain soir ou après le 31 mai"
`Pas de créneau dispo d'ici demain soir ou avant le ${formatedDate}`
);
}

if (!autoBook) {
browser.runtime.sendMessage({
Expand Down

0 comments on commit 086fff9

Please sign in to comment.