All files / client/src/pages calendar-view.tsx

0% Statements 0/36
0% Branches 0/24
0% Functions 0/9
0% Lines 0/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185                                                                                                                                                                                                                                                                                                                                                                                 
import { useState } from "react";
import { useQuery } from "@tanstack/react-query";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { ChevronLeft, ChevronRight, Calendar as CalendarIcon } from "lucide-react";
import { Skeleton } from "@/components/ui/skeleton";
import { CategoryBadge } from "@/components/category-badge";
import { 
  format, 
  startOfMonth, 
  endOfMonth, 
  eachDayOfInterval, 
  isSameMonth, 
  isSameDay,
  addMonths,
  subMonths,
  startOfWeek,
  endOfWeek,
  parseISO,
  isValid,
} from "date-fns";
import type { Resolution, CheckIn, Category } from "@shared/schema";
 
export function CalendarView() {
  const [currentDate, setCurrentDate] = useState(new Date());
 
  const { data: resolutions = [], isLoading: resolutionsLoading } = useQuery<Resolution[]>({
    queryKey: ["/api/resolutions"],
  });
 
  const { data: checkIns = [], isLoading: checkInsLoading } = useQuery<CheckIn[]>({
    queryKey: ["/api/check-ins"],
  });
 
  const isLoading = resolutionsLoading || checkInsLoading;
 
  const monthStart = startOfMonth(currentDate);
  const monthEnd = endOfMonth(currentDate);
  const calendarStart = startOfWeek(monthStart);
  const calendarEnd = endOfWeek(monthEnd);
  const days = eachDayOfInterval({ start: calendarStart, end: calendarEnd });
 
  const getEventsForDay = (day: Date) => {
    const deadlines = resolutions.filter((r) => {
      if (!r.targetDate) return false;
      try {
        const date = parseISO(r.targetDate);
        return isValid(date) && isSameDay(date, day);
      } catch {
        return false;
      }
    });
 
    const dayCheckIns = checkIns.filter((c) => {
      try {
        const date = parseISO(c.date);
        return isValid(date) && isSameDay(date, day);
      } catch {
        return false;
      }
    });
 
    return { deadlines, checkIns: dayCheckIns };
  };
 
  const previousMonth = () => setCurrentDate(subMonths(currentDate, 1));
  const nextMonth = () => setCurrentDate(addMonths(currentDate, 1));
 
  if (isLoading) {
    return (
      <div className="p-6 space-y-6">
        <div className="flex items-center justify-between">
          <Skeleton className="h-9 w-48" />
          <Skeleton className="h-9 w-32" />
        </div>
        <Skeleton className="h-[500px]" />
      </div>
    );
  }
 
  return (
    <div className="p-6 space-y-6">
      <div className="flex items-center justify-between gap-4 flex-wrap">
        <div>
          <h1 className="text-3xl font-semibold" data-testid="text-calendar-title">
            Calendar
          </h1>
          <p className="text-muted-foreground mt-1">
            View deadlines and track your progress over time
          </p>
        </div>
        <div className="flex items-center gap-2">
          <Button variant="outline" size="icon" onClick={previousMonth} data-testid="button-prev-month">
            <ChevronLeft className="h-4 w-4" />
          </Button>
          <span className="text-lg font-medium min-w-[140px] text-center">
            {format(currentDate, "MMMM yyyy")}
          </span>
          <Button variant="outline" size="icon" onClick={nextMonth} data-testid="button-next-month">
            <ChevronRight className="h-4 w-4" />
          </Button>
        </div>
      </div>
 
      <Card className="border-card-border">
        <CardContent className="p-0">
          <div className="grid grid-cols-7 border-b">
            {["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"].map((day) => (
              <div
                key={day}
                className="p-3 text-center text-sm font-medium text-muted-foreground"
              >
                {day}
              </div>
            ))}
          </div>
          <div className="grid grid-cols-7">
            {days.map((day, index) => {
              const { deadlines, checkIns: dayCheckIns } = getEventsForDay(day);
              const isCurrentMonth = isSameMonth(day, currentDate);
              const isToday = isSameDay(day, new Date());
 
              return (
                <div
                  key={index}
                  className={`min-h-[100px] p-2 border-b border-r ${
                    !isCurrentMonth ? "bg-muted/30" : ""
                  }`}
                  data-testid={`calendar-day-${format(day, "yyyy-MM-dd")}`}
                >
                  <div className="flex items-center justify-between mb-1">
                    <span
                      className={`text-sm ${
                        isToday
                          ? "bg-primary text-primary-foreground rounded-full w-6 h-6 flex items-center justify-center font-medium"
                          : isCurrentMonth
                          ? "text-foreground"
                          : "text-muted-foreground"
                      }`}
                    >
                      {format(day, "d")}
                    </span>
                  </div>
                  <div className="space-y-1">
                    {deadlines.slice(0, 2).map((resolution) => (
                      <div
                        key={resolution.id}
                        className="text-xs truncate p-1 rounded bg-primary/10 text-primary"
                      >
                        {resolution.title}
                      </div>
                    ))}
                    {dayCheckIns.length > 0 && (
                      <div className="text-xs p-1 rounded bg-emerald-100 dark:bg-emerald-900/30 text-emerald-700 dark:text-emerald-400">
                        {dayCheckIns.length} check-in{dayCheckIns.length > 1 ? "s" : ""}
                      </div>
                    )}
                    {deadlines.length > 2 && (
                      <div className="text-xs text-muted-foreground">
                        +{deadlines.length - 2} more
                      </div>
                    )}
                  </div>
                </div>
              );
            })}
          </div>
        </CardContent>
      </Card>
 
      <div className="flex items-center gap-4 text-sm text-muted-foreground">
        <div className="flex items-center gap-2">
          <div className="w-3 h-3 rounded bg-primary/10" />
          <span>Deadline</span>
        </div>
        <div className="flex items-center gap-2">
          <div className="w-3 h-3 rounded bg-emerald-100 dark:bg-emerald-900/30" />
          <span>Check-in</span>
        </div>
      </div>
    </div>
  );
}