Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

Refactor weeklybarchart data calculation logic

Khuram Niaz548e261c3d59
diff --git a/src/actions/dashboard.actions.ts b/src/actions/dashboard.actions.ts
index 68717cc..72f0e48 100644
--- a/src/actions/dashboard.actions.ts
+++ b/src/actions/dashboard.actions.ts
@@ -2,7 +2,14 @@ import prisma from "@/lib/db";
import { calculatePercentageDifference, getLast7Days } from "@/lib/utils";
import { getCurrentUser } from "@/utils/user.utils";
import { Prisma } from "@prisma/client";
-import { addMinutes, format, subDays } from "date-fns";
+import {
+ addMinutes,
+ format,
+ formatISO,
+ parseISO,
+ startOfDay,
+ subDays,
+} from "date-fns";
export const getJobsAppliedForPeriod = async (
daysAgo: number
@@ -81,7 +88,7 @@ export const getActivityDataForPeriod = async (): Promise<any | undefined> => {
if (!user) {
throw new Error("Not authenticated");
}
- const today = addMinutes(new Date(), 5);
+ const today = new Date();
const sevenDaysAgo = subDays(today, 6);
const activities = await prisma.activity.findMany({
where: {
@@ -105,26 +112,25 @@ export const getActivityDataForPeriod = async (): Promise<any | undefined> => {
},
});
const groupedData = activities.reduce((acc: any, activity: any) => {
- const day = format(new Date(activity.endTime), "PP");
+ const activityDate = startOfDay(new Date(activity.endTime));
+ const day = formatISO(activityDate, { representation: "date" });
const activityTypeLabel = activity.activityType?.label || "Unknown";
if (!acc[day]) {
- acc[day] = { day: day.split(",")[0] };
+ acc[day] = {};
}
const durationInHours = (activity.duration || 0) / 60;
- acc[day][activityTypeLabel] = (
- (parseFloat(acc[day][activityTypeLabel]) || 0) + durationInHours
- ).toFixed(1);
+ acc[day][activityTypeLabel] =
+ (acc[day][activityTypeLabel] || 0) + durationInHours;
return acc;
}, {});
- const last7Days = getLast7Days();
- const result = last7Days.map((date) => ({
- day: date.split(",")[0],
- ...groupedData[date],
+ const last7Days = getLast7Days("yyyy-MM-dd");
+ const result = last7Days.map((dateStr) => ({
+ day: format(parseISO(dateStr), "EEE, MMM d"),
+ ...groupedData[dateStr],
}));
-
return result;
} catch (error) {
const msg = "Failed to fetch activities data.";
@@ -141,8 +147,7 @@ export const getJobsActivityForPeriod = async (): Promise<any | undefined> => {
throw new Error("Not authenticated");
}
const today = new Date();
- const sevenDaysAgo = new Date();
- sevenDaysAgo.setDate(today.getDate() - 30);
+ const sevenDaysAgo = subDays(today, 6);
const jobData = await prisma.job.groupBy({
by: "appliedDate",
_count: {
@@ -162,16 +167,17 @@ export const getJobsActivityForPeriod = async (): Promise<any | undefined> => {
});
// Reduce to a format that groups by unique date (YYYY-MM-DD)
const groupedPosts = jobData.reduce((acc: any, post: any) => {
- const date = format(new Date(post.appliedDate), "PP");
+ const jobDate = startOfDay(new Date(post.appliedDate));
+ const date = formatISO(jobDate, { representation: "date" });
acc[date] = (acc[date] || 0) + post._count._all;
return acc;
}, {});
// Get the last 7 days
- const last7Days = getLast7Days();
+ const last7Days = getLast7Days("yyyy-MM-dd");
// Map to ensure all dates are represented with a count of 0 if necessary
- const result = last7Days.map((date) => ({
- day: date.split(",")[0],
- value: groupedPosts[date] || 0,
+ const result = last7Days.map((dateStr) => ({
+ day: format(parseISO(dateStr), "EEE, MMM d"),
+ value: groupedPosts[dateStr] || 0,
}));
return result;
@@ -191,7 +197,7 @@ export const getActivityCalendarData = async (): Promise<any | undefined> => {
}
const today = new Date();
const daysAgo = new Date();
- daysAgo.setDate(today.getDate() - 356);
+ daysAgo.setDate(today.getDate() - 365);
const jobData = await prisma.job.groupBy({
by: "appliedDate",
_count: {
diff --git a/src/components/dashboard/WeeklyBarChart.tsx b/src/components/dashboard/WeeklyBarChart.tsx
index 418876e..982881c 100644
--- a/src/components/dashboard/WeeklyBarChart.tsx
+++ b/src/components/dashboard/WeeklyBarChart.tsx
@@ -16,11 +16,20 @@ export default function WeeklyBarChart({
groupMode,
axisLeftLegend,
}: WeeklyBarChartProps) {
+ const roundedData = data.map((item) => {
+ const newItem: any = { ...item };
+ keys.forEach((key) => {
+ if (typeof newItem[key] === "number") {
+ newItem[key] = Math.round(newItem[key] * 100) / 100;
+ }
+ });
+ return newItem;
+ });
return (
<Card className="mb-2 lg:mb-0">
<CardContent className="h-[240px] p-3">
<ResponsiveBar
- data={data}
+ data={roundedData}
keys={keys}
indexBy="day"
margin={{
diff --git a/src/lib/utils.ts b/src/lib/utils.ts
index 1dacdf8..7385674 100644
--- a/src/lib/utils.ts
+++ b/src/lib/utils.ts
@@ -97,11 +97,12 @@ export const calculatePercentageDifference = (
return Math.round(difference);
};
-export const getLast7Days = (dateType = "PP") => {
+export const getLast7Days = (dateType = "PP", baseDate?: Date) => {
const dates = [];
+ const anchor = baseDate ? new Date(baseDate) : new Date();
for (let i = 6; i >= 0; i--) {
- const date = new Date();
- date.setDate(date.getDate() - i);
+ const date = new Date(anchor);
+ date.setDate(anchor.getDate() - i);
dates.push(format(date, dateType));
}
return dates;