Skip to content

Commit

Permalink
feat: show how much people spent on their coffee in wrapped
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelblijleven committed Jan 5, 2024
1 parent eedd124 commit 743a84c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/components/beanconqueror/wrapped/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export type WrappedData = {
totalBrews: number;
totalCoffee: number;
totalWeight: number;
totalCost: number;
hasMissingCosts: boolean;
brewsPerMonth: Record<string, number>;
averageBrewsPerDay: number;
mostCommonDrinkingHour: number;
Expand All @@ -58,8 +60,6 @@ function getDefaultDict(initial: number): Record<string | symbol, number> {
});
}

const foo = {};

/**
* Checks if the provided data has either a buy date equal to the provided year, or
* a unix timestamp of the provided year.
Expand All @@ -70,10 +70,7 @@ function inYear(data: HasTimeData, year: number): boolean {
if (!!data.buyDate) {
return new Date(data.buyDate).getFullYear() === year;
}
const d = new Date(data.config.unix_timestamp * 1000);
if (d.getFullYear() === year) {
console.log(d.getHours())
}

return new Date(data.config.unix_timestamp * 1000).getFullYear() === year;
}

Expand Down Expand Up @@ -195,11 +192,22 @@ export function createWrappedStatistics(data: BCData, year: number): WrappedData
const grinders = brewsInYear.map(b => b.mill);
const preps = brewsInYear.map(b => b.method_of_preparation);

let hasMissingCosts = false;

for (const bean of beansInYear) {
if (!bean.cost) {
hasMissingCosts = true;
break;
}
}

return {
year: year,
totalBrews: mappings.brewMaps.inYear.size,
totalCoffee: mappings.beanMaps.inYear.size,
totalWeight: Array.from(beansInYear).reduce((prev, bean) => prev + bean.weight, 0),
totalCost: beansInYear.reduce((prev, curr) => prev + curr.cost, 0),
hasMissingCosts: hasMissingCosts,
brewsPerMonth: timeStats.countPerMonth,
averageBrewsPerDay: Object.values(timeStats.brewsPerDay).reduce((prev, curr) => prev + curr, 0) / Object.values(timeStats.brewsPerDay).length,
mostCommonDrinkingHour: parseInt(mostCommonDrinkingHour),
Expand Down
10 changes: 8 additions & 2 deletions src/components/beanconqueror/wrapped/wrapped.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ function WrappedCarousel() {
);
});

console.log(data.mostCommonProcessingMethod);
return (
<Carousel opts={{
align: "start",
Expand All @@ -63,6 +62,13 @@ function WrappedCarousel() {
<Text>With a total weight of</Text>
<BigText>{Math.floor(data.totalWeight * 100) / 100} grams</BigText>
</CarouselItem>
{data.totalCost > 0 && (
<CarouselItem className={"flex flex-col items-center justify-center bg-tiles"}>
<Text>You&apos;ve spent</Text>
<BigText>{data.totalCost} 🤫</BigText>
{data.hasMissingCosts && (<div className={"whitespace-pre-wrap"}>Did you enter everything 😉?</div>)}
</CarouselItem>
)}
<CarouselItem className={"flex flex-col items-center justify-center bg-circle-wave-2"}>
<Text>You&apos;ve been busy, you made</Text>
<BigText>{data.totalBrews} brews</BigText>
Expand Down Expand Up @@ -126,7 +132,6 @@ function Upload() {

const callback = async () => {
const file = fileRef.current?.files?.[0];

if (!file) {
toast({
title: "Error",
Expand All @@ -137,6 +142,7 @@ function Upload() {
}
try {
setData(createWrappedStatistics(await readZipFile(file), year));
console.log("miaw?")
} catch (e) {
const wrappedError = (e as {wrappedError: string}).wrappedError;
if (wrappedError) {
Expand Down

0 comments on commit 743a84c

Please sign in to comment.