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

Add Digital Usages, fixes #57 #58

Open
wants to merge 1 commit into
base: master
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
11 changes: 3 additions & 8 deletions components/categories-selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useState } from "react";
import { loadCategories } from "../lib/ademe-api";
import classNames from "classnames";
import styles from "../styles/categories-selector.module.scss";
import { Trans, t } from "@lingui/macro";
import { Trans } from "@lingui/macro";
import { Item } from "../types/item";
import Button from "./button";
import { loadCategory } from "../lib/ademe-api";
Expand All @@ -22,13 +22,8 @@ export default function CategoriesSelector({setSelectedItems, setCategoriesMode}
// Make 0 a fake one, to avoid the hassle of subtracting by one all the time.
const [selectedCategories, setSelectedCategories] = useState<boolean[]>(Array(categories.length + 1).fill(false));
const updateCategories = (id: number) => {
// Some categories are not available yet
if (id === 10) {
alert(t`This category is not yet available.`);
} else {
selectedCategories[id] = !selectedCategories[id];
setSelectedCategories([...selectedCategories]); // We create a new array to force a React rerender
}
selectedCategories[id] = !selectedCategories[id];
setSelectedCategories([...selectedCategories]); // We create a new array to force a React rerender
};

return (
Expand Down
34 changes: 21 additions & 13 deletions lib/ademe-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import enElectromenager from "../data/ademe/en/6-electromenager.json";
import enMobilier from "../data/ademe/en/7-mobilier.json";
import enChauffage from "../data/ademe/en/8-chauffage.json";
import enVegetablesAndFruits from "../data/ademe/en/9-fruitsetlegumes.json";
import enDigitalUsages from "../data/ademe/en/10-usagenumerique.json";
import frNumerique from "../data/ademe/fr/1-numerique.json";
import frRepas from "../data/ademe/fr/2-repas.json";
import frBoisson from "../data/ademe/fr/3-boisson.json";
Expand All @@ -19,6 +20,7 @@ import frElectromenager from "../data/ademe/fr/6-electromenager.json";
import frMobilier from "../data/ademe/fr/7-mobilier.json";
import frChauffage from "../data/ademe/fr/8-chauffage.json";
import frVegetablesAndFruits from "../data/ademe/fr/9-fruitsetlegumes.json";
import frDigitalUsages from "../data/ademe/fr/10-usagenumerique.json";
import footprintDetailCategories from "../data/ademe/footprintDetailCategories.json";
import { AdemeCategory, AdemeECV, FootprintDetails } from "../types/AdemeECV";
import { Locale } from "../types/i18n";
Expand Down Expand Up @@ -111,7 +113,7 @@ export function loadCategory(id: number, locale: Locale): Item[] {
case 9:
return loadVegetablesAndFruits(locale);
case 10:
return []; // TODO Usage numérique needs more work
return loadDigitalUsagesItems(locale);
default:
return [];
}
Expand Down Expand Up @@ -301,18 +303,24 @@ function loadVegetablesAndFruits(locale: Locale): Item[] {
return vegetablesAndFruitsItems;
}

// Not ready on Ademe side, they should explain with which device, on which network, how much, etc.
// usageNumerique.data.forEach(element => {
// const item: Item = {
// id: element.slug,
// category: "digital",
// label: element.name,
// description: "Achat et usage pendant " + element.usage.defaultyears + " ans.",
// explanation: "",
// source: element
// }
// items.push(item);
// });
const digitalUsagesItems: Item[] = [];
function loadDigitalUsagesItems(locale: Locale): Item[] {
if (digitalUsagesItems.length === 0) {
const digitalUsages = locale === "fr" ? frDigitalUsages : enDigitalUsages;
digitalUsages.data.forEach(element => {
const item: Item = {
id: element.slug,
categoryId: 10,
label: element.name,
description: "",
explanation: "",
source: element
}
digitalUsagesItems.push(item);
});
}
return digitalUsagesItems;
}

export function getFootprintDetails(): FootprintDetails {
return footprintDetailCategories;
Expand Down