Skip to content

Commit

Permalink
Merge pull request #25 from Theodo-UK/2023/07/06-AAUser-I-can-see-pre…
Browse files Browse the repository at this point in the history
…viously-calculated-recordings

2023/07/06 aa user i can see previously calculated recordings
  • Loading branch information
juskek authored Jul 6, 2023
2 parents 125b505 + 961dfa4 commit 209fc10
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 99 deletions.
36 changes: 36 additions & 0 deletions src/components/calculation-history/CalculationHistory.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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>
<ul>
{Array.from(calculation.selectedCountries).map(
([countryName, percentage]) => (
<li key={countryName}>
{countryName}: {percentage}
</li>
)
)}
</ul>
</li>
))}
</ul>
</div>
);
};
66 changes: 66 additions & 0 deletions src/data/calculations/CalculationsRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { IStorageRepository } from "../storage/IStorageRepository";
import {
CalculationData,
ICalculationsRepository,
} from "./ICalculationsRepository";

export class CalculationsRepository implements ICalculationsRepository {
remoteDataSource: IStorageRepository = IStorageRepository.instance;

async storeCalculation(calculationData: CalculationData): Promise<void> {
const oldCalculations = await this.getAllCalculations();
const newCalculations = [calculationData, ...oldCalculations];
await this.remoteDataSource.set({
allCalculations: JSON.stringify(newCalculations),
});
}

async cacheOngoingCalculation(
calculationData: CalculationData
): Promise<void> {
await this.remoteDataSource.set({
ongoingCalculation: JSON.stringify(calculationData),
});
}

async clearOngoingCalculation(): Promise<void> {
await this.remoteDataSource.set({
ongoingCalculation: null,
});
}

async getAllCalculations(): Promise<CalculationData[]> {
const data = await this.remoteDataSource.get({
allCalculations: JSON.stringify([]),
});

return JSON.parse(
data["allCalculations"] as string
) as CalculationData[];
}

async _getOngoingCalculation(): Promise<CalculationData | null> {
const data = await this.remoteDataSource.get({
ongoingCalculation: null,
});

if (data["ongoingCalculation"] !== null) {
return JSON.parse(
data["ongoingCalculation"] as string
) as CalculationData;
}
return null;
}

async getLastCalculation(): Promise<CalculationData | null> {
const ongoingCalculation = await this._getOngoingCalculation();
if (ongoingCalculation !== null) {
return ongoingCalculation;
}
const oldCalculations = await this.getAllCalculations();
if (oldCalculations.length > 0) {
return oldCalculations[0];
}
return null;
}
}
42 changes: 42 additions & 0 deletions src/data/calculations/ICalculationsRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { CountryName } from "../../constants/Countries";
import { CalculationsRepository } from "./CalculationsRepository";
import { TestCalculationsRepository } from "./TestCalculationsRepository";

export abstract class ICalculationsRepository {
private static _instance: ICalculationsRepository;
static get instance(): ICalculationsRepository {
if (!this._instance) {
switch (process.env.ENV) {
case "development":
this._instance = new CalculationsRepository();
break;
case "test":
this._instance = new TestCalculationsRepository();
break;
default:
throw new Error(`Unknown environment: ${process.env.ENV}`);
}
}

return this._instance;
}

abstract storeCalculation(calculationData: CalculationData): Promise<void>;

abstract cacheOngoingCalculation(
calculationData: CalculationData
): Promise<void>;

abstract clearOngoingCalculation(): Promise<void>;

abstract getLastCalculation(): Promise<CalculationData | null>;

abstract getAllCalculations(): Promise<CalculationData[]>;
}

export type CalculationData = {
bytes: number;
emissions: number;
specificEmissions: number;
selectedCountries: Map<CountryName, number>;
};
38 changes: 38 additions & 0 deletions src/data/calculations/TestCalculationsRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
CalculationData,
ICalculationsRepository,
} from "./ICalculationsRepository";

export class TestCalculationsRepository implements ICalculationsRepository {
private _allCalculations: CalculationData[] = [];
private _ongoingCalculation: CalculationData | null = null;

async storeCalculation(calculationData: CalculationData): Promise<void> {
const tempArray = [calculationData, ...this._allCalculations];
this._allCalculations = tempArray;
}

async cacheOngoingCalculation(
calculationData: CalculationData
): Promise<void> {
this._ongoingCalculation = calculationData;
}

async clearOngoingCalculation(): Promise<void> {
this._ongoingCalculation = null;
}

async getAllCalculations(): Promise<CalculationData[]> {
return this._allCalculations;
}

async getLastCalculation(): Promise<CalculationData | null> {
if (this._ongoingCalculation !== null) {
return this._ongoingCalculation;
}
if (this._allCalculations.length > 0) {
return this._allCalculations[0];
}
return null;
}
}
34 changes: 0 additions & 34 deletions src/data/emissions/EmissionsRepository.ts

This file was deleted.

32 changes: 0 additions & 32 deletions src/data/emissions/IEmissionsRepository.ts

This file was deleted.

17 changes: 0 additions & 17 deletions src/data/emissions/TestEmissionsRepository.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/data/storage/IStorageRepository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { StorageRepository } from "./StorageRepository";
import { TestStorageRepository } from "./TestStorageRepository";

export type StorageDataType = string | number;
export type StorageDataType = string | number | null;
export abstract class IStorageRepository {
private static _instance: IStorageRepository;
static get instance(): IStorageRepository {
Expand Down
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
Loading

0 comments on commit 209fc10

Please sign in to comment.