Skip to content

Commit

Permalink
refactor(defaultDict): use more widely
Browse files Browse the repository at this point in the history
  • Loading branch information
benji6 committed Jan 2, 2024
1 parent 3925824 commit 29d971b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 36 deletions.
15 changes: 5 additions & 10 deletions client/src/components/pages/Mood/MoodLog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "../../../../constants";
import {
createDateFromLocalDateString,
defaultDict,
formatIsoDateInLocalTimezone,
mapRight,
roundDateDown,
Expand All @@ -39,16 +40,10 @@ const DAYS_PER_PAGE = 7;
const groupMoodIdsByDay = (
moodIds: string[],
): [dateStr: string, moodIds: string[]][] => {
const moodsGroupedByDate: { [date: string]: string[] } = {};

for (let i = 0; i < moodIds.length; i++) {
const id = moodIds[i];
const key = formatIsoDateInLocalTimezone(new Date(id));
if (moodsGroupedByDate[key]) moodsGroupedByDate[key].push(id);
else moodsGroupedByDate[key] = [id];
}

return Object.entries(moodsGroupedByDate);
const moodsByDate = defaultDict((): string[] => []);
for (const id of moodIds)
moodsByDate[formatIsoDateInLocalTimezone(new Date(id))].push(id);
return Object.entries(moodsByDate);
};

export default function MoodLog() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { getWeatherDisplayData, moodToColor } from "../../../../utils";
import {
defaultDict,
getWeatherDisplayData,
moodToColor,
} from "../../../../utils";
import { ComponentProps } from "react";
import { Icon } from "eri";
import { MINIMUM_LOCATION_COUNT_FOR_MEAN_CHARTS } from "./constants";
Expand Down Expand Up @@ -26,13 +30,7 @@ export default function MoodByWeatherForPeriod({ dateFrom, dateTo }: Props) {
)
return;

const chartData: {
[nameAndColor: string]: {
moodCount: number;
sumOfMoods: number;
};
} = {};

const chartData = defaultDict(() => ({ moodCount: 0, sumOfMoods: 0 }));
for (let i = 0; i < weatherResults.length; i++) {
const result = weatherResults[i];
const { data } = result;
Expand All @@ -45,13 +43,8 @@ export default function MoodByWeatherForPeriod({ dateFrom, dateTo }: Props) {
weatherId,
});
const key = `${label}:${iconName}:${weatherColor}`;
chartData[key] =
key in chartData
? {
moodCount: chartData[key].moodCount + 1,
sumOfMoods: chartData[key].sumOfMoods + mood,
}
: { moodCount: 1, sumOfMoods: mood };
chartData[key].moodCount += 1;
chartData[key].sumOfMoods += mood;
}
}

Expand Down
16 changes: 5 additions & 11 deletions client/src/store/eventsSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,17 +490,11 @@ export default createSlice({
// value in the returned object cannot be empty
moodIdsByDate: createSelector(
normalizedMoodsSelector,
({ allIds }): { [date: string]: string[] | undefined } => {
const moodsGroupedByDate: { [date: string]: string[] } = {};

for (let i = 0; i < allIds.length; i++) {
const id = allIds[i];
const key = formatIsoDateInLocalTimezone(new Date(id));
if (moodsGroupedByDate[key]) moodsGroupedByDate[key].push(id);
else moodsGroupedByDate[key] = [id];
}

return moodsGroupedByDate;
({ allIds }): { [date: string]: string[] } => {
const moodsByDate = defaultDict((): string[] => []);
for (const id of allIds)
moodsByDate[formatIsoDateInLocalTimezone(new Date(id))].push(id);
return { ...moodsByDate };
},
),
moodIdsInPeriod: createSelector(
Expand Down

0 comments on commit 29d971b

Please sign in to comment.