Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: IRL events sort #398

Merged
merged 3 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 1 addition & 29 deletions components/page/irl/events/irl-all-events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,14 @@ interface EventDetailsProps {

const IrlAllEvents = ({ eventDetails, isLoggedIn, isUpcoming, searchParams, handleDataNotFound }: EventDetailsProps) => {

let eventsToShow = getEventsToShow() || [];
let eventsToShow = eventDetails.events || [];
const [isExpanded, setExpanded] = useState(false);
const [itemsToShow, setItemsToShow] = useState(4);
const dialogRef = useRef<HTMLDivElement>(null);
const [resources, setResources] = useState([]);
const analytics = useIrlAnalytics();
const router = useRouter();

function getEventsToShow() {
if (eventDetails) {
// Determine events to show based on upcoming or past
const events = eventDetails.events;

if (events && events.length > 0) {
// Sort events based on startDate first, then by duration (endDate - startDate)
const sortedEvents = events.sort((a: any, b: any) => {
// First, compare by start date (earlier start first)
const startDateComparison = new Date(a.startDate).getTime() - new Date(b.startDate).getTime();

if (startDateComparison !== 0) {
return startDateComparison;
}

// If start dates are equal, compare by duration (longer duration first)
const durationA = new Date(a.endDate).getTime() - new Date(a.startDate).getTime();
const durationB = new Date(b.endDate).getTime() - new Date(b.startDate).getTime();

return durationB - durationA;
});

// Set eventsToShow based on the sorted events
return sortedEvents;
}
}
}

const toggleDescription = () => {
setItemsToShow(isExpanded ? 4 : itemsToShow + 4);
if (itemsToShow >= eventsToShow?.length - 4) {
Expand Down
1 change: 0 additions & 1 deletion components/page/irl/events/irl-past-events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ const IrlPastEvents = ({ eventDetails, isLoggedIn, isUpcoming, searchParams, han

const [searchText, setSearchText] = useState('');

// Sort pastEvents in descending order by end date if it's not upcoming
const eventsToShow = events;

// Determine the selected event based on searchParams
Expand Down
30 changes: 1 addition & 29 deletions components/page/irl/events/irl-upcoming-events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,14 @@ interface EventDetailsProps {

const IrlUpcomingEvents = ({ eventDetails, isLoggedIn, isUpcoming, searchParams, handleDataNotFound }: EventDetailsProps) => {
const eventType = isUpcoming ? 'Upcoming Events' : 'Past Events';
let eventsToShow = getEventsToShow() || [];
let eventsToShow = eventDetails.upcomingEvents || [];
const [isExpanded, setExpanded] = useState(false);
const [itemsToShow, setItemsToShow] = useState(4);
const dialogRef = useRef<HTMLDivElement>(null);
const [resources, setResources] = useState([]);
const analytics = useIrlAnalytics();
const router = useRouter();

function getEventsToShow() {
if (eventDetails) {
// Determine events to show based on upcoming or past
const events = isUpcoming ? eventDetails.upcomingEvents : [];

if (events && events.length > 0) {
// Sort events based on startDate first, then by duration (endDate - startDate)
const sortedEvents = events.sort((a: any, b: any) => {
// First, compare by start date (earlier start first)
const startDateComparison = new Date(a.startDate).getTime() - new Date(b.startDate).getTime();

if (startDateComparison !== 0) {
return startDateComparison;
}

// If start dates are equal, compare by duration (longer duration first)
const durationA = new Date(a.endDate).getTime() - new Date(a.startDate).getTime();
const durationB = new Date(b.endDate).getTime() - new Date(b.startDate).getTime();

return durationB - durationA;
});

// Set eventsToShow based on the sorted events
return sortedEvents;
}
}
}

const toggleDescription = () => {
setItemsToShow(isExpanded ? 4 : itemsToShow + 4);
if (itemsToShow >= eventsToShow?.length - 4) {
Expand Down
7 changes: 0 additions & 7 deletions services/irl.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ export const getAllLocations = async () => {
}

const result = await response.json();
result.sort((a: { priority: number }, b: { priority: number }) => a.priority - b.priority);

result.forEach((item: { pastEvents: any[] }) => {
if (Array.isArray(item.pastEvents)) {
item.pastEvents = sortPastEvents(item.pastEvents);
}
});

const filteredResult = result.filter(
(item: { pastEvents: IPastEvents[]; upcomingEvents: IUpcomingEvents[]; }) =>
Expand Down