Skip to content

Commit

Permalink
Add UI to render calculation history, and button to refresh history
Browse files Browse the repository at this point in the history
  • Loading branch information
juskek committed Jul 6, 2023
1 parent 1a686f7 commit 56d3e77
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
37 changes: 37 additions & 0 deletions src/components/calculation-history/CalculationHistory.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from "react";
import { CalculationData } from "../../data/calculations/ICalculationsRepository";
type CountryDropdownType = {
refreshCalculationHistory: () => void;
calculationHistory: CalculationData[];
};
export const CalculationHistory = ({
refreshCalculationHistory,
calculationHistory,
}: CountryDropdownType) => {
return (
<div>
<button onClick={refreshCalculationHistory}>Refresh History</button>
<ul>
{calculationHistory.map((calculation, index) => (
<li key={index}>
<p>Bytes: {calculation.bytes}</p>
<p>Emissions: {calculation.emissions}</p>
<p>
Specific Emissions: {calculation.specificEmissions}
</p>
{/* Render the selected countries */}
<ul>
{Array.from(calculation.selectedCountries).map(
([countryName, percentage]) => (
<li key={countryName}>
{countryName}: {percentage}
</li>
)
)}
</ul>
</li>
))}
</ul>
</div>
);
};
7 changes: 7 additions & 0 deletions src/popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createRoot } from "react-dom/client";
import { CountryDropdown } from "./components/CountryDropdown";
import { usePopup } from "./usePopup";
import { SelectedCountries } from "./components/selected-countries/SelectedCountries";
import { CalculationHistory } from "./components/calculation-history/CalculationHistory";

export const Popup = () => {
const {
Expand All @@ -15,6 +16,8 @@ export const Popup = () => {
averageSpecificEmissions,
refreshAndGetSize,
stopRecording,
refreshCalculationHistory,
calculationHistory,
error,
} = usePopup();

Expand Down Expand Up @@ -45,6 +48,10 @@ export const Popup = () => {
setCountryPercentage={setCountryPercentage}
/>
<CountryDropdown addSelectedCountry={addSelectedCountry} />
<CalculationHistory
calculationHistory={calculationHistory}
refreshCalculationHistory={refreshCalculationHistory}
/>
{error && <p>{error}</p>}
</div>
);
Expand Down
16 changes: 15 additions & 1 deletion src/usePopup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { calculateAverageSpecificEmissionsHelper } from "./helpers/calculateAver
import { calculateCarbon } from "./helpers/calculateCarbon";
import { ISelectedCountriesRepository } from "./data/selected_countries/ISelectedCountriesRepository";
import { useMountEffect } from "./helpers/useOnceAfterFirstMount";
import { ICalculationsRepository } from "./data/calculations/ICalculationsRepository";
import {
CalculationData,
ICalculationsRepository,
} from "./data/calculations/ICalculationsRepository";
import { IBytesRepository } from "./data/bytes/IBytesRepository";

export const usePopup = () => {
Expand All @@ -21,6 +24,15 @@ export const usePopup = () => {
>(new Map<CountryName, number>());
const [averageSpecificEmissions, setAverageSpecificEmissions] = useState(0);
const [error, setError] = useState<string>();
const [calculationHistory, setCalculationHistory] = useState<
CalculationData[]
>([]);

const refreshCalculationHistory = async () => {
const calculationsData =
await calculationsRepository.getAllCalculations();
setCalculationHistory(calculationsData);
};

const setCountryPercentage = async (
country: CountryName,
Expand Down Expand Up @@ -197,6 +209,8 @@ export const usePopup = () => {
averageSpecificEmissions,
refreshAndGetSize,
stopRecording,
calculationHistory,
refreshCalculationHistory,
error,
};
};

0 comments on commit 56d3e77

Please sign in to comment.