All files / src/viewmodels/event useMyEventsViewModel.ts

94.23% Statements 49/52
55.55% Branches 10/18
100% Functions 9/9
94.11% Lines 48/51

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 1661x         1x 1x   1x                   1x                                                                                     4x 4x             9x   9x 2x 2x   2x 2x                       9x     1x 9x   9x 9x 9x 9x 9x     4x 1x 1x 1x 1x 1x     3x 3x   3x 3x 2x 2x   1x 1x 1x   3x       9x 4x     9x 9x 12x       36x   48x     9x 9x     9x       9x                                  
import { useEffect, useState } from 'react';
import {
  MyEventStatus,
  MyEventSummary,
} from '@/models/event';
import { useAuth } from '@/contexts/AuthContext';
import { listMyEvents } from '@/services/eventService';
 
const STATUS_OPTIONS: Array<{ value: MyEventStatus; label: string }> = [
  { value: 'ACTIVE', label: 'Active' },
  { value: 'IN_PROGRESS', label: 'In Progress' },
  { value: 'COMPLETED', label: 'Completed' },
  { value: 'CANCELED', label: 'Canceled' },
];
 
const EMPTY_STATE_COPY: Record<
  MyEventStatus,
  { title: string; subtitle: string }
> = {
  ACTIVE: {
    title: 'No active events right now',
    subtitle: 'Hosted plans and upcoming participations will appear here.',
  },
  IN_PROGRESS: {
    title: 'Nothing is in progress',
    subtitle: 'Events that are currently happening will show up here.',
  },
  COMPLETED: {
    title: 'No completed events yet',
    subtitle: 'Your hosted wrap-ups and participation history will build here.',
  },
  CANCELED: {
    title: 'No canceled events',
    subtitle: 'Canceled hosted or attended events will appear here when needed.',
  },
};
 
export interface MyEventsStatusTab {
  value: MyEventStatus;
  label: string;
  count: number;
}
 
export interface MyEventsViewModel {
  activeStatus: MyEventStatus;
  statusTabs: MyEventsStatusTab[];
  hostedEvents: MyEventSummary[];
  attendedEvents: MyEventSummary[];
  visibleEvents: MyEventSummary[];
  hostedCount: number;
  attendedCount: number;
  isLoading: boolean;
  errorMessage: string | null;
  canRetry: boolean;
  emptyTitle: string;
  emptySubtitle: string;
  setActiveStatus: (status: MyEventStatus) => void;
  reload: () => Promise<void>;
}
 
function getTimeValue(value: string) {
  const time = new Date(value).getTime();
  return Number.isNaN(time) ? 0 : time;
}
 
function sortVisibleEvents(
  events: MyEventSummary[],
  status: MyEventStatus,
): MyEventSummary[] {
  const sorted = [...events];
 
  sorted.sort((first, second) => {
    const firstTime = getTimeValue(first.start_time);
    const secondTime = getTimeValue(second.start_time);
 
    Eif (firstTime !== secondTime) {
      return status === 'ACTIVE' || status === 'IN_PROGRESS'
        ? firstTime - secondTime
        : secondTime - firstTime;
    }
 
    if (first.relation !== second.relation) {
      return first.relation === 'HOSTING' ? -1 : 1;
    }
 
    return first.title.localeCompare(second.title);
  });
 
  return sorted;
}
 
export function useMyEventsViewModel(): MyEventsViewModel {
  const { token } = useAuth();
 
  const [activeStatus, setActiveStatus] = useState<MyEventStatus>('ACTIVE');
  const [hostedEvents, setHostedEvents] = useState<MyEventSummary[]>([]);
  const [attendedEvents, setAttendedEvents] = useState<MyEventSummary[]>([]);
  const [isLoading, setIsLoading] = useState(true);
  const [errorMessage, setErrorMessage] = useState<string | null>(null);
 
  async function reload() {
    if (!token) {
      setHostedEvents([]);
      setAttendedEvents([]);
      setErrorMessage('You must be logged in to manage your events.');
      setIsLoading(false);
      return;
    }
 
    setIsLoading(true);
    setErrorMessage(null);
 
    try {
      const response = await listMyEvents(token);
      setHostedEvents(response.hosted_events);
      setAttendedEvents(response.attended_events);
    } catch {
      setHostedEvents([]);
      setAttendedEvents([]);
      setErrorMessage('Failed to load your events. Please try again.');
    } finally {
      setIsLoading(false);
    }
  }
 
  useEffect(() => {
    void reload();
  }, [token]);
 
  const allEvents = [...hostedEvents, ...attendedEvents];
  const visibleEvents = sortVisibleEvents(
    allEvents.filter((event) => event.status === activeStatus),
    activeStatus,
  );
 
  const statusTabs = STATUS_OPTIONS.map((statusOption) => ({
    ...statusOption,
    count: allEvents.filter((event) => event.status === statusOption.value).length,
  }));
 
  const hasAnyEvents = allEvents.length > 0;
  const emptyTitle = hasAnyEvents
    ? EMPTY_STATE_COPY[activeStatus].title
    : 'No events to manage yet';
  const emptySubtitle = hasAnyEvents
    ? EMPTY_STATE_COPY[activeStatus].subtitle
    : 'Events you host or join will appear here once your plans start coming together.';
 
  return {
    activeStatus,
    statusTabs,
    hostedEvents,
    attendedEvents,
    visibleEvents,
    hostedCount: hostedEvents.length,
    attendedCount: attendedEvents.length,
    isLoading,
    errorMessage,
    canRetry: Boolean(token),
    emptyTitle,
    emptySubtitle,
    setActiveStatus,
    reload,
  };
}